Skip to content

Commit

Permalink
Improvements to hodel logger, with spec
Browse files Browse the repository at this point in the history
  • Loading branch information
topfunky committed Nov 29, 2007
1 parent 9e51d3b commit ed4f2c1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.txt
Expand Up @@ -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/]
4 changes: 4 additions & 0 deletions Rakefile
@@ -0,0 +1,4 @@
desc "Run specs"
task :default do
system "spec spec"
end
31 changes: 28 additions & 3 deletions lib/hodel_3000_compliant_logger.rb
Expand Up @@ -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
38 changes: 38 additions & 0 deletions spec/hodel_3000_compliant_logger_spec.rb
@@ -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.