Class: RJR::Logger

Inherits:
Object show all
Defined in:
lib/rjr/util/logger.rb

Overview

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)

Examples:

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

Class Method Summary collapse

Class Method Details

.add_filter(filter) ⇒ Object

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



38
39
40
41
42
# File 'lib/rjr/util/logger.rb', line 38

def self.add_filter(filter)
  @logger_mutex.synchronize{
    @filters << filter
  }
end

.debug?Boolean

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

Returns:

  • (Boolean)


110
111
112
# File 'lib/rjr/util/logger.rb', line 110

def self.debug?
  @log_level == ::Logger::DEBUG
end

.highlight(hlight) ⇒ Object

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



46
47
48
49
50
# File 'lib/rjr/util/logger.rb', line 46

def self.highlight(hlight)
  @logger_mutex.synchronize{
    @highlights << hlight
  }
end

.log_level=(level) ⇒ Object

Set log level.

Parameters:

  • level

    one of the standard rails log levels (default fatal)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rjr/util/logger.rb', line 89

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) ⇒ Object

Set log destination

Parameters:

  • dst

    destination which to log to (file name, STDOUT, etc)



81
82
83
84
85
# File 'lib/rjr/util/logger.rb', line 81

def self.log_to(dst)
  @log_to = dst
  @logger = nil
  _instantiate_logger
end

.loggerObject



74
75
76
77
# File 'lib/rjr/util/logger.rb', line 74

def self.logger
   _instantiate_logger
   @logger
end

.method_missing(method_id, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rjr/util/logger.rb', line 52

def self.method_missing(method_id, *args)
   _instantiate_logger
   @logger_mutex.synchronize {
     args = args.first if args.first.is_a?(Array)
     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) if @filters.all? { |f| f.call a }
     }
   }
end

.safe_exec(*args, &bl) ⇒ Object



67
68
69
70
71
72
# File 'lib/rjr/util/logger.rb', line 67

def self.safe_exec(*args, &bl)
  _instantiate_logger
  @logger_mutex.synchronize {
    bl.call *args
  }
end