Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 545159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:43:55+00:00 2026-05-13T10:43:55+00:00

We have code to log data in our Ruby 1.8.6 web application. You call

  • 0

We have code to log data in our Ruby 1.8.6 web application. You call it roughly as follows:

$log.info("Some text here")

Now, in the logged output, I would like to include the module where that line appeared. I know that the Kernel#caller will give me an array where I can pull out the file and line number that the log line occurred, but I don’t want that. I want the module, not the file name. The obvious solution is to modify the log line so that it reads like:

$log.info("Some text here", self.class.name)

and then parse the result. That’s not going to work, though, because I am trying to extract this information in the default case. That is, I need the solution to work if the programmer forgot to specify the module, the second parameter to the log line.

Is there any way to do this? If not, I will just have to make do with the caller array; most of our modules are in separate directories, so this would be an 80% solution.

More complete example, please excuse minor syntax errors:

in file log.rb:

module Log
  class Logger
    def info(msg, mod = '')
      puts "Module: #{mod}  Msg: #{msg}"
    end
  end # class Logger
end # module Log
$log = Log::Logger.new

in file foo.rb:

module Foo
  class Bar
    def do_something
      # Do not pass in self.class.name.
      # We want the output to look like:
      # Module: Foo  Msg: I did something!
      $log.info "I did something!"
    end
  end # class Bar
end #module Foo
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:43:56+00:00Added an answer on May 13, 2026 at 10:43 am

    Use call_stack.

    First install it with RubyGems:

    gem install call_stack
    

    Then change log.rb to:

    require 'rubygems'
    require 'call_stack'
    
    call_stack_on
    
    module Log
      class Logger
        def info(msg, mod = '')
            mod = call_stack(2)[0][0] if mod == ''
            puts "Module: #{mod}  Msg: #{msg}"
        end
      end # class Logger
    end # module Log
    $log = Log::Logger.new
    

    Works for me (Ruby 1.8.7).

    $ ruby foo.rb
    Module: Foo::Bar  Msg: I did something!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code inside a iframe: window.addEventListener('message', function(e){ if(e.data == 'test') console.log(e); },
How to print both to Log.e/d/etc. and to System.out/err/etc.? I have some code that
We have an ASMX web service which we invoke from our ASP.NET application using
I have the following code: Log.e(MainScreen, 1); ListAction listAction = new ListAction(this); Log.e(MainScreen, 2);
I have this code in JS: if($('.log input:checked')) { alert('login checked'); alert('do what you
I have this code: function Keyboard() { this.log = $('#log')[0]; this.pressedKeys = []; this.bindUpKeys
I have this code App.Model('users').find(function (err, users) { users.forEach(function(user) { console.log(user.username); }); }); //make
I have this code.. String you = buf.readLine(); Log.d(STRING, ikaw); StringTokenizer st = new
I have this code , and I want to run Log.d every 1000 milis
I have the following code snippet: #ifdef DO_LOG #define log(p) record(p) #else #define log(p)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.