Permalink
Browse files

Improvements to hodel logger, with spec

  • Loading branch information...
1 parent 9e51d3b commit ed4f2c1d3b86c19f8d0d1159fe9752d6bb10e9be @topfunky committed Nov 29, 2007
Showing with 74 additions and 3 deletions.
  1. +4 −0 README.txt
  2. +4 −0 Rakefile
  3. +28 −3 lib/hodel_3000_compliant_logger.rb
  4. +38 −0 spec/hodel_3000_compliant_logger_spec.rb
View
@@ -17,3 +17,7 @@ If you are using FastCGI, you may need to hard-code the hostname instead of usin
Geoffrey Grosenbach, with help from Eric Hodel
http://topfunky.com
+
+== Changes
+
+* Nov 29, 2007: Improvements and spec from Chris Bernard [http://logicleaf.com/]
View
@@ -0,0 +1,4 @@
+desc "Run specs"
+task :default do
+ system "spec spec"
+end
@@ -10,7 +10,32 @@ class Hodel3000CompliantLogger < Logger
##
# Note: If you are using FastCGI you may need to hard-code the hostname here instead of using Socket.gethostname
- def format_message(severity, timestamp, msg, progname)
- "#{timestamp.strftime("%b %d %H:%M:%S")} #{Socket.gethostname.split('.').first} rails[#{$PID}]: #{progname.gsub(/\n/, '').lstrip}\n"
+ def format_message(severity, timestamp, progname, msg)
+ "#{timestamp.strftime("%b %d %H:%M:%S")} #{hostname} rails[#{$PID}]: #{msg2str(msg).gsub(/\n/, '').lstrip}\n"
end
-end
+
+ # original method, pre-patch for Exception handling:
+ #
+ # def format_message(severity, timestamp, msg, progname)
+ # "#{timestamp.strftime("%b %d %H:%M:%S")} #{Socket.gethostname.split('.').first} rails[#{$PID}]: #{progname.gsub(/\n/, '').lstrip}\n"
+ # end
+
+ private
+
+ def hostname
+ @parsed_hostname ||= Socket.gethostname.split('.').first
+ end
+
+ def msg2str(msg)
+ case msg
+ when ::String
+ msg
+ when ::Exception
+ "#{ msg.message } (#{ msg.class }): " <<
+ (msg.backtrace || []).join(" | ")
+ else
+ msg.inspect
+ end
+ end
+
+end
@@ -0,0 +1,38 @@
+$: << File.dirname(__FILE__) + "/../lib"
+
+require 'rubygems'
+require 'spec'
+require 'hodel_3000_compliant_logger'
+
+describe Hodel3000CompliantLogger do
+
+ before :each do
+ @out = StringIO.new
+ @log = Hodel3000CompliantLogger.new(@out)
+ end
+
+ it "should log stuff in a syslog-like format so that Eric Hodel's Rails Analyzer Tools can parse it" do
+ msg = "Yo ho hello there!"
+ @log.info(msg)
+ @out.string.should match(/^\w{3} \d{2} \d{2}:\d{2}:\d{2} \w+ rails\[\d+\]: #{msg}\n$/)
+ end
+
+ it "should handle an Exception object used as an argument in Logger#error, rather than blow chunks" do
+ @log.error(Exception.new)
+ @out.string.should match(/Exception/)
+ end
+
+ it "should display a semi-readable stack trace (albiet on one line) when Logger#error(SomeException) was called" do
+ @log.error(toss_runtime_error)
+ @out.string.should match(/.*? \| .*? \| .*? \|/) # pipe separated stack frames
+ @out.string.should match(/\n$/)
+ @out.string.count("\n").should == 1
+ end
+
+ def toss_runtime_error
+ raise "Catastrophic Failure"
+ rescue => e
+ return e
+ end
+end
+

0 comments on commit ed4f2c1

Please sign in to comment.