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
data/examples/layouts.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# :stopdoc:
|
2
|
+
#
|
3
|
+
# The formatting of log messages is controlled by the layout given to the
|
4
|
+
# appender. By default all appenders use the Basic layout. It's pretty
|
5
|
+
# basic. However, a more sophisticated Pattern layout can be used or one of
|
6
|
+
# the Parseable layouts -- JSON or YAML.
|
7
|
+
#
|
8
|
+
# The available layouts are:
|
9
|
+
#
|
10
|
+
# Logging.layouts.basic
|
11
|
+
# Logging.layouts.pattern
|
12
|
+
# Logging.layouts.json
|
13
|
+
# Logging.layouts.yaml
|
14
|
+
#
|
15
|
+
# In this example we'll demonstrate use of different layouts and setting log
|
16
|
+
# levels in the appenders to filter out events.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'logging'
|
20
|
+
|
21
|
+
# only show "info" or higher messages on STDOUT using the Basic layout
|
22
|
+
Logging.appenders.stdout(:level => :info)
|
23
|
+
|
24
|
+
# send all log events to the development log (including debug) as JSON
|
25
|
+
Logging.appenders.rolling_file(
|
26
|
+
'development.log',
|
27
|
+
:age => 'daily',
|
28
|
+
:layout => Logging.layouts.json
|
29
|
+
)
|
30
|
+
|
31
|
+
# send growl notifications for errors and fatals using a nice pattern
|
32
|
+
Logging.appenders.growl(
|
33
|
+
'growl',
|
34
|
+
:level => :error,
|
35
|
+
:layout => Logging.layouts.pattern(:pattern => '[%d] %-5l: %m\n')
|
36
|
+
)
|
37
|
+
|
38
|
+
log = Logging.logger['Foo::Bar']
|
39
|
+
log.add_appenders 'stdout', 'development.log', 'growl'
|
40
|
+
log.level = :debug
|
41
|
+
|
42
|
+
log.debug "a very nice little debug message"
|
43
|
+
log.info "things are operating nominally"
|
44
|
+
log.warn "this is your last warning"
|
45
|
+
log.error StandardError.new("something went horribly wrong")
|
46
|
+
log.fatal "I Die!"
|
47
|
+
|
48
|
+
# :startdoc:
|
data/examples/loggers.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# :stopdoc:
|
2
|
+
#
|
3
|
+
# Multiple loggers can be created and each can be configured with it's own
|
4
|
+
# log level and appenders. So one logger can be configured to output debug
|
5
|
+
# messages, and all the others can be left at the info or warn level. This
|
6
|
+
# makes it easier to debug specific portions of your code.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'logging'
|
10
|
+
|
11
|
+
# all loggers inherit the log level of the "root" logger
|
12
|
+
# but specific loggers can be given their own level
|
13
|
+
Logging.logger.root.level = :warn
|
14
|
+
|
15
|
+
# similarly, the root appender will be used by all loggers
|
16
|
+
Logging.logger.root.appenders = Logging.appenders.file('output.log')
|
17
|
+
|
18
|
+
log1 = Logging.logger['Log1']
|
19
|
+
log2 = Logging.logger['Log2']
|
20
|
+
log3 = Logging.logger['Log3']
|
21
|
+
|
22
|
+
# you can use strings or symbols to set the log level
|
23
|
+
log3.level = 'debug'
|
24
|
+
|
25
|
+
log1.info "this message will not get logged"
|
26
|
+
log2.info "nor will this message"
|
27
|
+
log3.info "but this message will get logged"
|
28
|
+
|
29
|
+
# :startdoc:
|
data/examples/names.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# :stopdoc:
|
2
|
+
#
|
3
|
+
# Loggers and appenders can be looked up by name. The bracket notation is
|
4
|
+
# used to find these objects:
|
5
|
+
#
|
6
|
+
# Logging.logger['foo']
|
7
|
+
# Logging.appenders['bar']
|
8
|
+
#
|
9
|
+
# A logger will be created if a new name is used. Appenders are different;
|
10
|
+
# nil is returned when an unknown appender name is used. The reason for this
|
11
|
+
# is that appenders come in many different flavors (so it is unclear which
|
12
|
+
# type should be created), but there is only one type of logger.
|
13
|
+
#
|
14
|
+
# So it is useful to be able to create an appender and then reference it by
|
15
|
+
# name to add it to multiple loggers. When the same name is used, the same
|
16
|
+
# object will be returned by the bracket methods.
|
17
|
+
#
|
18
|
+
# Layouts do not have names. Some are stateful, and none are threadsafe. So
|
19
|
+
# each appender is configured with it's own layout.
|
20
|
+
#
|
21
|
+
|
22
|
+
require 'logging'
|
23
|
+
|
24
|
+
Logging.appenders.file('Debug File', :filename => 'debug.log')
|
25
|
+
Logging.appenders.growl('Growl Notifier', :level => :error)
|
26
|
+
|
27
|
+
# configure the root logger
|
28
|
+
Logging.logger.root.appenders = 'Debug File'
|
29
|
+
Logging.logger.root.level = :debug
|
30
|
+
|
31
|
+
# add the growl notifier to the Critical logger (it will use it's own
|
32
|
+
# appender and the root logger's appender, too)
|
33
|
+
Logging.logger['Critical'].appenders = 'Growl Notifier'
|
34
|
+
|
35
|
+
# if you'll notice above, assigning appenders using just the name is valid
|
36
|
+
# the logger is smart enough to figure out it was given a string and then
|
37
|
+
# go lookup the appender by name
|
38
|
+
|
39
|
+
# and now log some messages
|
40
|
+
Logging.logger['Critical'].info 'just keeping you informed'
|
41
|
+
Logging.logger['Critical'].fatal 'WTF!!'
|
42
|
+
|
43
|
+
# :startdoc:
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# :stopdoc:
|
2
|
+
#
|
3
|
+
# Logging provides a simple, default logger configured in the same manner as
|
4
|
+
# the default Ruby Logger class -- i.e. the output of the two will be the
|
5
|
+
# same. All log messags at "warn" or higher are printed to STDOUT; any
|
6
|
+
# message below the "warn" level are discarded.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'logging'
|
10
|
+
|
11
|
+
log = Logging.logger(STDOUT)
|
12
|
+
log.level = :warn
|
13
|
+
|
14
|
+
log.debug "this debug message will not be output by the logger"
|
15
|
+
log.warn "this is your last warning"
|
16
|
+
|
17
|
+
# :startdoc:
|
data/lib/logging.rb
ADDED
@@ -0,0 +1,528 @@
|
|
1
|
+
|
2
|
+
# Equivalent to a header guard in C/C++
|
3
|
+
# Used to prevent the class/module from being loaded more than once
|
4
|
+
unless defined? Logging
|
5
|
+
|
6
|
+
require File.expand_path(
|
7
|
+
File.join(File.dirname(__FILE__), %w[logging utils]))
|
8
|
+
|
9
|
+
require 'yaml'
|
10
|
+
require 'stringio'
|
11
|
+
require 'thread'
|
12
|
+
require 'fileutils'
|
13
|
+
require 'little-plugger'
|
14
|
+
|
15
|
+
HAVE_SYSLOG = require? 'syslog'
|
16
|
+
|
17
|
+
#
|
18
|
+
#
|
19
|
+
module Logging
|
20
|
+
extend LittlePlugger
|
21
|
+
|
22
|
+
# :stopdoc:
|
23
|
+
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
24
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
25
|
+
LEVELS = {}
|
26
|
+
LNAMES = []
|
27
|
+
module Plugins; end
|
28
|
+
# :startdoc:
|
29
|
+
|
30
|
+
class << self
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# Logging.configure( filename )
|
34
|
+
# Logging.configure { block }
|
35
|
+
#
|
36
|
+
# Configures the Logging framework using the configuration information
|
37
|
+
# found in the given file. The file extension should be either '.yaml'
|
38
|
+
# or '.yml' (XML configuration is not yet supported).
|
39
|
+
#
|
40
|
+
def configure( *args, &block )
|
41
|
+
if block
|
42
|
+
return ::Logging::Config::Configurator.process(&block)
|
43
|
+
end
|
44
|
+
|
45
|
+
filename = args.shift
|
46
|
+
raise ArgumentError, 'a filename was not given' if filename.nil?
|
47
|
+
|
48
|
+
case File.extname(filename)
|
49
|
+
when '.yaml', '.yml'
|
50
|
+
::Logging::Config::YamlConfigurator.load(filename, *args)
|
51
|
+
else raise ArgumentError, 'unknown configuration file format' end
|
52
|
+
end
|
53
|
+
|
54
|
+
# call-seq:
|
55
|
+
# Logging.logger( device, age = 7, size = 1048576 )
|
56
|
+
# Logging.logger( device, age = 'weekly' )
|
57
|
+
#
|
58
|
+
# This convenience method returns a Logger instance configured to behave
|
59
|
+
# similarly to a core Ruby Logger instance.
|
60
|
+
#
|
61
|
+
# The _device_ is the logging destination. This can be a filename
|
62
|
+
# (String) or an IO object (STDERR, STDOUT, an open File, etc.). The
|
63
|
+
# _age_ is the number of old log files to keep or the frequency of
|
64
|
+
# rotation (+daily+, +weekly+, or +monthly+). The _size_ is the maximum
|
65
|
+
# logfile size and is only used when _age_ is a number.
|
66
|
+
#
|
67
|
+
# Using the same _device_ twice will result in the same Logger instance
|
68
|
+
# being returned. For example, if a Logger is created using STDOUT then
|
69
|
+
# the same Logger instance will be returned the next time STDOUT is
|
70
|
+
# used. A new Logger instance can be obtained by closing the previous
|
71
|
+
# logger instance.
|
72
|
+
#
|
73
|
+
# log1 = Logging.logger(STDOUT)
|
74
|
+
# log2 = Logging.logger(STDOUT)
|
75
|
+
# log1.object_id == log2.object_id #=> true
|
76
|
+
#
|
77
|
+
# log1.close
|
78
|
+
# log2 = Logging.logger(STDOUT)
|
79
|
+
# log1.object_id == log2.object_id #=> false
|
80
|
+
#
|
81
|
+
# The format of the log messages can be changed using a few optional
|
82
|
+
# parameters. The <tt>:pattern</tt> can be used to change the log
|
83
|
+
# message format. The <tt>:date_pattern</tt> can be used to change how
|
84
|
+
# timestamps are formatted.
|
85
|
+
#
|
86
|
+
# log = Logging.logger(STDOUT,
|
87
|
+
# :pattern => "[%d] %-5l : %m\n",
|
88
|
+
# :date_pattern => "%Y-%m-%d %H:%M:%S.%s")
|
89
|
+
#
|
90
|
+
# See the documentation for the Logging::Layouts::Pattern class for a
|
91
|
+
# full description of the :pattern and :date_pattern formatting strings.
|
92
|
+
#
|
93
|
+
def logger( *args )
|
94
|
+
return ::Logging::Logger if args.empty?
|
95
|
+
|
96
|
+
opts = args.pop if args.last.instance_of?(Hash)
|
97
|
+
opts ||= Hash.new
|
98
|
+
|
99
|
+
dev = args.shift
|
100
|
+
keep = age = args.shift
|
101
|
+
size = args.shift
|
102
|
+
|
103
|
+
name = case dev
|
104
|
+
when String; dev
|
105
|
+
when File; dev.path
|
106
|
+
else dev.object_id.to_s end
|
107
|
+
|
108
|
+
repo = ::Logging::Repository.instance
|
109
|
+
return repo[name] if repo.has_logger? name
|
110
|
+
|
111
|
+
l_opts = {
|
112
|
+
:pattern => "%.1l, [%d #%p] %#{::Logging::MAX_LEVEL_LENGTH}l : %m\n",
|
113
|
+
:date_pattern => '%Y-%m-%dT%H:%M:%S.%s'
|
114
|
+
}
|
115
|
+
[:pattern, :date_pattern, :date_method].each do |o|
|
116
|
+
l_opts[o] = opts.delete(o) if opts.has_key? o
|
117
|
+
end
|
118
|
+
layout = ::Logging::Layouts::Pattern.new(l_opts)
|
119
|
+
|
120
|
+
a_opts = Hash.new
|
121
|
+
a_opts[:size] = size if size.instance_of?(Fixnum)
|
122
|
+
a_opts[:age] = age if age.instance_of?(String)
|
123
|
+
a_opts[:keep] = keep if keep.instance_of?(Fixnum)
|
124
|
+
a_opts[:filename] = dev if dev.instance_of?(String)
|
125
|
+
a_opts[:layout] = layout
|
126
|
+
a_opts.merge! opts
|
127
|
+
|
128
|
+
appender =
|
129
|
+
case dev
|
130
|
+
when String
|
131
|
+
::Logging::Appenders::RollingFile.new(name, a_opts)
|
132
|
+
else
|
133
|
+
::Logging::Appenders::IO.new(name, dev, a_opts)
|
134
|
+
end
|
135
|
+
|
136
|
+
logger = ::Logging::Logger.new(name)
|
137
|
+
logger.add_appenders appender
|
138
|
+
logger.additive = false
|
139
|
+
|
140
|
+
class << logger
|
141
|
+
def close
|
142
|
+
@appenders.each {|a| a.close}
|
143
|
+
h = ::Logging::Repository.instance.instance_variable_get :@h
|
144
|
+
h.delete(@name)
|
145
|
+
class << self; undef :close; end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
logger
|
150
|
+
end
|
151
|
+
|
152
|
+
# Access to the layouts.
|
153
|
+
#
|
154
|
+
def layouts
|
155
|
+
::Logging::Layouts
|
156
|
+
end
|
157
|
+
|
158
|
+
# Access to the appenders.
|
159
|
+
#
|
160
|
+
def appenders
|
161
|
+
::Logging::Appenders
|
162
|
+
end
|
163
|
+
|
164
|
+
# Reopen all appenders. This method should be called immediately after a
|
165
|
+
# fork to ensure no conflict with file descriptors and calls to fcntl or
|
166
|
+
# flock.
|
167
|
+
#
|
168
|
+
def reopen
|
169
|
+
log_internal {'re-opening all appenders'}
|
170
|
+
::Logging::Appenders.each {|appender| appender.reopen}
|
171
|
+
end
|
172
|
+
|
173
|
+
# call-seq:
|
174
|
+
# Logging.consolidate( 'First::Name', 'Second::Name', ... )
|
175
|
+
#
|
176
|
+
# Consolidate all loggers under the given namespace. All child loggers
|
177
|
+
# in the namespace will use the "consolidated" namespace logger instead
|
178
|
+
# of creating a new logger for each class or module.
|
179
|
+
#
|
180
|
+
# If the "root" logger name is passed to this method then all loggers
|
181
|
+
# will consolidate to the root logger. In other words, only the root
|
182
|
+
# logger will be created, and it will be used by all classes and moduels
|
183
|
+
# in the applicaiton.
|
184
|
+
#
|
185
|
+
# ==== Example
|
186
|
+
#
|
187
|
+
# Logging.consolidate( 'Foo' )
|
188
|
+
#
|
189
|
+
# foo = Logging.logger['Foo']
|
190
|
+
# bar = Logging.logger['Foo::Bar']
|
191
|
+
# baz = Logging.logger['Baz']
|
192
|
+
#
|
193
|
+
# foo.object_id == bar.object_id #=> true
|
194
|
+
# foo.object_id == baz.object_id #=> false
|
195
|
+
#
|
196
|
+
def consolidate( *args )
|
197
|
+
::Logging::Repository.instance.add_master(*args)
|
198
|
+
end
|
199
|
+
|
200
|
+
# call-seq:
|
201
|
+
# include Logging.globally
|
202
|
+
# include Logging.globally( :logger )
|
203
|
+
#
|
204
|
+
# Add a "logger" method to the including context. If included from
|
205
|
+
# Object or Kernel, the logger method will be available to all objects.
|
206
|
+
#
|
207
|
+
# Optionally, a method name can be given and that will be used to
|
208
|
+
# provided access to the logger:
|
209
|
+
#
|
210
|
+
# include Logging.globally( :log )
|
211
|
+
# log.info "Just using a shorter method name"
|
212
|
+
#
|
213
|
+
# If you prefer to use the shorter "log" to access the logger.
|
214
|
+
#
|
215
|
+
# ==== Example
|
216
|
+
#
|
217
|
+
# include Logging.globally
|
218
|
+
#
|
219
|
+
# class Foo
|
220
|
+
# logger.debug "Loading the Foo class"
|
221
|
+
# def initialize
|
222
|
+
# logger.info "Creating some new foo"
|
223
|
+
# end
|
224
|
+
# end
|
225
|
+
#
|
226
|
+
# logger.fatal "End of example"
|
227
|
+
#
|
228
|
+
def globally( name = :logger )
|
229
|
+
Module.new {
|
230
|
+
eval "def #{name}() @_logging_logger ||= ::Logging::Logger[self] end"
|
231
|
+
}
|
232
|
+
end
|
233
|
+
|
234
|
+
# call-seq:
|
235
|
+
# Logging.init( levels )
|
236
|
+
#
|
237
|
+
# Defines the levels available to the loggers. The _levels_ is an array
|
238
|
+
# of strings and symbols. Each element in the array is downcased and
|
239
|
+
# converted to a symbol; these symbols are used to create the logging
|
240
|
+
# methods in the loggers.
|
241
|
+
#
|
242
|
+
# The first element in the array is the lowest logging level. Setting the
|
243
|
+
# logging level to this value will enable all log messages. The last
|
244
|
+
# element in the array is the highest logging level. Setting the logging
|
245
|
+
# level to this value will disable all log messages except this highest
|
246
|
+
# level.
|
247
|
+
#
|
248
|
+
# This method should only be invoked once to configure the logging
|
249
|
+
# levels. It is automatically invoked with the default logging levels
|
250
|
+
# when the first logger is created.
|
251
|
+
#
|
252
|
+
# The levels "all" and "off" are reserved and will be ignored if passed
|
253
|
+
# to this method.
|
254
|
+
#
|
255
|
+
# Example:
|
256
|
+
#
|
257
|
+
# Logging.init :debug, :info, :warn, :error, :fatal
|
258
|
+
# log = Logging::Logger['my logger']
|
259
|
+
# log.level = :warn
|
260
|
+
# log.warn 'Danger! Danger! Will Robinson'
|
261
|
+
# log.info 'Just FYI' # => not logged
|
262
|
+
#
|
263
|
+
# or
|
264
|
+
#
|
265
|
+
# Logging.init %w(DEBUG INFO NOTICE WARNING ERR CRIT ALERT EMERG)
|
266
|
+
# log = Logging::Logger['syslog']
|
267
|
+
# log.level = :notice
|
268
|
+
# log.warning 'This is your first warning'
|
269
|
+
# log.info 'Just FYI' # => not logged
|
270
|
+
#
|
271
|
+
def init( *args )
|
272
|
+
args = %w(debug info warn error fatal) if args.empty?
|
273
|
+
|
274
|
+
args.flatten!
|
275
|
+
levels = LEVELS.clear
|
276
|
+
names = LNAMES.clear
|
277
|
+
|
278
|
+
id = 0
|
279
|
+
args.each do |lvl|
|
280
|
+
lvl = levelify lvl
|
281
|
+
unless levels.has_key?(lvl) or lvl == 'all' or lvl == 'off'
|
282
|
+
levels[lvl] = id
|
283
|
+
names[id] = lvl.upcase
|
284
|
+
id += 1
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
longest = names.inject {|x,y| (x.length > y.length) ? x : y}
|
289
|
+
longest = 'off' if longest.length < 3
|
290
|
+
module_eval "MAX_LEVEL_LENGTH = #{longest.length}", __FILE__, __LINE__
|
291
|
+
|
292
|
+
initialize_plugins
|
293
|
+
|
294
|
+
levels.keys
|
295
|
+
end
|
296
|
+
|
297
|
+
# call-seq:
|
298
|
+
# Logging.format_as( obj_format )
|
299
|
+
#
|
300
|
+
# Defines the default _obj_format_ method to use when converting objects
|
301
|
+
# into string representations for logging. _obj_format_ can be one of
|
302
|
+
# <tt>:string</tt>, <tt>:inspect</tt>, or <tt>:yaml</tt>. These
|
303
|
+
# formatting commands map to the following object methods
|
304
|
+
#
|
305
|
+
# * :string => to_s
|
306
|
+
# * :inspect => inspect
|
307
|
+
# * :yaml => to_yaml
|
308
|
+
#
|
309
|
+
# An +ArgumentError+ is raised if anything other than +:string+,
|
310
|
+
# +:inspect+, +:yaml+ is passed to this method.
|
311
|
+
#
|
312
|
+
def format_as( f )
|
313
|
+
f = f.intern if f.instance_of? String
|
314
|
+
|
315
|
+
unless [:string, :inspect, :yaml].include? f
|
316
|
+
raise ArgumentError, "unknown object format '#{f}'"
|
317
|
+
end
|
318
|
+
|
319
|
+
module_eval "OBJ_FORMAT = :#{f}", __FILE__, __LINE__
|
320
|
+
end
|
321
|
+
|
322
|
+
# call-seq:
|
323
|
+
# Logging.backtrace #=> true or false
|
324
|
+
# Logging.backtrace( value ) #=> true or false
|
325
|
+
#
|
326
|
+
# Without any arguments, returns the global exception backtrace logging
|
327
|
+
# value. When set to +true+ backtraces will be written to the logs; when
|
328
|
+
# set to +false+ backtraces will be suppressed.
|
329
|
+
#
|
330
|
+
# When an argument is given the global exception backtrace setting will
|
331
|
+
# be changed. Value values are <tt>"on"</tt>, <tt>:on<tt> and +true+ to
|
332
|
+
# turn on backtraces and <tt>"off"</tt>, <tt>:off</tt> and +false+ to
|
333
|
+
# turn off backtraces.
|
334
|
+
#
|
335
|
+
def backtrace( b = nil )
|
336
|
+
@backtrace = true unless defined? @backtrace
|
337
|
+
return @backtrace if b.nil?
|
338
|
+
|
339
|
+
@backtrace = case b
|
340
|
+
when :on, 'on', true; true
|
341
|
+
when :off, 'off', false; false
|
342
|
+
else
|
343
|
+
raise ArgumentError, "backtrace must be true or false"
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
# Returns the version string for the library.
|
348
|
+
#
|
349
|
+
def version
|
350
|
+
@version ||= File.read(path('version.txt')).strip
|
351
|
+
end
|
352
|
+
|
353
|
+
# Returns the library path for the module. If any arguments are given,
|
354
|
+
# they will be joined to the end of the libray path using
|
355
|
+
# <tt>File.join</tt>.
|
356
|
+
#
|
357
|
+
def libpath( *args, &block )
|
358
|
+
rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
359
|
+
if block
|
360
|
+
begin
|
361
|
+
$LOAD_PATH.unshift LIBPATH
|
362
|
+
rv = block.call
|
363
|
+
ensure
|
364
|
+
$LOAD_PATH.shift
|
365
|
+
end
|
366
|
+
end
|
367
|
+
return rv
|
368
|
+
end
|
369
|
+
|
370
|
+
# Returns the lpath for the module. If any arguments are given,
|
371
|
+
# they will be joined to the end of the path using
|
372
|
+
# <tt>File.join</tt>.
|
373
|
+
#
|
374
|
+
def path( *args, &block )
|
375
|
+
rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
376
|
+
if block
|
377
|
+
begin
|
378
|
+
$LOAD_PATH.unshift PATH
|
379
|
+
rv = block.call
|
380
|
+
ensure
|
381
|
+
$LOAD_PATH.shift
|
382
|
+
end
|
383
|
+
end
|
384
|
+
return rv
|
385
|
+
end
|
386
|
+
|
387
|
+
# call-seq:
|
388
|
+
# show_configuration( io = STDOUT, logger = 'root' )
|
389
|
+
#
|
390
|
+
# This method is used to show the configuration of the logging
|
391
|
+
# framework. The information is written to the given _io_ stream
|
392
|
+
# (defaulting to stdout). Normally the configuration is dumped starting
|
393
|
+
# with the root logger, but any logger name can be given.
|
394
|
+
#
|
395
|
+
# Each line contains information for a single logger and it's appenders.
|
396
|
+
# A child logger is indented two spaces from it's parent logger. Each
|
397
|
+
# line contains the logger name, level, additivity, and trace settings.
|
398
|
+
# Here is a brief example:
|
399
|
+
#
|
400
|
+
# root ........................... *info -T
|
401
|
+
# LoggerA ...................... info +A -T
|
402
|
+
# LoggerA::LoggerB ........... info +A -T
|
403
|
+
# LoggerA::LoggerC ........... *debug +A -T
|
404
|
+
# LoggerD ...................... *warn -A +T
|
405
|
+
#
|
406
|
+
# The lines can be deciphered as follows:
|
407
|
+
#
|
408
|
+
# 1) name - the name of the logger
|
409
|
+
#
|
410
|
+
# 2) level - the logger level; if it is preceeded by an
|
411
|
+
# asterisk then the level was explicitly set for that
|
412
|
+
# logger (as opposed to being inherited from the parent
|
413
|
+
# logger)
|
414
|
+
#
|
415
|
+
# 3) additivity - a "+A" shows the logger is additive, and log events
|
416
|
+
# will be passed up to the parent logger; "-A" shows
|
417
|
+
# that the logger will *not* pass log events up to the
|
418
|
+
# parent logger
|
419
|
+
#
|
420
|
+
# 4) trace - a "+T" shows that the logger will include trace
|
421
|
+
# information in generated log events (this includes
|
422
|
+
# filename and line number of the log message; "-T"
|
423
|
+
# shows that the logger does not include trace
|
424
|
+
# information in the log events)
|
425
|
+
#
|
426
|
+
# If a logger has appenders then they are listed, one per line,
|
427
|
+
# immediately below the logger. Appender lines are pre-pended with a
|
428
|
+
# single dash:
|
429
|
+
#
|
430
|
+
# root ........................... *info -T
|
431
|
+
# - <Appenders::Stdout:0x8b02a4 name="stdout">
|
432
|
+
# LoggerA ...................... info +A -T
|
433
|
+
# LoggerA::LoggerB ........... info +A -T
|
434
|
+
# LoggerA::LoggerC ........... *debug +A -T
|
435
|
+
# LoggerD ...................... *warn -A +T
|
436
|
+
# - <Appenders::Stderr:0x8b04ca name="stderr">
|
437
|
+
#
|
438
|
+
# We can see in this configuration dump that all the loggers will append
|
439
|
+
# to stdout via the Stdout appender configured in the root logger. All
|
440
|
+
# the loggers are additive, and so their generated log events will be
|
441
|
+
# passed up to the root logger.
|
442
|
+
#
|
443
|
+
# The exception in this configuration is LoggerD. Its additivity is set
|
444
|
+
# to false. It uses its own appender to send messages to stderr.
|
445
|
+
#
|
446
|
+
def show_configuration( io = STDOUT, logger = 'root', indent = 0 )
|
447
|
+
logger = ::Logging::Logger[logger] unless ::Logging::Logger === logger
|
448
|
+
|
449
|
+
logger._dump_configuration(io, indent)
|
450
|
+
|
451
|
+
indent += 2
|
452
|
+
children = ::Logging::Repository.instance.children(logger.name)
|
453
|
+
children.sort {|a,b| a.name <=> b.name}.each do |child|
|
454
|
+
::Logging.show_configuration(io, child, indent)
|
455
|
+
end
|
456
|
+
|
457
|
+
nil
|
458
|
+
end
|
459
|
+
|
460
|
+
# :stopdoc:
|
461
|
+
# Convert the given level into a connaconical form - a lowercase string.
|
462
|
+
def levelify( level )
|
463
|
+
case level
|
464
|
+
when String; level.downcase
|
465
|
+
when Symbol; level.to_s.downcase
|
466
|
+
else raise ArgumentError, "levels must be a String or Symbol" end
|
467
|
+
end
|
468
|
+
|
469
|
+
# Convert the given level into a level number.
|
470
|
+
def level_num( level )
|
471
|
+
l = levelify level
|
472
|
+
case l
|
473
|
+
when 'all'; 0
|
474
|
+
when 'off'; LEVELS.length
|
475
|
+
else begin; Integer(l); rescue ArgumentError; LEVELS[l] end end
|
476
|
+
end
|
477
|
+
|
478
|
+
# Internal logging method for use by the framework.
|
479
|
+
def log_internal( level = 1, &block )
|
480
|
+
::Logging::Logger[::Logging].__send__(levelify(LNAMES[level]), &block)
|
481
|
+
end
|
482
|
+
|
483
|
+
# Close all appenders
|
484
|
+
def shutdown
|
485
|
+
log_internal {'shutdown called - closing all appenders'}
|
486
|
+
::Logging::Appenders.each {|appender| appender.close}
|
487
|
+
end
|
488
|
+
|
489
|
+
# Reset the logging framework to it's uninitialized state
|
490
|
+
def reset
|
491
|
+
::Logging::Repository.reset
|
492
|
+
::Logging::Appenders.reset
|
493
|
+
LEVELS.clear
|
494
|
+
LNAMES.clear
|
495
|
+
remove_instance_variable :@backtrace if defined? @backtrace
|
496
|
+
remove_const :MAX_LEVEL_LENGTH if const_defined? :MAX_LEVEL_LENGTH
|
497
|
+
remove_const :OBJ_FORMAT if const_defined? :OBJ_FORMAT
|
498
|
+
end
|
499
|
+
# :startdoc:
|
500
|
+
end
|
501
|
+
end # module Logging
|
502
|
+
|
503
|
+
|
504
|
+
Logging.libpath {
|
505
|
+
require 'logging/appender'
|
506
|
+
require 'logging/layout'
|
507
|
+
require 'logging/log_event'
|
508
|
+
require 'logging/logger'
|
509
|
+
require 'logging/repository'
|
510
|
+
require 'logging/root_logger'
|
511
|
+
require 'logging/stats'
|
512
|
+
require 'logging/appenders'
|
513
|
+
require 'logging/layouts'
|
514
|
+
|
515
|
+
require 'logging/config/configurator'
|
516
|
+
require 'logging/config/yaml_configurator'
|
517
|
+
}
|
518
|
+
|
519
|
+
|
520
|
+
# This exit handler will close all the appenders that exist in the system.
|
521
|
+
# This is needed for closing IO streams and connections to the syslog server
|
522
|
+
# or e-mail servers, etc.
|
523
|
+
#
|
524
|
+
at_exit {Logging.shutdown}
|
525
|
+
|
526
|
+
end # unless defined?
|
527
|
+
|
528
|
+
# EOF
|