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.
175 176 177 178 179 |
# File 'lib/norikra/logger.rb', line 175 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.
170 171 172 |
# File 'lib/norikra/logger.rb', line 170 def buffer @buffer end |
Instance Method Details
#debug(message, data = nil, from = nil) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/norikra/logger.rb', line 206 def debug(, data=nil, from=nil) return unless @log4j.isDebugEnabled line = format(from, , data) push('debug', line) @log4j.debug(line) end |
#enabled?(level) ⇒ Boolean
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/norikra/logger.rb', line 188 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
227 228 229 230 231 232 |
# File 'lib/norikra/logger.rb', line 227 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
234 235 236 237 238 239 |
# File 'lib/norikra/logger.rb', line 234 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
260 261 262 263 |
# File 'lib/norikra/logger.rb', line 260 def format(from, , data) # LOG_FORMAT = '%s: %s%s' LOG_FORMAT % [format_location(from), , format_data(data)] end |
#format_data(data = nil) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/norikra/logger.rb', line 247 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
241 242 243 244 245 |
# File 'lib/norikra/logger.rb', line 241 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
213 214 215 216 217 218 |
# File 'lib/norikra/logger.rb', line 213 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
181 182 183 184 185 186 |
# File 'lib/norikra/logger.rb', line 181 def push(level, line) if @buffer.size == @buffer_lines @buffer.shift end @buffer << [Time.now.strftime(TIME_FORMAT), level, line] end |