Code tagged with logging

A nice simple jquery logger

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};
Language JavaScript / Tagged with jquery, logging

Hodel 3000 Compliant Logger

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)
Language Ruby / Tagged with logging, rails, pl_analyze

Tail your logs with capistrano

task :tail_log, :roles => :app do
  stream "tail -f #{shared_path}/log/production.log"
end
Language Ruby / Tagged with rails, capistrano, logging

Turn Deprecation Warnings Off in Rails

To turn off all deprecation warnings, just do the following:
ActiveSupport::Deprecation.silenced = true
Language Ruby / Tagged with rails, logging

Add back date, time, and logging level to logger

Posted by Chad Humphries about 1 year ago
# 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
Language Ruby / Tagged with rails, logging

Rotating your Loggers in Rails

Posted by Chad Humphries about 1 year ago
# Rotate the log at 10 megabyte, keeping the last 10
RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log", 10, 10000000)
Language Ruby / Tagged with rails, logging