<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gulics.com &#187; Logging</title>
	<atom:link href="http://blog.gulics.com/tag/logging/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.gulics.com</link>
	<description>Metal, Movies, Web Development, and More</description>
	<lastBuildDate>Wed, 29 Jun 2011 14:42:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Using Kernel#caller to Log Method Name and Line Number</title>
		<link>http://blog.gulics.com/2009/06/30/using-kernelcaller-to-log-method-name-and-line-number/</link>
		<comments>http://blog.gulics.com/2009/06/30/using-kernelcaller-to-log-method-name-and-line-number/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 22:53:13 +0000</pubDate>
		<dc:creator>Stephen Gulics</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Caller]]></category>
		<category><![CDATA[Logging]]></category>

		<guid isPermaLink="false">http://blog.gulics.com/?p=39</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>I was going through the <a href="http://www.webofmetal.com">Web of Metal</a> 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. </p>
<p>Your can download the source from <a href="http://github.com/sgulics/gulics.com/tree/master/log_helper/">Git hub</a></p>
<p>The module adds 5 helper methods to your class: log_debug, log_info, log_warn, log_error, and log_fatal</p>
<p>Whenever you call one of the methods it will log the message in the following format:<br />
<code><br />
classname:method_name:(line_number):your_message<br />
</code></p>
<p>This is what the code would look like in your method:</p>
<pre name="code" class="ruby">
def do_something
  log_debug(“entering method”)
end
</pre>
<p>Here is an example of the log:<br />
<code>LogHelperExample:do_something:(14):entering method</code></p>
<p>The module will also print out the log statements to the screen if the RAILS_ENV is &#8216;test&#8217; or &#8216;development&#8217;. I found that useful for my app but you may want to remove it. </p>
<p>The module creates the helper methods using the following code:</p>
<pre class="brush: ruby;">
def self.define_log_helper(base, level)
  base.class_eval &lt;&lt;-end_eval
    def log_#{level}(string, &amp;block)
      log_string = &quot;\#{get_caller(caller)}:\#{string}&quot;
      puts log_string if defined?(RAILS_ENV) &amp;&amp; (RAILS_ENV == 'test' || RAILS_ENV == 'development')
      get_logger.#{level}(log_string, &amp;block)
    end
  end_eval
end
</pre>
<p>Getting the name of the method and line number was relatively easy. The key is to use the private method caller from <a href="http://www.ruby-doc.org/core/classes/Kernel.html#M005955">Kernel</a></p>
<p>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. </p>
<pre class="brush: ruby;">
def get_caller(ca)
  &quot;#{self.class.name}:#{$2}:(#{$1})&quot; if /:(\d*):in `([^']*)'/.match( ca[0])
 end
</pre>
<p>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: </p>
<p><code><br />
['/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'',.....<br />
</code></p>
<p>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.&lt;log level&gt; directly will not have the extra information</p>
<p>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:</p>
<pre class="brush: ruby;">
for severity in Severity.constants
  class_eval &lt;&lt;-EOT, __FILE__, __LINE__
    def #{severity.downcase}(message = nil, progname = nil, &amp;block)
      if /(\\w*)\\.rb:(\\d*):in `([^']*)'/.match( caller[0])
        message = &quot;\#{$1.camelize}:\#{$3}:(\#{$2}):\#{message}&quot;
      end
      add(#{severity}, message, progname, &amp;block)
    end
    def #{severity.downcase}?
      #{severity} &gt;= @level
    end
  EOT
end
</pre>
<p>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. </p>
<p>Well that is about it. Hope you find something useful in this post. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gulics.com/2009/06/30/using-kernelcaller-to-log-method-name-and-line-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

