Class: RJR::Logger

Inherits:
Object show all
Defined in:
lib/rjr/common.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



56
57
58
59
60
# File 'lib/rjr/common.rb', line 56

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

.debug?Boolean

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



128
129
130
# File 'lib/rjr/common.rb', line 128

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



64
65
66
67
68
# File 'lib/rjr/common.rb', line 64

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

.log_level=(level) ⇒ Object

Set log level.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rjr/common.rb', line 107

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



99
100
101
102
103
# File 'lib/rjr/common.rb', line 99

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

.loggerObject



92
93
94
95
# File 'lib/rjr/common.rb', line 92

def self.logger
   _instantiate_logger
   @logger
end

.method_missing(method_id, *args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rjr/common.rb', line 70

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



85
86
87
88
89
90
# File 'lib/rjr/common.rb', line 85

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