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
- #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.
162 163 164 165 166 |
# File 'lib/norikra/logger.rb', line 162 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.
157 158 159 |
# File 'lib/norikra/logger.rb', line 157 def buffer @buffer end |
Instance Method Details
#debug(message, data = nil, from = nil) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/norikra/logger.rb', line 182 def debug(, data=nil, from=nil) return unless @log4j.isDebugEnabled line = format(from, , data) push('debug', line) @log4j.debug(line) end |
#error(message, data = nil, from = nil) ⇒ Object
203 204 205 206 207 208 |
# File 'lib/norikra/logger.rb', line 203 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
210 211 212 213 214 215 |
# File 'lib/norikra/logger.rb', line 210 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
236 237 238 239 |
# File 'lib/norikra/logger.rb', line 236 def format(from, , data) # LOG_FORMAT = '%s: %s%s' LOG_FORMAT % [format_location(from), , format_data(data)] end |
#format_data(data = nil) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/norikra/logger.rb', line 223 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
217 218 219 220 221 |
# File 'lib/norikra/logger.rb', line 217 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
189 190 191 192 193 194 |
# File 'lib/norikra/logger.rb', line 189 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
168 169 170 171 172 173 |
# File 'lib/norikra/logger.rb', line 168 def push(level, line) if @buffer.size == @buffer_lines @buffer.shift end @buffer << [Time.now.strftime(TIME_FORMAT), level, line] end |