Skip to content

Instantly share code, notes, and snippets.

@deepak
Created June 25, 2010 06:55
Show Gist options
  • Save deepak/452539 to your computer and use it in GitHub Desktop.
Save deepak/452539 to your computer and use it in GitHub Desktop.
logging the pid also so that the logs can be collected while doing distributed logging eg. passenger/mongrel
# source: http://gist.github.com/452539
# thanks to EP on
# http://groups.google.com/group/phusion-passenger/
# browse_thread/thread/a3221e0e15adc1ec
# [logger per spawner]
# add it to config/initializer in rails
# tested with rails-2.3.5
# simply giving the pid in the config will not work as it will be the
# pid of the ApplicationSpawner and not the worker
unless defined? Logger
require 'logger'
#puts "==== requiring logger (pid: #{$$})"
end
unless defined? ActiveSupport::BufferedLogger
require 'active_support'
#puts "==== requiring active_support (pid: #{$$})"
end
class Logger
class Formatter
private
def msg2str(msg)
case msg
when ::String
"msg (pid: #{$$})"
when ::Exception
"#{ msg.message } (#{ msg.class }) (pid: #{$$})\n" <<
(msg.backtrace || []).join("(pid: #{$$})\n")
else
"#{msg.inspect} (pid: #{$$})"
end
end
end
# Simple formatter which only displays the message.
class SimpleFormatter < Logger::Formatter
# This method is invoked when a log event occurs
def call(severity, timestamp, progname, msg)
"#{String === msg ? msg : msg.inspect} (pid: #{$$})\n"
end
end
private
def msg2str(msg) "msg (pid: #{$$})" end
end
module ActiveSupport
# Inspired by the buffered logger idea by Ezra
class BufferedLogger
def add(severity, message = nil, progname = nil, &block)
return if @level > severity
message = (message || (block && block.call) || progname).to_s
# If a newline is necessary then create a new message ending with a newline.
# Ensures that the original message is not mutated.
if message[-1] == ?\n
message = "#{message[0..-2]} (pid: #{$$})\n"
else
message = "#{message} (pid: #{$$})\n"
end
buffer << message
auto_flush
message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment