Code tagged with logging
jQuery.fn.log = function (msg) {
console.log("%s: %o", msg, this);
return this;
};
TopFunky introduced us all to the concept of a Hodel 3000 Compliant Logger. In the real world this logger let's you take advantage of the pl_analyze tools for log analysis by reformatting the log to a format it can parse.
The logger:
require 'logger'
require 'English'
# Jan 2 03:38:05 topfunky postfix/postqueue[2947]: warning blah blah blah
# A logger for use with pl_analyze and other tools that expect syslog-style log output.
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"
end
end
And to use it in your app just reset your logger (in your production.rb for example)
# The new automagically, log-a-fantastic hodel 3000 compliant logger (now in technicolor)
config.logger = Hodel3000CompliantLogger.new(config.log_path)
task :tail_log, :roles => :app do
stream "tail -f #{shared_path}/log/production.log"
end
To turn off all deprecation warnings, just do the following:
ActiveSupport::Deprecation.silenced = true
# Put something like this at the end of your environment.rb.
require 'logger'
class Logger
alias format_message old_format_message
def format_message(severity, timestamp, progname, msg)
"#{timestamp.to_s(:log_format)} [#{severity}] - #{msg}\n"
end
end
# Rotate the log at 10 megabyte, keeping the last 10
RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log", 10, 10000000)