filterfish-logging 0.9.8
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 +176 -0
- data/Manifest.txt +54 -0
- data/README.txt +93 -0
- data/Rakefile +28 -0
- data/data/logging.yaml +63 -0
- data/lib/logging.rb +288 -0
- data/lib/logging/appender.rb +257 -0
- data/lib/logging/appenders/console.rb +43 -0
- data/lib/logging/appenders/email.rb +131 -0
- data/lib/logging/appenders/file.rb +55 -0
- data/lib/logging/appenders/growl.rb +182 -0
- data/lib/logging/appenders/io.rb +81 -0
- data/lib/logging/appenders/rolling_file.rb +293 -0
- data/lib/logging/appenders/syslog.rb +202 -0
- data/lib/logging/config/yaml_configurator.rb +197 -0
- data/lib/logging/layout.rb +103 -0
- data/lib/logging/layouts/basic.rb +35 -0
- data/lib/logging/layouts/pattern.rb +292 -0
- data/lib/logging/log_event.rb +50 -0
- data/lib/logging/logger.rb +388 -0
- data/lib/logging/repository.rb +151 -0
- data/lib/logging/root_logger.rb +60 -0
- data/lib/logging/utils.rb +44 -0
- data/tasks/ann.rake +78 -0
- data/tasks/bones.rake +21 -0
- data/tasks/gem.rake +106 -0
- data/tasks/manifest.rake +49 -0
- data/tasks/notes.rake +22 -0
- data/tasks/post_load.rake +37 -0
- data/tasks/rdoc.rake +49 -0
- data/tasks/rubyforge.rake +57 -0
- data/tasks/setup.rb +253 -0
- data/tasks/svn.rake +45 -0
- data/tasks/test.rake +38 -0
- data/test/appenders/test_console.rb +40 -0
- data/test/appenders/test_email.rb +167 -0
- data/test/appenders/test_file.rb +94 -0
- data/test/appenders/test_growl.rb +115 -0
- data/test/appenders/test_io.rb +113 -0
- data/test/appenders/test_rolling_file.rb +187 -0
- data/test/appenders/test_syslog.rb +192 -0
- data/test/benchmark.rb +88 -0
- data/test/config/test_yaml_configurator.rb +41 -0
- data/test/layouts/test_basic.rb +44 -0
- data/test/layouts/test_pattern.rb +173 -0
- data/test/setup.rb +66 -0
- data/test/test_appender.rb +162 -0
- data/test/test_layout.rb +85 -0
- data/test/test_log_event.rb +81 -0
- data/test/test_logger.rb +589 -0
- data/test/test_logging.rb +250 -0
- data/test/test_repository.rb +123 -0
- data/test/test_root_logger.rb +82 -0
- data/test/test_utils.rb +48 -0
- metadata +126 -0
@@ -0,0 +1,257 @@
|
|
1
|
+
# $Id$
|
2
|
+
|
3
|
+
require 'thread'
|
4
|
+
|
5
|
+
module Logging
|
6
|
+
|
7
|
+
# The +Appender+ class is provides methods for appending log events to a
|
8
|
+
# logging destination. The log events are formatted into strings using a
|
9
|
+
# Layout.
|
10
|
+
#
|
11
|
+
# All other Appenders inherit from this class which provides stub methods.
|
12
|
+
# Each subclass should provide a +write+ method that will write log
|
13
|
+
# messages to the logging destination.
|
14
|
+
#
|
15
|
+
# A private +sync+ method is provided for use by subclasses. It is used to
|
16
|
+
# synchronize writes to the logging destination, and can be used by
|
17
|
+
# subclasses to synchronize the closing or flushing of the logging
|
18
|
+
# destination.
|
19
|
+
#
|
20
|
+
class Appender
|
21
|
+
|
22
|
+
@appenders = Hash.new
|
23
|
+
|
24
|
+
class << self
|
25
|
+
|
26
|
+
# call-seq:
|
27
|
+
# Appender[name]
|
28
|
+
#
|
29
|
+
# Returns the appender instance stroed in the Appender hash under the
|
30
|
+
# key _name_, or +nil+ if no appender has been created using that name.
|
31
|
+
#
|
32
|
+
def []( name ) @appenders[name] end
|
33
|
+
|
34
|
+
# call-seq:
|
35
|
+
# Appender[name] = appender
|
36
|
+
#
|
37
|
+
# Stores the given _appender_ instance in the Appender hash under the
|
38
|
+
# key _name_.
|
39
|
+
#
|
40
|
+
def []=( name, val ) @appenders[name] = val end
|
41
|
+
|
42
|
+
# call-seq:
|
43
|
+
# Appenders.remove( name )
|
44
|
+
#
|
45
|
+
# Removes the appender instance stored in the Appender hash under the
|
46
|
+
# key _name_.
|
47
|
+
#
|
48
|
+
def remove( name ) @appenders.delete(name) end
|
49
|
+
|
50
|
+
# call-seq:
|
51
|
+
# Appender.stdout
|
52
|
+
#
|
53
|
+
# Returns an instance of the Stdout Appender. Unless the user explicitly
|
54
|
+
# creates a new Stdout Appender, the instance returned by this method
|
55
|
+
# will always be the same:
|
56
|
+
#
|
57
|
+
# Appender.stdout.object_id == Appender.stdout.object_id #=> true
|
58
|
+
#
|
59
|
+
def stdout( ) self['stdout'] || ::Logging::Appenders::Stdout.new end
|
60
|
+
|
61
|
+
# call-seq:
|
62
|
+
# Appender.stderr
|
63
|
+
#
|
64
|
+
# Returns an instance of the Stderr Appender. Unless the user explicitly
|
65
|
+
# creates a new Stderr Appender, the instance returned by this method
|
66
|
+
# will always be the same:
|
67
|
+
#
|
68
|
+
# Appender.stderr.object_id == Appender.stderr.object_id #=> true
|
69
|
+
#
|
70
|
+
def stderr( ) self['stderr'] || ::Logging::Appenders::Stderr.new end
|
71
|
+
|
72
|
+
end # class << self
|
73
|
+
|
74
|
+
attr_reader :name, :layout, :level
|
75
|
+
|
76
|
+
# call-seq:
|
77
|
+
# Appender.new( name )
|
78
|
+
# Appender.new( name, :layout => layout )
|
79
|
+
#
|
80
|
+
# Creates a new appender using the given name. If no Layout is specified,
|
81
|
+
# then a Basic layout will be used. Any logging header supplied by the
|
82
|
+
# layout will be written to the logging destination when the Appender is
|
83
|
+
# created.
|
84
|
+
#
|
85
|
+
def initialize( name, opts = {} )
|
86
|
+
@name = name.to_s
|
87
|
+
@closed = false
|
88
|
+
|
89
|
+
self.layout = opts.getopt(:layout, ::Logging::Layouts::Basic.new)
|
90
|
+
self.level = opts.getopt(:level)
|
91
|
+
|
92
|
+
@mutex = Mutex.new
|
93
|
+
header = @layout.header
|
94
|
+
sync {write(header)} unless header.nil? || header.empty?
|
95
|
+
|
96
|
+
::Logging::Appender[@name] = self
|
97
|
+
end
|
98
|
+
|
99
|
+
# call-seq:
|
100
|
+
# append( event )
|
101
|
+
#
|
102
|
+
# Write the given _event_ to the logging destination. The log event will
|
103
|
+
# be processed through the Layout associated with the Appender.
|
104
|
+
#
|
105
|
+
def append( event )
|
106
|
+
if @closed
|
107
|
+
raise RuntimeError,
|
108
|
+
"appender '<#{self.class.name}: #{@name}>' is closed"
|
109
|
+
end
|
110
|
+
|
111
|
+
sync {write(event)} unless @level > event.level
|
112
|
+
self
|
113
|
+
end
|
114
|
+
|
115
|
+
# call-seq:
|
116
|
+
# appender << string
|
117
|
+
#
|
118
|
+
# Write the given _string_ to the logging destination "as is" -- no
|
119
|
+
# layout formatting will be performed.
|
120
|
+
#
|
121
|
+
def <<( str )
|
122
|
+
if @closed
|
123
|
+
raise RuntimeError,
|
124
|
+
"appender '<#{self.class.name}: #{@name}>' is closed"
|
125
|
+
end
|
126
|
+
|
127
|
+
sync {write(str)} unless @level >= ::Logging::LEVELS.length
|
128
|
+
self
|
129
|
+
end
|
130
|
+
|
131
|
+
# call-seq:
|
132
|
+
# level = :all
|
133
|
+
#
|
134
|
+
# Set the level for this appender; log events below this level will be
|
135
|
+
# ignored by this appender. The level can be either a +String+, a
|
136
|
+
# +Symbol+, or a +Fixnum+. An +ArgumentError+ is raised if this is not
|
137
|
+
# the case.
|
138
|
+
#
|
139
|
+
# There are two special levels -- "all" and "off". The former will
|
140
|
+
# enable recording of all log events. The latter will disable the
|
141
|
+
# recording of all events.
|
142
|
+
#
|
143
|
+
# Example:
|
144
|
+
#
|
145
|
+
# appender.level = :debug
|
146
|
+
# appender.level = "INFO"
|
147
|
+
# appender.level = 4
|
148
|
+
# appender.level = 'off'
|
149
|
+
# appender.level = :all
|
150
|
+
#
|
151
|
+
# These prodcue an +ArgumentError+
|
152
|
+
#
|
153
|
+
# appender.level = Object
|
154
|
+
# appender.level = -1
|
155
|
+
# appender.level = 1_000_000_000_000
|
156
|
+
#
|
157
|
+
def level=( level )
|
158
|
+
lvl = case level
|
159
|
+
when String, Symbol; ::Logging::level_num(level)
|
160
|
+
when Fixnum; level
|
161
|
+
when nil; 0
|
162
|
+
else
|
163
|
+
raise ArgumentError,
|
164
|
+
"level must be a String, Symbol, or Integer"
|
165
|
+
end
|
166
|
+
if lvl.nil? or lvl < 0 or lvl > ::Logging::LEVELS.length
|
167
|
+
raise ArgumentError, "unknown level was given '#{level}'"
|
168
|
+
end
|
169
|
+
|
170
|
+
@level = lvl
|
171
|
+
end
|
172
|
+
|
173
|
+
# call-seq
|
174
|
+
# appender.layout = Logging::Layouts::Basic.new
|
175
|
+
#
|
176
|
+
# Sets the layout to be used by this appender.
|
177
|
+
#
|
178
|
+
def layout=( layout )
|
179
|
+
unless layout.kind_of? ::Logging::Layout
|
180
|
+
raise TypeError,
|
181
|
+
"#{layout.inspect} is not a kind of 'Logging::Layout'"
|
182
|
+
end
|
183
|
+
@layout = layout
|
184
|
+
end
|
185
|
+
|
186
|
+
# call-seq:
|
187
|
+
# close( footer = true )
|
188
|
+
#
|
189
|
+
# Close the appender and writes the layout footer to the logging
|
190
|
+
# destination if the _footer_ flag is set to +true+. Log events will
|
191
|
+
# no longer be written to the logging destination after the appender
|
192
|
+
# is closed.
|
193
|
+
#
|
194
|
+
def close( footer = true )
|
195
|
+
return self if @closed
|
196
|
+
::Logging::Appender.remove(@name)
|
197
|
+
@closed = true
|
198
|
+
if footer
|
199
|
+
footer = @layout.footer
|
200
|
+
sync {write(footer)} unless footer.nil? || footer.empty?
|
201
|
+
end
|
202
|
+
self
|
203
|
+
end
|
204
|
+
|
205
|
+
# call-seq:
|
206
|
+
# closed?
|
207
|
+
#
|
208
|
+
# Returns +true+ if the appender has been closed; returns +false+
|
209
|
+
# otherwise. When an appender is closed, no more log events can be
|
210
|
+
# written to the logging destination.
|
211
|
+
#
|
212
|
+
def closed?
|
213
|
+
@closed
|
214
|
+
end
|
215
|
+
|
216
|
+
# call-seq:
|
217
|
+
# flush
|
218
|
+
#
|
219
|
+
# Call +flush+ to force an appender to write out any buffered log events.
|
220
|
+
# Similar to IO#flush, so use in a similar fashion.
|
221
|
+
#
|
222
|
+
def flush
|
223
|
+
self
|
224
|
+
end
|
225
|
+
|
226
|
+
|
227
|
+
private
|
228
|
+
|
229
|
+
# call-seq:
|
230
|
+
# write( event )
|
231
|
+
#
|
232
|
+
# Writes the given _event_ to the logging destination. Subclasses should
|
233
|
+
# provide an implementation of this method. The _event_ can be either a
|
234
|
+
# LogEvent or a String. If a LogEvent, then it will be formatted using
|
235
|
+
# the layout given to the appender when it was created.
|
236
|
+
#
|
237
|
+
def write( event )
|
238
|
+
nil
|
239
|
+
end
|
240
|
+
|
241
|
+
# call-seq:
|
242
|
+
# sync { block }
|
243
|
+
#
|
244
|
+
# Obtains an exclusive lock, runs the block, and releases the lock when
|
245
|
+
# the block completes. This method is re-entrant so that a single thread
|
246
|
+
# can call +sync+ multiple times without hanging the thread.
|
247
|
+
#
|
248
|
+
def sync
|
249
|
+
@mutex.synchronize {yield}
|
250
|
+
end
|
251
|
+
|
252
|
+
end # class Appender
|
253
|
+
end # module Logging
|
254
|
+
|
255
|
+
Logging.require_all_libs_relative_to(__FILE__, 'appenders')
|
256
|
+
|
257
|
+
# EOF
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# $Id$
|
2
|
+
|
3
|
+
require Logging.libpath(*%w[logging appenders io])
|
4
|
+
|
5
|
+
module Logging::Appenders
|
6
|
+
|
7
|
+
# This class provides an Appender that can write to STDOUT.
|
8
|
+
#
|
9
|
+
class Stdout < ::Logging::Appenders::IO
|
10
|
+
|
11
|
+
# call-seq:
|
12
|
+
# Stdout.new
|
13
|
+
# Stdout.new( :layout => layout )
|
14
|
+
#
|
15
|
+
# Creates a new Stdout Appender. The name 'stdout' will always be used
|
16
|
+
# for this appender.
|
17
|
+
#
|
18
|
+
def initialize( name = nil, opts = {} )
|
19
|
+
name ||= 'stdout'
|
20
|
+
super(name, STDOUT, opts)
|
21
|
+
end
|
22
|
+
end # class Stdout
|
23
|
+
|
24
|
+
# This class provides an Appender that can write to STDERR.
|
25
|
+
#
|
26
|
+
class Stderr < ::Logging::Appenders::IO
|
27
|
+
|
28
|
+
# call-seq:
|
29
|
+
# Stderr.new
|
30
|
+
# Stderr.new( :layout => layout )
|
31
|
+
#
|
32
|
+
# Creates a new Stderr Appender. The name 'stderr' will always be used
|
33
|
+
# for this appender.
|
34
|
+
#
|
35
|
+
def initialize( name = nil, opts = {} )
|
36
|
+
name ||= 'stderr'
|
37
|
+
super(name, STDERR, opts)
|
38
|
+
end
|
39
|
+
end # class Stderr
|
40
|
+
|
41
|
+
end # module Logging::Appenders
|
42
|
+
|
43
|
+
# EOF
|
@@ -0,0 +1,131 @@
|
|
1
|
+
# $Id$
|
2
|
+
|
3
|
+
require 'net/smtp'
|
4
|
+
require 'time' # get rfc822 time format
|
5
|
+
|
6
|
+
# a replacement EmailOutputter. This is essentially the default EmailOutptter from Log4r but with the following
|
7
|
+
# changes:
|
8
|
+
# 1) if there is data to send in an email, then do not send anything
|
9
|
+
# 2) connect to the smtp server at the last minute, do not connect at startup and then send later on.
|
10
|
+
# 3) Fix the To: field so that it looks alright.
|
11
|
+
module Logging::Appenders
|
12
|
+
|
13
|
+
class Email < ::Logging::Appender
|
14
|
+
|
15
|
+
attr_reader :server, :port, :domain, :acct, :authtype, :subject
|
16
|
+
|
17
|
+
def initialize( name, opts = {} )
|
18
|
+
super(name, opts)
|
19
|
+
|
20
|
+
@buff = []
|
21
|
+
@buffsize = opts.getopt :buffsize, 100, :as => Integer
|
22
|
+
|
23
|
+
# get the immediate levels -- no buffering occurs at these levels, and
|
24
|
+
# an e-mail is sent as soon as possible
|
25
|
+
@immediate = []
|
26
|
+
opts.getopt(:immediate_at, '').split(',').each do |lvl|
|
27
|
+
num = ::Logging.level_num(lvl.strip)
|
28
|
+
next if num.nil?
|
29
|
+
@immediate[num] = true
|
30
|
+
end
|
31
|
+
|
32
|
+
# get the SMTP parameters
|
33
|
+
@from = opts.getopt(:from)
|
34
|
+
raise ArgumentError, 'Must specify from address' if @from.nil?
|
35
|
+
|
36
|
+
@to = opts.getopt(:to, '').split(',')
|
37
|
+
raise ArgumentError, 'Must specify recipients' if @to.empty?
|
38
|
+
|
39
|
+
@server = opts.getopt :server, 'localhost'
|
40
|
+
@port = opts.getopt :port, 25, :as => Integer
|
41
|
+
@domain = opts.getopt(:domain, ENV['HOSTNAME']) || 'localhost.localdomain'
|
42
|
+
@acct = opts.getopt :acct
|
43
|
+
@passwd = opts.getopt :passwd
|
44
|
+
@authtype = opts.getopt :authtype, :cram_md5, :as => Symbol
|
45
|
+
@subject = opts.getopt :subject, "Message of #{$0}"
|
46
|
+
@params = [@server, @port, @domain, @acct, @passwd, @authtype]
|
47
|
+
end
|
48
|
+
|
49
|
+
# call-seq:
|
50
|
+
# flush
|
51
|
+
#
|
52
|
+
# Create and send an email containing the current message buffer.
|
53
|
+
#
|
54
|
+
def flush
|
55
|
+
sync { send_mail }
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
# call-seq:
|
60
|
+
# close( footer = true )
|
61
|
+
#
|
62
|
+
# Close the e-mail appender and then flush the message buffer. This will
|
63
|
+
# ensure that a final e-mail is sent with any remaining messages.
|
64
|
+
#
|
65
|
+
def close( footer = true )
|
66
|
+
super
|
67
|
+
flush
|
68
|
+
end
|
69
|
+
|
70
|
+
# cal-seq:
|
71
|
+
# queued_messages => integer
|
72
|
+
#
|
73
|
+
# Returns the number of messages in the buffer.
|
74
|
+
#
|
75
|
+
def queued_messages
|
76
|
+
@buff.length
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
# call-seq:
|
83
|
+
# write( event )
|
84
|
+
#
|
85
|
+
# Write the given _event_ to the e-mail message buffer. The log event will
|
86
|
+
# be processed through the Layout associated with this appender.
|
87
|
+
#
|
88
|
+
def write( event )
|
89
|
+
immediate = false
|
90
|
+
str = if event.instance_of?(::Logging::LogEvent)
|
91
|
+
immediate = @immediate[event.level]
|
92
|
+
@layout.format(event)
|
93
|
+
else
|
94
|
+
event.to_s
|
95
|
+
end
|
96
|
+
return if str.empty?
|
97
|
+
|
98
|
+
@buff << str
|
99
|
+
send_mail if @buff.length >= @buffsize || immediate
|
100
|
+
self
|
101
|
+
end
|
102
|
+
|
103
|
+
# Connect to the mail server and send out any buffered messages.
|
104
|
+
#
|
105
|
+
def send_mail
|
106
|
+
return if @buff.empty?
|
107
|
+
|
108
|
+
### build a mail header for RFC 822
|
109
|
+
rfc822msg = "From: #{@from}\n"
|
110
|
+
rfc822msg << "To: #{@to.join(",")}\n"
|
111
|
+
rfc822msg << "Subject: #{@subject}\n"
|
112
|
+
rfc822msg << "Date: #{Time.new.rfc822}\n"
|
113
|
+
rfc822msg << "Message-Id: <#{"%.8f" % Time.now.to_f}@#{@domain}>\n\n"
|
114
|
+
rfc822msg << @buff.join
|
115
|
+
|
116
|
+
### send email
|
117
|
+
begin
|
118
|
+
Net::SMTP.start(*@params) {|smtp| smtp.sendmail(rfc822msg, @from, @to)}
|
119
|
+
rescue Exception => e
|
120
|
+
self.level = :off
|
121
|
+
STDERR.puts e.message
|
122
|
+
# TODO - log that e-mail notification has been turned off
|
123
|
+
ensure
|
124
|
+
@buff.clear
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end # class Email
|
129
|
+
end # module Logging::Appenders
|
130
|
+
|
131
|
+
# EOF
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# $Id$
|
2
|
+
|
3
|
+
module Logging::Appenders
|
4
|
+
|
5
|
+
# This class provides an Appender that can write to a File.
|
6
|
+
#
|
7
|
+
class File < ::Logging::Appenders::IO
|
8
|
+
|
9
|
+
# call-seq:
|
10
|
+
# File.assert_valid_logfile( filename ) => true
|
11
|
+
#
|
12
|
+
# Asserts that the given _filename_ can be used as a log file by ensuring
|
13
|
+
# that if the file exists it is a regular file and it is writable. If
|
14
|
+
# the file does not exist, then the directory is checked to see if it is
|
15
|
+
# writable.
|
16
|
+
#
|
17
|
+
# An +ArgumentError+ is raised if any of these assertions fail.
|
18
|
+
#
|
19
|
+
def self.assert_valid_logfile( fn )
|
20
|
+
if ::File.exist?(fn)
|
21
|
+
if not ::File.file?(fn)
|
22
|
+
raise ArgumentError, "#{fn} is not a regular file"
|
23
|
+
elsif not ::File.writable?(fn)
|
24
|
+
raise ArgumentError, "#{fn} is not writeable"
|
25
|
+
end
|
26
|
+
elsif not ::File.writable?(::File.dirname(fn))
|
27
|
+
raise ArgumentError, "#{::File.dirname(fn)} is not writable"
|
28
|
+
end
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# File.new( name, :filename => 'file' )
|
34
|
+
# File.new( name, :filename => 'file', :truncate => true )
|
35
|
+
# File.new( name, :filename => 'file', :layout => layout )
|
36
|
+
#
|
37
|
+
# Creates a new File Appender that will use the given filename as the
|
38
|
+
# logging destination. If the file does not already exist it will be
|
39
|
+
# created. If the :truncate option is set to +true+ then the file will
|
40
|
+
# be truncated before writing begins; otherwise, log messages will be
|
41
|
+
# appened to the file.
|
42
|
+
#
|
43
|
+
def initialize( name, opts = {} )
|
44
|
+
@fn = opts.getopt(:filename, name)
|
45
|
+
raise ArgumentError, 'no filename was given' if @fn.nil?
|
46
|
+
self.class.assert_valid_logfile(@fn)
|
47
|
+
mode = opts.getopt(:truncate) ? 'w' : 'a'
|
48
|
+
|
49
|
+
super(name, ::File.new(@fn, mode), opts)
|
50
|
+
end
|
51
|
+
|
52
|
+
end # class FileAppender
|
53
|
+
end # module Logging::Appenders
|
54
|
+
|
55
|
+
# EOF
|