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/copland/logger.rb, line 100
100: def message_format=( format )
101: @message_format = format
102: return unless format
103:
104: @needs_caller_info = false
105:
106: format_string = ""
107: format_parameters = []
108:
109: @message_format_tokens = []
110: format.scan( SPECIFIER_PATTERN ) do |v|
111: format_string << v[0] if v[0].length > 0
112: opts = SPECIFIER_OPTIONS[ v[2] ]
113: format_string << "%#{v[1]}#{opts[:type]}"
114: format_parameters << opts[:value]
115: @needs_caller_info = true if v[2] =~ /[FlLM]/
116: end
117: format_string << $' if $'.length > 0
118: format_string << "\n"
119:
120: definition = "proc { |opts| #{format_string.inspect} % [ #{format_parameters.join(',')} ] }"
121: @message_formatter = eval( definition )
122: end
Extracts the unqualified name from the progname, after setting the progname.
[ show source ]
# File lib/copland/logger.rb, line 75
75: def progname=( progname )
76: super
77: @name = progname.split( /\./ ).last
78: end