Using Kernel#caller to Log Method Name and Line Number

I was going through the Web of Metal app logs and thought that it would be helpful if the log statements also printed the method name and line number along with the message being logged. I wrote this little helper module that I thought you might find helpful.

Your can download the source from Git hub

The module adds 5 helper methods to your class: log_debug, log_info, log_warn, log_error, and log_fatal

Whenever you call one of the methods it will log the message in the following format:

classname:method_name:(line_number):your_message

This is what the code would look like in your method:

def do_something
  log_debug(“entering method”)
end

Here is an example of the log:
LogHelperExample:do_something:(14):entering method

The module will also print out the log statements to the screen if the RAILS_ENV is ‘test’ or ‘development’. I found that useful for my app but you may want to remove it.

The module creates the helper methods using the following code:

def self.define_log_helper(base, level)
  base.class_eval <<-end_eval
    def log_#{level}(string, &block)
      log_string = "\#{get_caller(caller)}:\#{string}"
      puts log_string if defined?(RAILS_ENV) && (RAILS_ENV == 'test' || RAILS_ENV == 'development')
      get_logger.#{level}(log_string, &block)
    end
  end_eval
end

Getting the name of the method and line number was relatively easy. The key is to use the private method caller from Kernel

The caller method places the execution stack into an array object. The line number and method name are retrieved from the LogHelper.get_caller method.

def get_caller(ca)
  "#{self.class.name}:#{$2}:(#{$1})" if /:(\d*):in `([^']*)'/.match( ca[0])
 end

The code above pulls the first element of the array, which contains the value that we want to parse. It will look like the following:


['/Users/gulicss/source/blog/log_helper/test/log_helper_example.rb:5:in `do_something'', '/Users/gulicss/source/blog/log_helper/test/log_helper_test.rb:16:in `test_should_pass_in_callers_classname_method_name_and_line_number'', '/Library/Ruby/Gems/1.8/gems/mocha-0.9.3/lib/mocha/test_case_adapter.rb:69:in `__send__'', '/Library/Ruby/Gems/1.8/gems/mocha-0.9.3/lib/mocha/test_case_adapter.rb:69:in `run'', '/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'',.....

A downside to this implementation is that it really only works in classes that include the LogHelper module and use the helper methods. Any class that uses logger.<log level> directly will not have the extra information

In Rails 2.2.2 there is no way to change the format of the Logger output . If you want to add this functionality so that the format of all logged message displays the method name and line number you would have to modify BufferedLogger directly like in the following:

for severity in Severity.constants
  class_eval <<-EOT, __FILE__, __LINE__
    def #{severity.downcase}(message = nil, progname = nil, &block)
      if /(\\w*)\\.rb:(\\d*):in `([^']*)'/.match( caller[0])
        message = "\#{$1.camelize}:\#{$3}:(\#{$2}):\#{message}"
      end
      add(#{severity}, message, progname, &block)
    end
    def #{severity.downcase}?
      #{severity} >= @level
    end
  EOT
end

Notice in the above I parse out the class name from caller[0]. This is required because the value of self.class.name will now be ActiveSupport::BufferedLogger.

Well that is about it. Hope you find something useful in this post.

Leave a Comment

ActionMailer, DreamHost and “hostname was not match with the server certificate”

Today I wanted to add a simple Suggestion Box to Web of Metal. I planned out about 2 hours of my Sunday since the only unknown that I thought I would have to deal with was setting up the ActionMailer for Dreamhost.

A quick google search and I found a simple post on Dreamhost’s Wiki. So I configured my settings locally and successfully sent the email. These are the settings that I used:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address          => 'mail.domain.com',
:port                => 25,
:domain           => 'domain.com',
:user_name        => 'mailer@domain.com',
:password         => 'password',
:authentication   => :login,
}

After adding the settings to my environment.rb I did a quick test and everything worked. “Great!” I said, so now lets move the code to Dreamhost. And that’s were it all went downhill.

The functionality did not work in production. I looked at the logs and found the following error in the log:

HomeController:rescue_action_without_handler:hostname was not match with the server certificate

Ugh. To make a long story short it looks like a problem in Rails 2.2.2 with SSL  on the mailserver. It seems that Rails 2.2.2 automatically turns on something called STARTTLS if your using Ruby 1.8.7 and your SMTP server supports STARTTLS. Well I guess Dreamhost’s SMTP server supports it because it’s definitely turned it on. According to Dan Ryan in this post

“You’ll see this error if your mailserver has an SSL certificate set up for a hostname other than the one to which you are trying to connect. I was experiencing the same error, until I changed my smtp_settings to reflect the hostname of the associated SSL cert.”

This issue is going to be fixed in Rails 2.3 but since I am using 2.2.2, I needed a way to turn it off now. I guess I could of wrote a plugin using the information found in the post above but I found it just as easy to  modify the Rails code. I know, that is a bad practice but I did not feel like wasting any more of my time and my Sunday with this issue. Since Rails is frozen in my vendor directory I just modified vendor/rails/lib/action_mailer/base.rb

def perform_delivery_smtp(mail)
  destinations = mail.destinations
  mail.ready_to_send
  sender = mail['return-path'] || mail.from
  smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
  # Comment out the following to fix the "hostname was not match with the server certificate"
  # smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
  smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
  smtp_settings[:authentication]) do |smtp|
  smtp.sendmail(mail.encoded, sender, destinations)
end

Hope this helps anyone else having the issue.

Leave a Comment