Class: Commons::Logging::Impl::Log4rLogger

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/commons/logging/impl/log4r_logger.rb

Constant Summary collapse

FQCN =
self.name
CALLER_LEVEL =

The level of the caller that raises a log event. … (2)target_log_event -> (1)Log#log_method -> (0)Logger#log

2
LOG_PROPAGATED =
true

Constants included from Log

Log::DEBUG, Log::ERROR, Log::FATAL, Log::INFO, Log::TRACE, Log::WARN

Instance Method Summary collapse

Methods included from Log

#debug, #debug?, #error, #error?, #fatal, #fatal?, #info, #info?, #trace, #trace?, #warn, #warn?

Constructor Details

#initialize(param) ⇒ Log4rLogger

Returns a new instance of Log4rLogger.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/commons/logging/impl/log4r_logger.rb', line 55

def initialize(param)
  super()
  
  case param
    when String  # logger name
      @name = param
      @logger = get_logger()
    when Log4r::Logger  # logger object, this case for use with a log4r factory.
      if param == nil
        raise ArgumentError,
          'Warning - nil logger in constructor; possible log4r misconfiguration.'
      end
      @name = param.name
      @logger = param
    else
      raise ArgumentError,
        'Warning - invalid parameter; logger name or logger object is expected.'
  end
end

Instance Method Details

#enabled_for?(level) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/commons/logging/impl/log4r_logger.rb', line 139

def enabled_for?(level)
  case level
    when TRACE, DEBUG
      return get_logger.debug?
    when INFO
      return get_logger.info?
    when WARN
      return get_logger.warn?
    when ERROR
      return get_logger.error?
    when FATAL
      return get_logger.fatal?
    else
      return false
  end
end

#get_loggerObject



76
77
78
79
80
81
82
# File 'lib/commons/logging/impl/log4r_logger.rb', line 76

def get_logger
  if @logger == nil
    @logger = Log4r::Logger.get_logger(@name)
  end
  
  return @logger
end

#log(level, message, exception = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/commons/logging/impl/log4r_logger.rb', line 93

def log(level, message, exception = nil)
  message = build_message(message, exception)
  
  case level
    when TRACE, DEBUG
      get_logger.debug(
        create_log_event(Log4r::DEBUG, caller(CALLER_LEVEL), message),
        LOG_PROPAGATED)
    when INFO 
      get_logger.info(
        create_log_event(Log4r::INFO, caller(CALLER_LEVEL), message),
        LOG_PROPAGATED)
    when WARN
      get_logger.warn(
        create_log_event(Log4r::WARN, caller(CALLER_LEVEL), message),
        LOG_PROPAGATED)
    when ERROR
      get_logger.error(
        create_log_event(Log4r::ERROR, caller(CALLER_LEVEL), message),
        LOG_PROPAGATED)
    when FATAL
      get_logger.fatal(
        create_log_event(Log4r::FATAL, caller(CALLER_LEVEL), message),
        LOG_PROPAGATED)
    else
      # do nothing.
  end
end