Class: Treequel::LogFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/treequel/utils.rb

Overview

A alternate formatter for Logger instances.

Constant Summary collapse

DEFAULT_FORMAT =

The format to output unless debugging is turned on

"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"
DEFAULT_DEBUG_FORMAT =

The format to output if debugging is turned on

"[%1$s.%2$06d %3$d/%4$s] %5$5s {%6$s} -- %7$s\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, format = DEFAULT_FORMAT, debug = DEFAULT_DEBUG_FORMAT) ⇒ LogFormatter

Initialize the formatter with a reference to the logger so it can check for log level.



27
28
29
30
31
32
33
# File 'lib/treequel/utils.rb', line 27

def initialize( logger, format=DEFAULT_FORMAT, debug=DEFAULT_DEBUG_FORMAT ) # :notnew:
  @logger       = logger
  @format       = format
  @debug_format = debug

  super()
end

Instance Attribute Details

#debug_formatObject

The logging format string that’s used when outputting in debug mode



46
47
48
# File 'lib/treequel/utils.rb', line 46

def debug_format
  @debug_format
end

#formatObject

The logging format string



43
44
45
# File 'lib/treequel/utils.rb', line 43

def format
  @format
end

#loggerObject

The Logger object associated with the formatter



40
41
42
# File 'lib/treequel/utils.rb', line 40

def logger
  @logger
end

Instance Method Details

#call(severity, time, progname, msg) ⇒ Object

Log using either the DEBUG_FORMAT if the associated logger is at ::DEBUG level or using FORMAT if it’s anything less verbose.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/treequel/utils.rb', line 51

def call( severity, time, progname, msg )
  args = [
    time.strftime( '%Y-%m-%d %H:%M:%S' ),                         # %1$s
    time.usec,                                                    # %2$d
    Process.pid,                                                  # %3$d
    Thread.current == Thread.main ? 'main' : Thread.object_id,    # %4$s
    severity,                                                     # %5$s
    progname,                                                     # %6$s
    msg                                                           # %7$s
  ]

  if @logger.level == Logger::DEBUG
    return self.debug_format % args
  else
    return self.format % args
  end
end