Class: DRbService::LogFormatter

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

Overview

A alternate formatter for Logger instances.

Usage

require 'drbservice/utils'
DRbService.logger.formatter = DRbService::LogFormatter.new( DRbService.logger )

Version

$Id: utils.rb,v 87bc5aa9e2be 2011/08/29 21:05:42 ged $

Authors

:include: LICENSE

Please see the file LICENSE in the ‘docs’ directory for licensing details.

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.



110
111
112
113
114
115
116
# File 'lib/drbservice/utils.rb', line 110

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



129
130
131
# File 'lib/drbservice/utils.rb', line 129

def debug_format
  @debug_format
end

#formatObject

The logging format string



126
127
128
# File 'lib/drbservice/utils.rb', line 126

def format
  @format
end

#loggerObject

The Logger object associated with the formatter



123
124
125
# File 'lib/drbservice/utils.rb', line 123

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.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/drbservice/utils.rb', line 134

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