# lib/logger/custom_logger
require 'singleton'
class CustomLogger < Logger
include Singleton
def initialize
super(Rails.root.join('log/custom_logger.log'))
self.formatter = formatter()
self
end
def formatter
Proc.new{|severity, time, progname, msg|
formatted_severity = sprintf("%-5s",severity.to_s)
formatted_time = time.strftime("%d-%m-%Y %H:%M:%S")
"[#{formatted_severity} #{formatted_time} #{$$}] #{msg.to_s.strip}\n"
}
end
class << self
delegate :error, :debug, :fatal, :info, :warn, :add, :log, :to => :instance
end
end
Usage:
CustomLogger.info("text to log")
CustomLogger.warn("text to log")
CustomLogger.debug("text to log")
CustomLogger.fatal("text to log")
CustomLogger.error("text to log")
Example
[INFO 04-04-2015 14:12:57 18491] Processing data</pre>