Class: Treequel::ColorLogFormatter

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

Overview

A ANSI-colorized formatter for Logger instances.

Constant Summary collapse

LEVEL_FORMATS =

Color settings

{
	:debug => colorize( :bold, :black ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s {%6$s} -- %7$s\n"},
	:info  => colorize( :normal ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"},
	:warn  => colorize( :bold, :yellow ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"},
	:error => colorize( :red ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"},
	:fatal => colorize( :bold, :red, :on_white ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"},
}

Constants included from ANSIColorUtilities

ANSIColorUtilities::ANSI_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ANSIColorUtilities

ansi_code, colorize

Constructor Details

#initialize(logger, settings = {}) ⇒ ColorLogFormatter

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



87
88
89
90
91
92
93
94
# File 'lib/treequel/utils.rb', line 87

def initialize( logger, settings={} ) # :notnew:
	settings = LEVEL_FORMATS.merge( settings )

	@logger   = logger
	@settings = settings

	super()
end

Instance Attribute Details

#loggerObject

The Logger object associated with the formatter



101
102
103
# File 'lib/treequel/utils.rb', line 101

def logger
  @logger
end

#settingsObject

The formats, by level



104
105
106
# File 'lib/treequel/utils.rb', line 104

def settings
  @settings
end

Instance Method Details

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

Log using the format associated with the severity



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/treequel/utils.rb', line 108

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
	]

	return self.settings[ severity.downcase.to_sym ] % args
end