Created
January 6, 2012 18:49
-
-
Save thbishop/1571881 to your computer and use it in GitHub Desktop.
custom logger wrapping slogger gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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