Class: Trinidad::Logging::DefaultFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/trinidad/logging.rb

Overview

A formatter that formats application file logs (e.g. production.log).

Constant Summary collapse

JDate =
Java::JavaUtil::Date

Instance Method Summary collapse

Constructor Details

#initialize(format = nil, time_zone = nil) ⇒ DefaultFormatter

Allows customizing the date format + the time zone to be used.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/trinidad/logging.rb', line 247

def initialize(format = nil, time_zone = nil)
  super()
  @format = format ? 
    Java::JavaText::SimpleDateFormat.new(format) : 
      Java::JavaText::SimpleDateFormat.new
  case time_zone
  when Java::JavaUtil::Calendar then
    @format.calendar = time_zone
  when Java::JavaUtil::TimeZone then
    @format.time_zone = time_zone
  when String then
    time_zone = Java::JavaUtil::TimeZone.getTimeZone(time_zone)
    @format.time_zone = time_zone
  when Numeric then
    time_zones = Java::JavaUtil::TimeZone.getAvailableIDs(time_zone)
    if time_zones.length > 0
      time_zone = Java::JavaUtil::TimeZone.getTimeZone(time_zones[0])
      @format.time_zone = time_zone
    end
  end if time_zone
end

Instance Method Details

#format(record) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/trinidad/logging.rb', line 271

def format(record)
  timestamp = @format.synchronized do 
    @format.format JDate.new(record.millis)
  end
  level = record.level.name
  message = formatMessage(record)

  out = "#{timestamp} #{level}: #{message}"
  out << formatThrown(record).to_s
  (lns = "\n") == out[-1, 1] ? out : out << lns
end