Class: Norikra::Logger
- Inherits:
-
Object
- Object
- Norikra::Logger
- Defined in:
- lib/norikra/logger.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_MEMORY_BUFFER_LINES =
1000- TIME_FORMAT =
'%Y-%m-%d %H:%M:%S %z'
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
Instance Method Summary collapse
- #debug(message, data = nil, from = nil) ⇒ Object
- #enabled?(level) ⇒ Boolean
- #error(message, data = nil, from = nil) ⇒ Object
- #fatal(message, data = nil, from = nil) ⇒ Object
- #format(from, message, data) ⇒ Object
- #format_data(data = nil) ⇒ Object
- #format_location(locations) ⇒ Object
- #info(message, data = nil, from = nil) ⇒ Object
-
#initialize(name, opts = {}) ⇒ Logger
constructor
A new instance of Logger.
- #push(level, line) ⇒ Object
- #trace(message, data = nil, from = nil) ⇒ Object
- #warn(message, data = nil, from = nil) ⇒ Object
Constructor Details
#initialize(name, opts = {}) ⇒ Logger
Returns a new instance of Logger.
174 175 176 177 178 |
# File 'lib/norikra/logger.rb', line 174 def initialize(name, opts={}) @log4j = org.apache.commons.logging.LogFactory.getLog(name) @buffer = Array.new @buffer_lines = opts[:bufferlines] || DEFAULT_MEMORY_BUFFER_LINES end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
169 170 171 |
# File 'lib/norikra/logger.rb', line 169 def buffer @buffer end |
Instance Method Details
#debug(message, data = nil, from = nil) ⇒ Object
205 206 207 208 209 210 |
# File 'lib/norikra/logger.rb', line 205 def debug(, data=nil, from=nil) return unless @log4j.isDebugEnabled line = format(from, , data) push('debug', line) @log4j.debug(line) end |
#enabled?(level) ⇒ Boolean
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/norikra/logger.rb', line 187 def enabled?(level) case level when LEVEL_TRACE then @log4j.isTraceEnabled when LEVEL_DEBUG then @log4j.isDebugEnabled when LEVEL_INFO then @log4j.isInfoEnabled when LEVEL_WARN then @log4j.isWarnEnabled when LEVEL_ERROR then @log4j.isErrorEnabled else true end end |
#error(message, data = nil, from = nil) ⇒ Object
226 227 228 229 230 231 |
# File 'lib/norikra/logger.rb', line 226 def error(, data=nil, from=nil) return unless @log4j.isErrorEnabled line = format(from, , data) push('error', line) @log4j.error(line) end |
#fatal(message, data = nil, from = nil) ⇒ Object
233 234 235 236 237 238 |
# File 'lib/norikra/logger.rb', line 233 def fatal(, data=nil, from=nil) return unless @log4j.isFatalEnabled line = format(from, , data) push('fatal', line) @log4j.fatal(line) end |
#format(from, message, data) ⇒ Object
259 260 261 262 |
# File 'lib/norikra/logger.rb', line 259 def format(from, , data) # LOG_FORMAT = '%s: %s%s' LOG_FORMAT % [format_location(from), , format_data(data)] end |
#format_data(data = nil) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/norikra/logger.rb', line 246 def format_data(data=nil) return '' if data.nil? #, status:404, message:'content not found' if data.is_a?(Proc) format_data(data.call) elsif data.is_a?(Hash) ', ' + data.map{|k,v| "#{k}:#{v.inspect}"}.join(', ') else ', ' + data.inspect end end |
#format_location(locations) ⇒ Object
240 241 242 243 244 |
# File 'lib/norikra/logger.rb', line 240 def format_location(locations) return '' if locations.nil? c = locations.first "#{c.path}:#{c.lineno}(#{c.label})" end |
#info(message, data = nil, from = nil) ⇒ Object
212 213 214 215 216 217 |
# File 'lib/norikra/logger.rb', line 212 def info(, data=nil, from=nil) return unless @log4j.isInfoEnabled line = format(from, , data) push('info', line) @log4j.info(line) end |
#push(level, line) ⇒ Object
180 181 182 183 184 185 |
# File 'lib/norikra/logger.rb', line 180 def push(level, line) if @buffer.size == @buffer_lines @buffer.shift end @buffer << [Time.now.strftime(TIME_FORMAT), level, line] end |