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.
Methods
Constants
| SPECIFIER_OPTIONS | = | { "c" => { :type => "s", :value => "@name" }, "C" => { :type => "s", :value => "self.progname" }, "d" => { :type => "s", :value => "opts[:timestamp]" }, "F" => { :type => "s", :value => "opts[:caller_file]" }, "l" => { :type => "s", :value => "opts[:caller_info]" }, "L" => { :type => "d", :value => "opts[:caller_line]" }, "m" => { :type => "s", :value => "opts[:msg]" }, "M" => { :type => "s", :value => "opts[:caller_method]" }, "n" => { :type => "s", :value => "$/" }, "p" => { :type => "s", :value => "opts[:severity]" }, "t" => { :type => "d", :value => "Thread.current.__id__" }, "%" => { :type => "s", :value => "'%'" }, "P" => { :type => "s", :value => "opts[:progname]" }, "$" => { :type => "d", :value => "$$" } |
| The map of specifier options supported by this class. | ||
| SPECIFIER_PATTERN | = | /(.*?)%(-?\d*(?:\.\d+)?)?([cCdFlLmMnpt%$P])/ |
| The regular expression for matching specifier patterns in the format strings. | ||
Attributes
| [R] | message_format | The format string for the message (nil if the default should be used) |
| [R] | name | The brief name of this logger (derived from progname). |
Public Instance methods
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 datetime_format 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 |
[ show source ]
# 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.
[ show source ]
# 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.
[ show source ]
# 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