Skip to content

Instantly share code, notes, and snippets.

@thbishop
Created January 6, 2012 18:49
Show Gist options
  • Save thbishop/1571881 to your computer and use it in GitHub Desktop.
Save thbishop/1571881 to your computer and use it in GitHub Desktop.
custom logger wrapping slogger gem
CustomLogger.info 'here is info'
CustomLogger.debug'here is debug'
CustomLogger.warn 'here is warn'
CustomLogger.error 'here is error'
# and this will record the execution time of the block and inject it into the message
CustomLogger.info 'here is a timing item. i wonder how long it will take' do
sleep 10
end
class CustomLogger
LOG_LEVELS = ['debug', 'info', 'warn', 'error']
def self.log(level, *args, &block)
setup_logger unless @logger
@logger.send level.to_sym, *args, &block
end
class << self
LOG_LEVELS.each do |level|
define_method level do |*args, &block|
self.log level, *args, &block
end
end
end
private
def self.setup_logger
@logger = Slogger::CommonLogger.new determine_app_name,
determine_level,
determine_facility
end
def self.determine_app_name
default_app_name = 'custom_logger'
app_name = ENV['LOGGER_APP_NAME'] || default_app_name
if app_name == default_app_name && (Object.const_defined? :Rails)
app_name = Rails.application.config.app_name
end
app_name
end
def self.determine_level
ENV['LOGGER_LEVEL'] || :info
end
def self.determine_facility
ENV['LOGGER_FACILITY'] || :local2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment