Class: Norikra::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/norikra/logger.rb

Direct Known Subclasses

DummyLogger

Constant Summary collapse

DEFAULT_MEMORY_BUFFER_LINES =
1000
TIME_FORMAT =
'%Y-%m-%d %H:%M:%S %z'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bufferObject (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(message, data=nil, from=nil)
  return unless @log4j.isDebugEnabled
  line = format(from, message, 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(message, data=nil, from=nil)
  return unless @log4j.isErrorEnabled
  line = format(from, message, 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(message, data=nil, from=nil)
  return unless @log4j.isFatalEnabled
  line = format(from, message, 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, message, data)
  # LOG_FORMAT = '%s: %s%s'
  LOG_FORMAT % [format_location(from), message, 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(message, data=nil, from=nil)
  return unless @log4j.isInfoEnabled
  line = format(from, message, 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

#trace(message, data = nil, from = nil) ⇒ Object



175
176
177
178
179
180
# File 'lib/norikra/logger.rb', line 175

def trace(message, data=nil, from=nil)
  return unless @log4j.isTraceEnabled
  line = format(from, message, data)
  push('trace', line)
  @log4j.trace(line)
end

#warn(message, data = nil, from = nil) ⇒ Object



196
197
198
199
200
201
# File 'lib/norikra/logger.rb', line 196

def warn(message, data=nil, from=nil)
  return unless @log4j.isWarnEnabled
  line = format(from, message, data)
  push('warn', line)
  @log4j.warn(line)
end