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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OK, this makes a lot more sense after the edit.… May 17, 2026 at 2:31 am
  • Editorial Team
    Editorial Team added an answer Working from 1) a type is dependent if it is… May 17, 2026 at 2:31 am
  • Editorial Team
    Editorial Team added an answer The []= method returns the assigned value, so the value… May 17, 2026 at 2:31 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Here is My Code To Log In var expire = DateTime.Now.AddDays(7); // Create a
I have some code that is using mechanize and a password protected site. I
So we've produced a windows service to feed data to our client application and
I have a log file containing the following data: Shortest path(2)::RV3280-RV0973C-RV2888C Shortest path(1)::RV3280-RV2502C Shortest
I have some python code on both the client and server side. I am
I've faced with a next problem: In our database we have objects with ids,
I have this piece of code which I'm hoping will be able to tell
I have a fairly substantial library of web services built in .NET that I
I have a bunch of simple lookup tables cached in my asp.net application since
I had some problems figuring out a good title, but hopefully the code examples

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.