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)


112
113
114
# File 'lib/rjr/util/logger.rb', line 112

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)



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object

Set log destination

Parameters:

  • dst

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



83
84
85
86
87
# File 'lib/rjr/util/logger.rb', line 83

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

.loggerObject



76
77
78
79
# File 'lib/rjr/util/logger.rb', line 76

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
66
67
# 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) ⇒ Object



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

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