::Logger
A specialization of the standard Logger class that comes with Ruby. This provides the additional functionality of a fully-customizable message format, whereas the original only provided a customizable date format.
The map of specifier options supported by this class.
The regular expression for matching specifier patterns in the format strings.
Set the message format string to the given string. This also pre-parses the format for faster processing.
The format string is a printf-formatted string, which supports the following format specifiers:
c | the unqualified name of the logger |
C | the fully-qualified name of the logger |
d | the date/time string (as formatted by the # string) |
F | the filename of the calling routine |
l | the location of the calling routine |
L | the line number of the calling routine |
m | the message to log |
M | the name of the calling method |
n | the newline character |
p | the name of the priority (or severity) used to log this method |
t | the id of the current thread |
% | a percentage character |
P | the progname that was passed to the logger method |
$ | the current process id |
# File lib/needle/logger.rb, line 104 104: def message_format=( format ) 105: @message_format = format 106: return unless format 107: 108: @needs_caller_info = false 109: 110: format_string = "" 111: format_parameters = [] 112: 113: @message_format_tokens = [] 114: format.scan( SPECIFIER_PATTERN ) do |v| 115: format_string << v[0] if v[0].length > 0 116: opts = SPECIFIER_OPTIONS[ v[2] ] 117: format_string << "%#{v[1]}#{opts[:type]}" 118: format_parameters << opts[:value] 119: @needs_caller_info = true if v[2] =~ /[FlLM]/ 120: end 121: format_string << $' if $'.length > 0 122: format_string << "\n" 123: 124: definition = 125: "proc { |opts| #{format_string.inspect} " + 126: "% [ #{format_parameters.join(',')} ] }" 127: @message_formatter = eval( definition ) 128: end
Extracts the unqualified name from the progname, after setting the progname.
# File lib/needle/logger.rb, line 58 58: def progname=( progname ) 59: super 60: @name = progname.split( /\./ ).last 61: end
Changes the device that the given logger writes to, to be the given device. Does so in a thread-safe manner.
# File lib/needle/logger.rb, line 65 65: def write_to( device, shift_age = 0, shift_size = 1048576 ) 66: saved_critical = Thread.critical 67: Thread.critical = true 68: 69: if device 70: if device.respond_to?( :write ) && device.respond_to?( :close ) 71: @logdev = device 72: else 73: @logdev = Logger::LogDevice.new( device, 74: :shift_age => shift_age, 75: :shift_size => shift_size ) 76: end 77: end 78: 79: device 80: ensure 81: Thread.critical = saved_critical 82: end
Format the message using the given parameters. If a message format has not been given, just call the superclass’s implementation. Otherwise, process the tokens from the parsed message format.
# File lib/needle/logger.rb, line 133 133: def format_message( severity, timestamp, *args ) 134: if @message_format.nil? 135: super 136: else 137: msg, progname = args 138: 139: # check for API change in 1.8.3 and later 140: msg, progname = progname, msg if respond_to?(:formatter=) 141: 142: opts = { 143: :severity => severity, 144: :timestamp => timestamp, 145: :msg => msg, 146: :progname => progname 147: } 148: 149: if @needs_caller_info 150: stack = caller 151: stack.shift while stack.first =~ /\blogger\.rb/ 152: opts[:caller_info] = caller_info = stack.first 153: match = caller_info.match( /(.*):(\d+)(?::in `(.*)')?/ ) 154: opts[:caller_file] = match[1] 155: opts[:caller_line] = match[2] 156: opts[:caller_method] = match[3] 157: end 158: 159: @message_formatter.call( opts ) 160: end 161: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.