sgeorgi-logging 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +262 -0
- data/README.rdoc +115 -0
- data/Rakefile +32 -0
- data/data/bad_logging_1.rb +13 -0
- data/data/bad_logging_2.rb +21 -0
- data/data/logging.rb +42 -0
- data/data/logging.yaml +63 -0
- data/data/simple_logging.rb +13 -0
- data/examples/appenders.rb +47 -0
- data/examples/classes.rb +41 -0
- data/examples/consolidation.rb +83 -0
- data/examples/fork.rb +37 -0
- data/examples/formatting.rb +51 -0
- data/examples/hierarchies.rb +73 -0
- data/examples/layouts.rb +48 -0
- data/examples/loggers.rb +29 -0
- data/examples/names.rb +43 -0
- data/examples/simple.rb +17 -0
- data/lib/logging.rb +528 -0
- data/lib/logging/appender.rb +260 -0
- data/lib/logging/appenders.rb +137 -0
- data/lib/logging/appenders/buffering.rb +178 -0
- data/lib/logging/appenders/console.rb +60 -0
- data/lib/logging/appenders/email.rb +75 -0
- data/lib/logging/appenders/file.rb +75 -0
- data/lib/logging/appenders/growl.rb +197 -0
- data/lib/logging/appenders/io.rb +69 -0
- data/lib/logging/appenders/rolling_file.rb +327 -0
- data/lib/logging/appenders/string_io.rb +68 -0
- data/lib/logging/appenders/syslog.rb +210 -0
- data/lib/logging/config/configurator.rb +188 -0
- data/lib/logging/config/yaml_configurator.rb +191 -0
- data/lib/logging/layout.rb +117 -0
- data/lib/logging/layouts.rb +47 -0
- data/lib/logging/layouts/basic.rb +32 -0
- data/lib/logging/layouts/parseable.rb +211 -0
- data/lib/logging/layouts/pattern.rb +311 -0
- data/lib/logging/log_event.rb +45 -0
- data/lib/logging/logger.rb +504 -0
- data/lib/logging/repository.rb +232 -0
- data/lib/logging/root_logger.rb +61 -0
- data/lib/logging/stats.rb +278 -0
- data/lib/logging/utils.rb +201 -0
- data/lib/spec/logging_helper.rb +34 -0
- data/test/appenders/test_buffered_io.rb +176 -0
- data/test/appenders/test_console.rb +66 -0
- data/test/appenders/test_email.rb +170 -0
- data/test/appenders/test_file.rb +95 -0
- data/test/appenders/test_growl.rb +127 -0
- data/test/appenders/test_io.rb +129 -0
- data/test/appenders/test_rolling_file.rb +209 -0
- data/test/appenders/test_syslog.rb +194 -0
- data/test/benchmark.rb +86 -0
- data/test/config/test_configurator.rb +70 -0
- data/test/config/test_yaml_configurator.rb +40 -0
- data/test/layouts/test_basic.rb +42 -0
- data/test/layouts/test_json.rb +112 -0
- data/test/layouts/test_pattern.rb +198 -0
- data/test/layouts/test_yaml.rb +121 -0
- data/test/setup.rb +43 -0
- data/test/test_appender.rb +152 -0
- data/test/test_consolidate.rb +46 -0
- data/test/test_layout.rb +110 -0
- data/test/test_log_event.rb +80 -0
- data/test/test_logger.rb +699 -0
- data/test/test_logging.rb +267 -0
- data/test/test_repository.rb +158 -0
- data/test/test_root_logger.rb +81 -0
- data/test/test_stats.rb +274 -0
- data/test/test_utils.rb +116 -0
- data/version.txt +1 -0
- metadata +227 -0
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
module Logging
|
3
|
+
module Layouts
|
4
|
+
|
5
|
+
# Accessor / Factory for the Basic layout.
|
6
|
+
#
|
7
|
+
def basic( *args )
|
8
|
+
return ::Logging::Layouts::Basic if args.empty?
|
9
|
+
::Logging::Layouts::Basic.new(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Accessor / Factory for the Pattern layout.
|
13
|
+
#
|
14
|
+
def pattern( *args )
|
15
|
+
return ::Logging::Layouts::Pattern if args.empty?
|
16
|
+
::Logging::Layouts::Pattern.new(*args)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Accessor for the Parseable layout.
|
20
|
+
#
|
21
|
+
def parseable
|
22
|
+
::Logging::Layouts::Parseable
|
23
|
+
end
|
24
|
+
|
25
|
+
# Factory for the Parseable layout using JSON formatting.
|
26
|
+
#
|
27
|
+
def json( *args )
|
28
|
+
::Logging::Layouts::Parseable.json(*args)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Factory for the Parseable layout using YAML formatting.
|
32
|
+
#
|
33
|
+
def yaml( *args )
|
34
|
+
::Logging::Layouts::Parseable.yaml(*args)
|
35
|
+
end
|
36
|
+
|
37
|
+
extend self
|
38
|
+
end # module Layouts
|
39
|
+
end # module Logging
|
40
|
+
|
41
|
+
Logging.libpath {
|
42
|
+
require 'logging/layouts/basic'
|
43
|
+
require 'logging/layouts/parseable'
|
44
|
+
require 'logging/layouts/pattern'
|
45
|
+
}
|
46
|
+
|
47
|
+
# EOF
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
module Logging::Layouts
|
3
|
+
|
4
|
+
# The +Basic+ layout class provides methods for simple formatting of log
|
5
|
+
# events. The resulting string follows the format below.
|
6
|
+
#
|
7
|
+
# LEVEL LoggerName : log message
|
8
|
+
#
|
9
|
+
# _LEVEL_ is the log level of the event. _LoggerName_ is the name of the
|
10
|
+
# logger that generated the event. <em>log message</em> is the message
|
11
|
+
# or object that was passed to the logger. If multiple message or objects
|
12
|
+
# were passed to the logger then each will be printed on its own line with
|
13
|
+
# the format show above.
|
14
|
+
#
|
15
|
+
class Basic < ::Logging::Layout
|
16
|
+
|
17
|
+
# call-seq:
|
18
|
+
# format( event )
|
19
|
+
#
|
20
|
+
# Returns a string representation of the given loggging _event_. See the
|
21
|
+
# class documentation for details about the formatting used.
|
22
|
+
#
|
23
|
+
def format( event )
|
24
|
+
obj = format_obj(event.data)
|
25
|
+
sprintf("%*s %s : %s\n", ::Logging::MAX_LEVEL_LENGTH,
|
26
|
+
::Logging::LNAMES[event.level], event.logger, obj)
|
27
|
+
end
|
28
|
+
|
29
|
+
end # class Basic
|
30
|
+
end # module Logging::Layouts
|
31
|
+
|
32
|
+
# EOF
|
@@ -0,0 +1,211 @@
|
|
1
|
+
|
2
|
+
module Logging::Layouts
|
3
|
+
|
4
|
+
# This layout will produce parseable log output in either JSON or YAML
|
5
|
+
# format. This makes it much easier for machines to parse log files and
|
6
|
+
# perform analysis on those logs.
|
7
|
+
#
|
8
|
+
# The information about the log event can be configured when the layout is
|
9
|
+
# created. Any or all of the following labels can be set as the _items_ to
|
10
|
+
# log:
|
11
|
+
#
|
12
|
+
# 'logger' Used to output the name of the logger that generated the
|
13
|
+
# log event.
|
14
|
+
# 'timestamp' Used to output the timestamp of the log event.
|
15
|
+
# 'level' Used to output the level of the log event.
|
16
|
+
# 'message' Used to output the application supplied message
|
17
|
+
# associated with the log event.
|
18
|
+
# 'file' Used to output the file name where the logging request
|
19
|
+
# was issued.
|
20
|
+
# 'line' Used to output the line number where the logging request
|
21
|
+
# was issued.
|
22
|
+
# 'method' Used to output the method name where the logging request
|
23
|
+
# was issued.
|
24
|
+
# 'pid' Used to output the process ID of the currently running
|
25
|
+
# program.
|
26
|
+
# 'millis' Used to output the number of milliseconds elapsed from
|
27
|
+
# the construction of the Layout until creation of the log
|
28
|
+
# event.
|
29
|
+
# 'thread_id' Used to output the object ID of the thread that generated
|
30
|
+
# the log event.
|
31
|
+
# 'thread' Used to output the name of the thread that generated the
|
32
|
+
# log event. Name can be specified using Thread.current[:name]
|
33
|
+
# notation. Output empty string if name not specified. This
|
34
|
+
# option helps to create more human readable output for
|
35
|
+
# multithread application logs.
|
36
|
+
#
|
37
|
+
# These items are supplied to the layout as an array of strings. The items
|
38
|
+
# 'file', 'line', and 'method' will only work if the Logger generating the
|
39
|
+
# events is configured to generate tracing information. If this is not the
|
40
|
+
# case these fields will always be empty.
|
41
|
+
#
|
42
|
+
# When configured to output log events in YAML format, each log message
|
43
|
+
# will be formatted as a hash in it's own YAML document. The hash keys are
|
44
|
+
# the name of the item, and the value is what you would expect it to be.
|
45
|
+
# Therefore, for the default set of times log message would appear as
|
46
|
+
# follows:
|
47
|
+
#
|
48
|
+
# ---
|
49
|
+
# timestamp: 2009-04-17 16:15:42
|
50
|
+
# level: INFO
|
51
|
+
# logger: Foo::Bar
|
52
|
+
# message: this is a log message
|
53
|
+
# ---
|
54
|
+
# timestamp: 2009-04-17 16:15:43
|
55
|
+
# level: ERROR
|
56
|
+
# logger: Foo
|
57
|
+
# message: <RuntimeError> Oooops!!
|
58
|
+
#
|
59
|
+
# The output order of the fields is not guaranteed to be the same as the
|
60
|
+
# order specified in the _items_ list. This is because Ruby hashes are not
|
61
|
+
# ordered by default (unless your running this in Ruby 1.9).
|
62
|
+
#
|
63
|
+
# When configured to output log events in JSON format, each log message
|
64
|
+
# will be formatted as an object (in the JSON sense of the work) on it's
|
65
|
+
# own line in the log output. Therefore, to parse the output you must read
|
66
|
+
# it line by line and parse the individual objects. Taking the same
|
67
|
+
# example above the JSON output would be:
|
68
|
+
#
|
69
|
+
# {"timestamp":"2009-04-17 16:15:42","level":"INFO","logger":"Foo::Bar","message":"this is a log message"}
|
70
|
+
# {"timestamp":"2009-04-17 16:15:43","level":"ERROR","logger":"Foo","message":"<RuntimeError> Oooops!!"}
|
71
|
+
#
|
72
|
+
# The output order of the fields is guaranteed to be the same as the order
|
73
|
+
# specified in the _items_ list.
|
74
|
+
#
|
75
|
+
class Parseable < ::Logging::Layout
|
76
|
+
|
77
|
+
# :stopdoc:
|
78
|
+
# Arguments to sprintf keyed to directive letters
|
79
|
+
DIRECTIVE_TABLE = {
|
80
|
+
'logger' => 'event.logger',
|
81
|
+
'timestamp' => 'event.time.strftime(Pattern::ISO8601)',
|
82
|
+
'level' => '::Logging::LNAMES[event.level]',
|
83
|
+
'message' => 'format_obj(event.data)',
|
84
|
+
'file' => 'event.file',
|
85
|
+
'line' => 'event.line',
|
86
|
+
'method' => 'event.method',
|
87
|
+
'pid' => 'Process.pid',
|
88
|
+
'millis' => 'Integer((event.time-@created_at)*1000)',
|
89
|
+
'thread_id' => 'Thread.current.object_id',
|
90
|
+
'thread' => 'Thread.current[:name]'
|
91
|
+
}
|
92
|
+
|
93
|
+
# call-seq:
|
94
|
+
# Pattern.create_yaml_format_methods( layout )
|
95
|
+
#
|
96
|
+
# This method will create the +format+ method in the given Parseable
|
97
|
+
# _layout_ based on the configured items for the layout instance.
|
98
|
+
#
|
99
|
+
def self.create_yaml_format_method( layout )
|
100
|
+
code = "undef :format if method_defined? :format\n"
|
101
|
+
code << "def format( event )\nstr = {\n"
|
102
|
+
|
103
|
+
code << layout.items.map {|name|
|
104
|
+
"'#{name}' => #{Parseable::DIRECTIVE_TABLE[name]}"
|
105
|
+
}.join(",\n")
|
106
|
+
code << "\n}.to_yaml\nreturn str\nend\n"
|
107
|
+
|
108
|
+
(class << layout; self end).class_eval(code, __FILE__, __LINE__)
|
109
|
+
end
|
110
|
+
|
111
|
+
# call-seq:
|
112
|
+
# Pattern.create_json_format_methods( layout )
|
113
|
+
#
|
114
|
+
# This method will create the +format+ method in the given Parseable
|
115
|
+
# _layout_ based on the configured items for the layout instance.
|
116
|
+
#
|
117
|
+
def self.create_json_format_method( layout )
|
118
|
+
code = "undef :format if method_defined? :format\n"
|
119
|
+
code << "def format( event )\n\"{"
|
120
|
+
|
121
|
+
args = []
|
122
|
+
code << layout.items.map {|name|
|
123
|
+
args << "format_as_json(#{Parseable::DIRECTIVE_TABLE[name]})"
|
124
|
+
"\\\"#{name}\\\":%s"
|
125
|
+
}.join(',')
|
126
|
+
code << "}\\n\" % [#{args.join(', ')}]\nend"
|
127
|
+
|
128
|
+
(class << layout; self end).class_eval(code, __FILE__, __LINE__)
|
129
|
+
end
|
130
|
+
# :startdoc:
|
131
|
+
|
132
|
+
# call-seq:
|
133
|
+
# Parseable.json( opts )
|
134
|
+
#
|
135
|
+
# Create a new Parseable layout that outputs log events usig JSON style
|
136
|
+
# formatting. See the initializer documentation for available options.
|
137
|
+
#
|
138
|
+
def self.json( opts = {} )
|
139
|
+
opts[:style] = 'json'
|
140
|
+
new(opts)
|
141
|
+
end
|
142
|
+
|
143
|
+
# call-seq:
|
144
|
+
# Parseable.yaml( opts )
|
145
|
+
#
|
146
|
+
# Create a new Parseable layout that outputs log events usig YAML style
|
147
|
+
# formatting. See the initializer documentation for available options.
|
148
|
+
#
|
149
|
+
def self.yaml( opts = {} )
|
150
|
+
opts[:style] = 'yaml'
|
151
|
+
new(opts)
|
152
|
+
end
|
153
|
+
|
154
|
+
# call-seq:
|
155
|
+
# Parseable.new( opts )
|
156
|
+
#
|
157
|
+
# Creates a new Parseable layout using the following options:
|
158
|
+
#
|
159
|
+
# :style => :json or :yaml
|
160
|
+
# :items => %w[timestamp level logger message]
|
161
|
+
#
|
162
|
+
def initialize( opts = {} )
|
163
|
+
super
|
164
|
+
@created_at = Time.now
|
165
|
+
@style = opts.getopt(:style, 'json').to_s.intern
|
166
|
+
self.items = opts.getopt(:items, %w[timestamp level logger message])
|
167
|
+
end
|
168
|
+
|
169
|
+
attr_reader :items
|
170
|
+
|
171
|
+
# call-seq:
|
172
|
+
# layout.items = %w[timestamp level logger message]
|
173
|
+
#
|
174
|
+
# Set the log event items that will be formatted by this layout. These
|
175
|
+
# items, and only these items, will appear in the log output.
|
176
|
+
#
|
177
|
+
def items=( ary )
|
178
|
+
@items = Array(ary).map {|name| name.to_s.downcase}
|
179
|
+
valid = DIRECTIVE_TABLE.keys
|
180
|
+
@items.each do |name|
|
181
|
+
raise ArgumentError, "unknown item - #{name.inspect}" unless valid.include? name
|
182
|
+
end
|
183
|
+
create_format_method
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
private
|
188
|
+
|
189
|
+
# Take the given _value_ and format it into a JSON compatible string.
|
190
|
+
#
|
191
|
+
def format_as_json( value )
|
192
|
+
case value
|
193
|
+
when String, Integer, Float; value.inspect
|
194
|
+
when nil; 'null'
|
195
|
+
else value.to_s.inspect end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Call the appropriate class level create format method based on the
|
199
|
+
# style of this parseable layout.
|
200
|
+
#
|
201
|
+
def create_format_method
|
202
|
+
case @style
|
203
|
+
when :json; Parseable.create_json_format_method(self)
|
204
|
+
when :yaml; Parseable.create_yaml_format_method(self)
|
205
|
+
else raise ArgumentError, "unknown format style '#@style'" end
|
206
|
+
end
|
207
|
+
|
208
|
+
end # class Parseable
|
209
|
+
end # module Logging::Layouts
|
210
|
+
|
211
|
+
# EOF
|
@@ -0,0 +1,311 @@
|
|
1
|
+
|
2
|
+
module Logging::Layouts
|
3
|
+
|
4
|
+
# A flexible layout configurable with pattern string.
|
5
|
+
#
|
6
|
+
# The goal of this class is to format a LogEvent and return the results as
|
7
|
+
# a String. The results depend on the conversion pattern.
|
8
|
+
#
|
9
|
+
# The conversion pattern is closely related to the conversion pattern of
|
10
|
+
# the sprintf function. A conversion pattern is composed of literal text
|
11
|
+
# and format control expressions called conversion specifiers.
|
12
|
+
#
|
13
|
+
# You are free to insert any literal text within the conversion pattern.
|
14
|
+
#
|
15
|
+
# Each conversion specifier starts with a percent sign (%) and is followed
|
16
|
+
# by optional format modifiers and a conversion character. The conversion
|
17
|
+
# character specifies the type of data, e.g. logger, level, date, thread
|
18
|
+
# ID. The format modifiers control such things as field width, padding,
|
19
|
+
# left and right justification. The following is a simple example.
|
20
|
+
#
|
21
|
+
# Let the conversion pattern be "%-5l [%c]: %m\n" and assume that the
|
22
|
+
# logging environment was set to use a Pattern layout. Then the statements
|
23
|
+
#
|
24
|
+
# root = Logging.logger[:root]
|
25
|
+
# root.debug("Message 1")
|
26
|
+
# root.warn("Message 2")
|
27
|
+
#
|
28
|
+
# would yield the output
|
29
|
+
#
|
30
|
+
# DEBUG [root]: Message 1
|
31
|
+
# WARN [root]: Message 2
|
32
|
+
#
|
33
|
+
# Note that there is no explicit separator between text and conversion
|
34
|
+
# specifiers. The pattern parser knows when it has reached the end of a
|
35
|
+
# conversion specifier when it reads a conversion character. In the example
|
36
|
+
# above the conversion specifier %-5l means the level of the logging event
|
37
|
+
# should be left justified to a width of five characters. The recognized
|
38
|
+
# conversion characters are
|
39
|
+
#
|
40
|
+
# [c] Used to output the name of the logger that generated the log
|
41
|
+
# event. Supports an optional "precision" described further below.
|
42
|
+
# [d] Used to output the date of the log event. The format of the
|
43
|
+
# date is specified using the :date_pattern option when the Layout
|
44
|
+
# is created. ISO8601 format is assumed if not date pattern is given.
|
45
|
+
# [F] Used to output the file name where the logging request was issued.
|
46
|
+
# [l] Used to output the level of the log event.
|
47
|
+
# [L] Used to output the line number where the logging request was
|
48
|
+
# issued.
|
49
|
+
# [m] Used to output the application supplied message associated with
|
50
|
+
# the log event.
|
51
|
+
# [M] Used to output the method name where the logging request was
|
52
|
+
# issued.
|
53
|
+
# [p] Used to output the process ID of the currently running program.
|
54
|
+
# [r] Used to output the number of milliseconds elapsed from the
|
55
|
+
# construction of the Layout until creation of the log event.
|
56
|
+
# [t] Used to output the object ID of the thread that generated the
|
57
|
+
# log event.
|
58
|
+
# [T] Used to output the name of the thread that generated the log event.
|
59
|
+
# Name can be specified using Thread.current[:name] notation. Output
|
60
|
+
# empty string if name not specified. This option helps to create
|
61
|
+
# more human readable output for multithread application logs.
|
62
|
+
# [%] The sequence '%%' outputs a single percent sign.
|
63
|
+
#
|
64
|
+
# The logger name directive 'c' accepts an optional precision that will
|
65
|
+
# only print the rightmost number of namespace identifiers for the logger.
|
66
|
+
# By default the logger name is printed in full. For example, for the
|
67
|
+
# logger name "Foo::Bar::Baz" the pattern %c{2} will output "Bar::Baz".
|
68
|
+
#
|
69
|
+
# The directives F, L, and M will only work if the Logger generating the
|
70
|
+
# events is configured to generate tracing information. If this is not
|
71
|
+
# the case these fields will always be empty.
|
72
|
+
#
|
73
|
+
# By default the relevant information is output as is. However, with the
|
74
|
+
# aid of format modifiers it is possible to change the minimum field width,
|
75
|
+
# the maximum field width and justification.
|
76
|
+
#
|
77
|
+
# The optional format modifier is placed between the percent sign and the
|
78
|
+
# conversion character.
|
79
|
+
#
|
80
|
+
# The first optional format modifier is the left justification flag which
|
81
|
+
# is just the minus (-) character. Then comes the optional minimum field
|
82
|
+
# width modifier. This is a decimal constant that represents the minimum
|
83
|
+
# number of characters to output. If the data item requires fewer
|
84
|
+
# characters, it is padded on either the left or the right until the
|
85
|
+
# minimum width is reached. The default is to pad on the left (right
|
86
|
+
# justify) but you can specify right padding with the left justification
|
87
|
+
# flag. The padding character is space. If the data item is larger than the
|
88
|
+
# minimum field width, the field is expanded to accommodate the data. The
|
89
|
+
# value is never truncated.
|
90
|
+
#
|
91
|
+
# This behavior can be changed using the maximum field width modifier which
|
92
|
+
# is designated by a period followed by a decimal constant. If the data
|
93
|
+
# item is longer than the maximum field, then the extra characters are
|
94
|
+
# removed from the end of the data item.
|
95
|
+
#
|
96
|
+
# Below are various format modifier examples for the category conversion
|
97
|
+
# specifier.
|
98
|
+
#
|
99
|
+
# [%20c] Left pad with spaces if the logger name is less than 20
|
100
|
+
# characters long
|
101
|
+
# [%-20c] Right pad with spaces if the logger name is less than 20
|
102
|
+
# characters long
|
103
|
+
# [%.30c] Truncates the logger name if it is longer than 30 characters
|
104
|
+
# [%20.30c] Left pad with spaces if the logger name is shorter than
|
105
|
+
# 20 characters. However, if the logger name is longer than
|
106
|
+
# 30 characters, then truncate the name.
|
107
|
+
# [%-20.30c] Right pad with spaces if the logger name is shorter than
|
108
|
+
# 20 characters. However, if the logger name is longer than
|
109
|
+
# 30 characters, then truncate the name.
|
110
|
+
#
|
111
|
+
# Below are examples of some conversion patterns.
|
112
|
+
#
|
113
|
+
# %.1l, [%d] %5l -- %c: %m\n
|
114
|
+
#
|
115
|
+
# This is how the Logger class in the Ruby standard library formats
|
116
|
+
# messages. The main difference will be in the date format (the Pattern
|
117
|
+
# Layout uses the ISO8601 date format). Set the :date_method on the
|
118
|
+
# Pattern Layout to be 'to_s' and then the date formats will agree.
|
119
|
+
#
|
120
|
+
class Pattern < ::Logging::Layout
|
121
|
+
|
122
|
+
# :stopdoc:
|
123
|
+
|
124
|
+
# Arguments to sprintf keyed to directive letters
|
125
|
+
DIRECTIVE_TABLE = {
|
126
|
+
'c' => 'event.logger'.freeze,
|
127
|
+
'd' => 'format_date(event.time)'.freeze,
|
128
|
+
'F' => 'event.file'.freeze,
|
129
|
+
'l' => '::Logging::LNAMES[event.level]'.freeze,
|
130
|
+
'L' => 'event.line'.freeze,
|
131
|
+
'm' => 'format_obj(event.data)'.freeze,
|
132
|
+
'M' => 'event.method'.freeze,
|
133
|
+
'p' => 'Process.pid'.freeze,
|
134
|
+
'r' => 'Integer((event.time-@created_at)*1000).to_s'.freeze,
|
135
|
+
't' => 'Thread.current.object_id.to_s'.freeze,
|
136
|
+
'T' => 'Thread.current[:name]'.freeze,
|
137
|
+
'%' => :placeholder
|
138
|
+
}.freeze
|
139
|
+
|
140
|
+
# Matches the first directive encountered and the stuff around it.
|
141
|
+
#
|
142
|
+
# * $1 is the stuff before directive or "" if not applicable
|
143
|
+
# * $2 is the %#.# match within directive group
|
144
|
+
# * $3 is the directive letter
|
145
|
+
# * $4 is the precision specifier for the logger name
|
146
|
+
# * $5 is the stuff after the directive or "" if not applicable
|
147
|
+
DIRECTIVE_RGXP = %r/([^%]*)(?:(%-?\d*(?:\.\d+)?)([a-zA-Z%])(?:\{(\d+)\})?)?(.*)/m
|
148
|
+
|
149
|
+
# default date format
|
150
|
+
ISO8601 = "%Y-%m-%d %H:%M:%S".freeze
|
151
|
+
|
152
|
+
# call-seq:
|
153
|
+
# Pattern.create_date_format_methods( pf )
|
154
|
+
#
|
155
|
+
# This method will create the +date_format+ method in the given Pattern
|
156
|
+
# Layout _pf_ based on the configured date patten and/or date method
|
157
|
+
# specified by the user.
|
158
|
+
#
|
159
|
+
def self.create_date_format_methods( pf )
|
160
|
+
code = "undef :format_date if method_defined? :format_date\n"
|
161
|
+
code << "def format_date( time )\n"
|
162
|
+
if pf.date_method.nil?
|
163
|
+
if pf.date_pattern =~ %r/%s/
|
164
|
+
code << <<-CODE
|
165
|
+
dp = '#{pf.date_pattern}'.gsub('%s','%06d' % time.usec)
|
166
|
+
time.strftime dp
|
167
|
+
CODE
|
168
|
+
else
|
169
|
+
code << "time.strftime '#{pf.date_pattern}'\n"
|
170
|
+
end
|
171
|
+
else
|
172
|
+
code << "time.#{pf.date_method}\n"
|
173
|
+
end
|
174
|
+
code << "end\n"
|
175
|
+
::Logging.log_internal(0) {code}
|
176
|
+
|
177
|
+
pf._meta_eval(code, __FILE__, __LINE__)
|
178
|
+
end
|
179
|
+
|
180
|
+
# call-seq:
|
181
|
+
# Pattern.create_format_method( pf )
|
182
|
+
#
|
183
|
+
# This method will create the +format+ method in the given Pattern
|
184
|
+
# Layout _pf_ based on the configured format pattern specified by the
|
185
|
+
# user.
|
186
|
+
#
|
187
|
+
def self.create_format_method( pf )
|
188
|
+
# Create the format(event) method
|
189
|
+
code = "undef :format if method_defined? :format\n"
|
190
|
+
code << "def format( event )\nsprintf(\""
|
191
|
+
pattern = pf.pattern.dup
|
192
|
+
args = []
|
193
|
+
|
194
|
+
while true
|
195
|
+
m = DIRECTIVE_RGXP.match(pattern)
|
196
|
+
code << m[1] unless m[1].empty?
|
197
|
+
|
198
|
+
case m[3]
|
199
|
+
when '%'; code << '%%'
|
200
|
+
when 'c'
|
201
|
+
code << m[2] + 's'
|
202
|
+
args << DIRECTIVE_TABLE[m[3]].dup
|
203
|
+
if m[4]
|
204
|
+
raise ArgumentError, "logger name precision must be an integer greater than zero: #{m[4]}" unless Integer(m[4]) > 0
|
205
|
+
args.last <<
|
206
|
+
".split(::Logging::Repository::PATH_DELIMITER)" \
|
207
|
+
".last(#{m[4]}).join(::Logging::Repository::PATH_DELIMITER)"
|
208
|
+
end
|
209
|
+
when *DIRECTIVE_TABLE.keys
|
210
|
+
code << m[2] + 's'
|
211
|
+
code << "{#{m[4]}}" if m[4]
|
212
|
+
args << DIRECTIVE_TABLE[m[3]]
|
213
|
+
when nil; break
|
214
|
+
else
|
215
|
+
raise ArgumentError, "illegal format character - '#{m[3]}'"
|
216
|
+
end
|
217
|
+
|
218
|
+
break if m[5].empty?
|
219
|
+
pattern = m[5]
|
220
|
+
end
|
221
|
+
|
222
|
+
code << '"'
|
223
|
+
code << ', ' + args.join(', ') unless args.empty?
|
224
|
+
code << ")\n"
|
225
|
+
code << "end\n"
|
226
|
+
::Logging.log_internal(0) {code}
|
227
|
+
|
228
|
+
pf._meta_eval(code, __FILE__, __LINE__)
|
229
|
+
end
|
230
|
+
# :startdoc:
|
231
|
+
|
232
|
+
# call-seq:
|
233
|
+
# Pattern.new( opts )
|
234
|
+
#
|
235
|
+
# Creates a new Pattern layout using the following options.
|
236
|
+
#
|
237
|
+
# :pattern => "[%d] %-5l -- %c : %m\n"
|
238
|
+
# :date_pattern => "%Y-%m-%d %H:%M:%S"
|
239
|
+
# :date_method => 'usec' or 'to_s'
|
240
|
+
#
|
241
|
+
# If used, :date_method will supersede :date_pattern.
|
242
|
+
#
|
243
|
+
def initialize( opts = {} )
|
244
|
+
super
|
245
|
+
@created_at = Time.now
|
246
|
+
|
247
|
+
@date_pattern = opts.getopt(:date_pattern)
|
248
|
+
@date_method = opts.getopt(:date_method)
|
249
|
+
@date_pattern = ISO8601 if @date_pattern.nil? and @date_method.nil?
|
250
|
+
|
251
|
+
@pattern = opts.getopt(:pattern,
|
252
|
+
"[%d] %-#{::Logging::MAX_LEVEL_LENGTH}l -- %c : %m\n")
|
253
|
+
|
254
|
+
Pattern.create_date_format_methods(self)
|
255
|
+
Pattern.create_format_method(self)
|
256
|
+
end
|
257
|
+
|
258
|
+
attr_reader :pattern, :date_pattern, :date_method
|
259
|
+
|
260
|
+
# call-seq:
|
261
|
+
# appender.pattern = "[%d] %-5l -- %c : %m\n"
|
262
|
+
#
|
263
|
+
# Set the message formatting pattern to be used by the layout.
|
264
|
+
#
|
265
|
+
def pattern=( var )
|
266
|
+
@pattern = var
|
267
|
+
Pattern.create_format_method(self)
|
268
|
+
end
|
269
|
+
|
270
|
+
# call-seq:
|
271
|
+
# appender.date_pattern = "%Y-%m-%d %H:%M:%S"
|
272
|
+
#
|
273
|
+
# Set the date formatting pattern to be used when outputting timestamps
|
274
|
+
# in the log messages.
|
275
|
+
#
|
276
|
+
def date_pattern=( var )
|
277
|
+
@date_pattern = var
|
278
|
+
Pattern.create_date_format_methods(self)
|
279
|
+
end
|
280
|
+
|
281
|
+
# call-seq:
|
282
|
+
# appender.date_method = 'to_s'
|
283
|
+
# appender.date_method = :usec
|
284
|
+
#
|
285
|
+
# Set the date method to be used when outputting timestamps in the log
|
286
|
+
# messages. If a date method is configured, the output of that method
|
287
|
+
# will be used in leu of the date pattern.
|
288
|
+
#
|
289
|
+
def date_method=( var )
|
290
|
+
@date_method = var
|
291
|
+
Pattern.create_date_format_methods(self)
|
292
|
+
end
|
293
|
+
|
294
|
+
# :stopdoc:
|
295
|
+
|
296
|
+
# call-seq:
|
297
|
+
# _meta_eval( code )
|
298
|
+
#
|
299
|
+
# Evaluates the given string of _code_ if the singleton class of this
|
300
|
+
# Pattern Layout object.
|
301
|
+
#
|
302
|
+
def _meta_eval( code, file = nil, line = nil )
|
303
|
+
meta = class << self; self end
|
304
|
+
meta.class_eval code, file, line
|
305
|
+
end
|
306
|
+
# :startdoc:
|
307
|
+
|
308
|
+
end # class Pattern
|
309
|
+
end # module Logging::Layouts
|
310
|
+
|
311
|
+
# EOF
|