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



45
46
47
48
49
# File 'lib/rjr/common.rb', line 45

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)


117
118
119
# File 'lib/rjr/common.rb', line 117

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



53
54
55
56
57
# File 'lib/rjr/common.rb', line 53

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)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rjr/common.rb', line 96

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)



88
89
90
91
92
# File 'lib/rjr/common.rb', line 88

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

.loggerObject



81
82
83
84
# File 'lib/rjr/common.rb', line 81

def self.logger
   _instantiate_logger
   @logger
end

.method_missing(method_id, *args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rjr/common.rb', line 59

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



74
75
76
77
78
79
# File 'lib/rjr/common.rb', line 74

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