<?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; Rails</title>
	<atom:link href="http://blog.gulics.com/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.gulics.com</link>
	<description>Metal, Movies, Web Development, and More</description>
	<lastBuildDate>Thu, 01 Oct 2009 01:01:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 [...]]]></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>
		<item>
		<title>ActionMailer, DreamHost and &#8220;hostname was not match with the server certificate&#8221;</title>
		<link>http://blog.gulics.com/2009/03/22/actionmailer-dreamhost-and-hostname-was-not-match-with-the-server-certificate/</link>
		<comments>http://blog.gulics.com/2009/03/22/actionmailer-dreamhost-and-hostname-was-not-match-with-the-server-certificate/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 23:46:15 +0000</pubDate>
		<dc:creator>Stephen Gulics</dc:creator>
				<category><![CDATA[Dreamhost]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Web of Metal]]></category>
		<category><![CDATA[ActionMailer]]></category>

		<guid isPermaLink="false">http://blog.gulics.com/?p=11</guid>
		<description><![CDATA[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&#8217;s Wiki. So [...]]]></description>
			<content:encoded><![CDATA[<p>Today I wanted to add a simple Suggestion Box to <a href="http://www.webofmetal.com" target="_blank">Web of Metal</a>. 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.</p>
<p>A quick google search and I found a simple post on Dreamhost&#8217;s <a href="http://wiki.dreamhost.com/ActionMailer" target="_blank">Wiki</a>. So I configured my settings locally and successfully sent the email. These are the settings that I used:</p>
<pre name="code" class="ruby">
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address          =&gt; 'mail.domain.com',
:port                =&gt; 25,
:domain           =&gt; 'domain.com',
:user_name        =&gt; 'mailer@domain.com',
:password         =&gt; 'password',
:authentication   =&gt; :login,
}
</pre>
<p>After adding the settings to my environment.rb I did a quick test and everything worked. &#8220;Great!&#8221; I said, so now lets move the code to Dreamhost. And that&#8217;s were it all went downhill.</p>
<p>The functionality did not work in production. I looked at the logs and found the following error in the log:</p>
<pre>HomeController:rescue_action_without_handler:hostname was not match with the server certificate</pre>
<p>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&#8217;s SMTP server supports it because it&#8217;s definitely turned it on. According to Dan Ryan in this <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1491-ssl-problem-with-smtp-in-222" target="_blank">post </a></p>
<blockquote><p>&#8220;You&#8217;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.&#8221;</p></blockquote>
<p>This issue is going to be <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1731" target="_blank">fixed</a> 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 <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1491-ssl-problem-with-smtp-in-222" target="_blank">post</a> 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</p>
<pre class="brush: ruby;">
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 &quot;hostname was not match with the server certificate&quot;
  # 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
</pre>
<p>Hope this helps anyone else having the issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gulics.com/2009/03/22/actionmailer-dreamhost-and-hostname-was-not-match-with-the-server-certificate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
