class RJR::Logger

Logger helper class.

Encapsulates the standard ruby logger in a thread safe manner. Dispatches class methods to an internally tracked logger to provide global access.

TODO handle logging errors (log size too big, logrotate, etc)

@example

RJR::Logger.info 'my message'
RJR::Logger.warn 'my warning'

Public Class Methods

add_filter(filter) click to toggle source

Add method which to call on every log message to determine if messages should be included/excluded

# File lib/rjr/util/logger.rb, line 38
def self.add_filter(filter)
  @logger_mutex.synchronize{
    @filters << filter
  }
end
debug?() click to toggle source

Return true if log level is set to debug, else false

# File lib/rjr/util/logger.rb, line 112
def self.debug?
  @log_level == ::Logger::DEBUG
end
highlight(hlight) click to toggle source

Add a method which to call on every log message to determine if message should be highlighted

# File lib/rjr/util/logger.rb, line 46
def self.highlight(hlight)
  @logger_mutex.synchronize{
    @highlights << hlight
  }
end
log_level=(level) click to toggle source

Set log level. @param level one of the standard rails log levels (default fatal)

# File lib/rjr/util/logger.rb, line 91
def self.log_level=(level)
  _instantiate_logger
  if level.is_a?(String)
    level = case level
            when 'debug' then
              ::Logger::DEBUG
            when 'info' then
              ::Logger::INFO
            when 'warn' then
              ::Logger::WARN
            when 'error' then
              ::Logger::ERROR
            when 'fatal' then
              ::Logger::FATAL
            end
  end
  @log_level    = level
  @logger.level = level
end
log_to(dst) click to toggle source

Set log destination @param dst destination which to log to (file name, STDOUT, etc)

# File lib/rjr/util/logger.rb, line 83
def self.log_to(dst)
  @log_to = dst
  @logger = nil
  _instantiate_logger
end
logger() click to toggle source
# File lib/rjr/util/logger.rb, line 76
def self.logger
   _instantiate_logger
   @logger
end
method_missing(method_id, *args) click to toggle source
# File lib/rjr/util/logger.rb, line 52
def self.method_missing(method_id, *args)
   _instantiate_logger
   @logger_mutex.synchronize {
     # TODO option to indent & prepend configurable prefix to extra log lines after first entry
     args = args.first if args.first.is_a?(Array)
     shouldnt_log = args.any? { |a| @filters.any? { |f| !f.call a } }
     args.each { |a|
       # run highlights / filters against output before
       # sending formatted output to logger
       # TODO allow user to customize highlight mechanism/text
       na = @highlights.any? { |h| h.call a } ?
              "\e[1m\e[31m#{a}\e[0m\e[0m" : a
       @logger.send(method_id, na)
     } unless shouldnt_log
   }
end
safe_exec(*args, &bl) click to toggle source
# File lib/rjr/util/logger.rb, line 69
def self.safe_exec(*args, &bl)
  _instantiate_logger
  @logger_mutex.synchronize {
    bl.call *args
  }
end

Private Class Methods

_instantiate_logger() click to toggle source
# File lib/rjr/util/logger.rb, line 22
def self._instantiate_logger
   if @logger.nil?
     #STDOUT.sync = true
     output = @log_to || ENV['RJR_LOG'] || STDOUT
     @logger = ::Logger.new(output)
     @logger.level = @log_level || ::Logger::FATAL
     @logger_mutex = Mutex.new
     @filters = []
     @highlights = []
   end
end