log4r 1.0.6
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/doc/content/contact.html +22 -0
- data/doc/content/contribute.html +21 -0
- data/doc/content/index.html +90 -0
- data/doc/content/license.html +56 -0
- data/doc/content/manual.html +449 -0
- data/doc/dev/README.developers +55 -0
- data/doc/dev/checklist +23 -0
- data/doc/dev/things-to-do +5 -0
- data/doc/images/log4r-logo.png +0 -0
- data/doc/images/logo2.png +0 -0
- data/doc/log4r.css +111 -0
- data/doc/templates/main.html +147 -0
- data/examples/README +19 -0
- data/examples/customlevels.rb +34 -0
- data/examples/fileroll.rb +40 -0
- data/examples/log4r_yaml.yaml +0 -0
- data/examples/logclient.rb +25 -0
- data/examples/logserver.rb +18 -0
- data/examples/moderate.xml +29 -0
- data/examples/moderateconfig.rb +66 -0
- data/examples/myformatter.rb +23 -0
- data/examples/outofthebox.rb +21 -0
- data/examples/rrconfig.xml +63 -0
- data/examples/rrsetup.rb +42 -0
- data/examples/simpleconfig.rb +39 -0
- data/examples/xmlconfig.rb +25 -0
- data/examples/yaml.rb +30 -0
- data/src/log4r.rb +17 -0
- data/src/log4r/base.rb +74 -0
- data/src/log4r/config.rb +9 -0
- data/src/log4r/configurator.rb +224 -0
- data/src/log4r/formatter/formatter.rb +105 -0
- data/src/log4r/formatter/patternformatter.rb +107 -0
- data/src/log4r/lib/drbloader.rb +52 -0
- data/src/log4r/lib/xmlloader.rb +24 -0
- data/src/log4r/logevent.rb +28 -0
- data/src/log4r/logger.rb +194 -0
- data/src/log4r/loggerfactory.rb +89 -0
- data/src/log4r/logserver.rb +28 -0
- data/src/log4r/outputter/consoleoutputters.rb +18 -0
- data/src/log4r/outputter/datefileoutputter.rb +110 -0
- data/src/log4r/outputter/emailoutputter.rb +116 -0
- data/src/log4r/outputter/fileoutputter.rb +49 -0
- data/src/log4r/outputter/iooutputter.rb +55 -0
- data/src/log4r/outputter/outputter.rb +132 -0
- data/src/log4r/outputter/outputterfactory.rb +59 -0
- data/src/log4r/outputter/remoteoutputter.rb +40 -0
- data/src/log4r/outputter/rollingfileoutputter.rb +126 -0
- data/src/log4r/outputter/staticoutputter.rb +30 -0
- data/src/log4r/outputter/syslogoutputter.rb +75 -0
- data/src/log4r/rdoc/configurator +243 -0
- data/src/log4r/rdoc/emailoutputter +103 -0
- data/src/log4r/rdoc/formatter +39 -0
- data/src/log4r/rdoc/log4r +89 -0
- data/src/log4r/rdoc/logger +175 -0
- data/src/log4r/rdoc/logserver +85 -0
- data/src/log4r/rdoc/outputter +108 -0
- data/src/log4r/rdoc/patternformatter +128 -0
- data/src/log4r/rdoc/syslogoutputter +29 -0
- data/src/log4r/rdoc/yamlconfigurator +20 -0
- data/src/log4r/repository.rb +65 -0
- data/src/log4r/staticlogger.rb +49 -0
- data/src/log4r/yamlconfigurator.rb +0 -0
- data/tests/README +10 -0
- data/tests/testall.rb +6 -0
- data/tests/testbase.rb +49 -0
- data/tests/testconf.xml +37 -0
- data/tests/testcustom.rb +27 -0
- data/tests/testformatter.rb +27 -0
- data/tests/testlogger.rb +196 -0
- data/tests/testoutputter.rb +111 -0
- data/tests/testpatternformatter.rb +21 -0
- data/tests/testxmlconf.rb +45 -0
- metadata +127 -0
@@ -0,0 +1,116 @@
|
|
1
|
+
# :include: ../rdoc/emailoutputter
|
2
|
+
|
3
|
+
require 'log4r/outputter/outputter'
|
4
|
+
require 'log4r/staticlogger'
|
5
|
+
require 'net/smtp'
|
6
|
+
|
7
|
+
module Log4r
|
8
|
+
|
9
|
+
class EmailOutputter < Outputter
|
10
|
+
attr_reader :server, :port, :domain, :acct, :authtype, :subject
|
11
|
+
|
12
|
+
def initialize(_name, hash={})
|
13
|
+
super(_name, hash)
|
14
|
+
validate(hash)
|
15
|
+
@buff = []
|
16
|
+
begin
|
17
|
+
Logger.log_internal {
|
18
|
+
"EmailOutputter '#{@name}' running SMTP client on #{@server}:#{@port}"
|
19
|
+
}
|
20
|
+
rescue Exception => e
|
21
|
+
Logger.log_internal(-2) {
|
22
|
+
"EmailOutputter '#{@name}' failed to start SMTP client!"
|
23
|
+
}
|
24
|
+
Logger.log_internal {e}
|
25
|
+
self.level = OFF
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# send out an email with the current buffer
|
30
|
+
def flush
|
31
|
+
synch { send_mail }
|
32
|
+
Logger.log_internal {"Flushed EmailOutputter '#{@name}'"}
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def validate(hash)
|
38
|
+
@buffsize = (hash[:buffsize] or hash['buffsize'] or 100).to_i
|
39
|
+
@formatfirst = Log4rTools.decode_bool(hash, :formatfirst, false)
|
40
|
+
decode_immediate_at(hash)
|
41
|
+
validate_smtp_params(hash)
|
42
|
+
end
|
43
|
+
|
44
|
+
def decode_immediate_at(hash)
|
45
|
+
@immediate = Hash.new
|
46
|
+
_at = (hash[:immediate_at] or hash['immediate_at'])
|
47
|
+
return if _at.nil?
|
48
|
+
Log4rTools.comma_split(_at).each {|lname|
|
49
|
+
level = LNAMES.index(lname)
|
50
|
+
if level.nil?
|
51
|
+
Logger.log_internal(-2) do
|
52
|
+
"EmailOutputter: skipping bad immediate_at level name '#{lname}'"
|
53
|
+
end
|
54
|
+
next
|
55
|
+
end
|
56
|
+
@immediate[level] = true
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
def validate_smtp_params(hash)
|
61
|
+
@from = (hash[:from] or hash['from'])
|
62
|
+
raise ArgumentError, "Must specify from address" if @from.nil?
|
63
|
+
_to = (hash[:to] or hash['to'] or "")
|
64
|
+
@to = Log4rTools.comma_split(_to)
|
65
|
+
raise ArgumentError, "Must specify recepients" if @to.empty?
|
66
|
+
@server = (hash[:server] or hash['server'] or 'localhost')
|
67
|
+
@port = (hash[:port] or hash['port'] or 25).to_i
|
68
|
+
@domain = (hash[:domain] or hash['domain'] or ENV['HOSTNAME'])
|
69
|
+
@acct = (hash[:acct] or hash['acct'])
|
70
|
+
@passwd = (hash[:passwd] or hash['passwd'])
|
71
|
+
@authtype = (hash[:authtype] or hash['authtype'] or :cram_md5).to_s.intern
|
72
|
+
@subject = (hash[:subject] or hash['subject'] or "Message of #{$0}")
|
73
|
+
@params = [@server, @port, @domain, @acct, @passwd, @authtype]
|
74
|
+
end
|
75
|
+
|
76
|
+
def canonical_log(event)
|
77
|
+
synch {
|
78
|
+
@buff.push case @formatfirst
|
79
|
+
when true then @formatter.format event
|
80
|
+
else event
|
81
|
+
end
|
82
|
+
send_mail if @buff.size >= @buffsize or @immediate[event.level]
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
def send_mail
|
87
|
+
msg =
|
88
|
+
case @formatfirst
|
89
|
+
when true then @buff.join
|
90
|
+
else @buff.collect{|e| @formatter.format e}.join
|
91
|
+
end
|
92
|
+
|
93
|
+
### build a mail header for RFC 822
|
94
|
+
rfc822msg =
|
95
|
+
"From: #{@from}\n" +
|
96
|
+
"To: #{@to}\n" +
|
97
|
+
"Subject: #{@subject}\n" +
|
98
|
+
"Date: #{Time.now.strftime( "%a, %d %b %Y %H:%M:%S %z %Z")}\n" +
|
99
|
+
"Message-Id: <#{"%.8f" % Time.now.to_f}@#{@domain}>\n\n" +
|
100
|
+
"#{msg}"
|
101
|
+
|
102
|
+
### send email
|
103
|
+
begin Net::SMTP.start(*@params) do |smtp|
|
104
|
+
smtp.sendmail(rfc822msg, @from, @to)
|
105
|
+
end
|
106
|
+
rescue Exception => e
|
107
|
+
Logger.log_internal(-2) {
|
108
|
+
"EmailOutputter '#{@name}' couldn't send email!"
|
109
|
+
}
|
110
|
+
Logger.log_internal {e}
|
111
|
+
self.level = OFF
|
112
|
+
ensure @buff.clear
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
# Version:: $Id: fileoutputter.rb,v 1.1.1.1 2004/03/19 03:31:09 fando Exp $
|
3
|
+
|
4
|
+
require "log4r/outputter/iooutputter"
|
5
|
+
require "log4r/staticlogger"
|
6
|
+
|
7
|
+
module Log4r
|
8
|
+
|
9
|
+
# Convenience wrapper for File. Additional hash arguments are:
|
10
|
+
#
|
11
|
+
# [<tt>:filename</tt>] Name of the file to log to.
|
12
|
+
# [<tt>:trunc</tt>] Truncate the file?
|
13
|
+
class FileOutputter < IOOutputter
|
14
|
+
attr_reader :trunc, :filename
|
15
|
+
|
16
|
+
def initialize(_name, hash={})
|
17
|
+
super(_name, nil, hash)
|
18
|
+
|
19
|
+
@trunc = Log4rTools.decode_bool(hash, :trunc, true)
|
20
|
+
_filename = (hash[:filename] or hash['filename'])
|
21
|
+
|
22
|
+
if _filename.class != String
|
23
|
+
raise TypeError, "Argument 'filename' must be a String", caller
|
24
|
+
end
|
25
|
+
|
26
|
+
# file validation
|
27
|
+
if FileTest.exist?( _filename )
|
28
|
+
if not FileTest.file?( _filename )
|
29
|
+
raise StandardError, "'#{_filename}' is not a regular file", caller
|
30
|
+
elsif not FileTest.writable?( _filename )
|
31
|
+
raise StandardError, "'#{_filename}' is not writable!", caller
|
32
|
+
end
|
33
|
+
else # ensure directory is writable
|
34
|
+
dir = File.dirname( _filename )
|
35
|
+
if not FileTest.writable?( dir )
|
36
|
+
raise StandardError, "'#{dir}' is not writable!"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
@filename = _filename
|
41
|
+
@out = File.new(@filename, (@trunc ? "w" : "a"))
|
42
|
+
Logger.log_internal {
|
43
|
+
"FileOutputter '#{@name}' writing to #{@filename}"
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
require "log4r/outputter/outputter"
|
3
|
+
require "log4r/staticlogger"
|
4
|
+
|
5
|
+
module Log4r
|
6
|
+
|
7
|
+
##
|
8
|
+
# IO Outputter invokes print then flush on the wrapped IO
|
9
|
+
# object. If the IO stream dies, IOOutputter sets itself to OFF
|
10
|
+
# and the system continues on its merry way.
|
11
|
+
#
|
12
|
+
# To find out why an IO stream died, create a logger named 'log4r'
|
13
|
+
# and look at the output.
|
14
|
+
|
15
|
+
class IOOutputter < Outputter
|
16
|
+
|
17
|
+
# IOOutputter needs an IO object to write to.
|
18
|
+
def initialize(_name, _out, hash={})
|
19
|
+
super(_name, hash)
|
20
|
+
@out = _out
|
21
|
+
end
|
22
|
+
|
23
|
+
def closed?
|
24
|
+
@out.closed?
|
25
|
+
end
|
26
|
+
|
27
|
+
# Close the IO and sets level to OFF
|
28
|
+
def close
|
29
|
+
@out.close unless @out.nil?
|
30
|
+
@level = OFF
|
31
|
+
OutputterFactory.create_methods(self)
|
32
|
+
Logger.log_internal {"Outputter '#{@name}' closed IO and set to OFF"}
|
33
|
+
end
|
34
|
+
|
35
|
+
#######
|
36
|
+
private
|
37
|
+
#######
|
38
|
+
|
39
|
+
# perform the write
|
40
|
+
def write(data)
|
41
|
+
begin
|
42
|
+
@out.print data
|
43
|
+
@out.flush
|
44
|
+
rescue IOError => ioe # recover from this instead of crash
|
45
|
+
Logger.log_internal {"IOError in Outputter '#{@name}'!"}
|
46
|
+
Logger.log_internal {ioe}
|
47
|
+
close
|
48
|
+
rescue NameError => ne
|
49
|
+
Logger.log_internal {"Outputter '#{@name}' IO is #{@out.class}!"}
|
50
|
+
Logger.log_internal {ne}
|
51
|
+
close
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# :include: ../rdoc/outputter
|
2
|
+
#
|
3
|
+
# == Other Info
|
4
|
+
#
|
5
|
+
# Version:: $Id: outputter.rb,v 1.1.1.1 2004/03/19 03:31:09 fando Exp $
|
6
|
+
# Author:: Leon Torres <leon@ugcs.caltech.edu>
|
7
|
+
|
8
|
+
require "thread"
|
9
|
+
|
10
|
+
require "log4r/outputter/outputterfactory"
|
11
|
+
require "log4r/formatter/formatter"
|
12
|
+
require "log4r/staticlogger"
|
13
|
+
|
14
|
+
module Log4r
|
15
|
+
|
16
|
+
class Outputter
|
17
|
+
attr_reader :name, :level, :formatter
|
18
|
+
@@outputters = Hash.new
|
19
|
+
|
20
|
+
# An Outputter needs a name. RootLogger will be loaded if not already
|
21
|
+
# done. The hash arguments are as follows:
|
22
|
+
#
|
23
|
+
# [<tt>:level</tt>] Logger level. Optional, defaults to root level
|
24
|
+
# [<tt>:formatter</tt>] A Formatter. Defaults to DefaultFormatter
|
25
|
+
|
26
|
+
def initialize(_name, hash={})
|
27
|
+
if _name.nil?
|
28
|
+
raise ArgumentError, "Bad arguments. Name and IO expected.", caller
|
29
|
+
end
|
30
|
+
@name = _name
|
31
|
+
validate_hash(hash)
|
32
|
+
@@outputters[@name] = self
|
33
|
+
end
|
34
|
+
|
35
|
+
# dynamically change the level
|
36
|
+
def level=(_level)
|
37
|
+
Log4rTools.validate_level(_level)
|
38
|
+
@level = _level
|
39
|
+
OutputterFactory.create_methods(self)
|
40
|
+
Logger.log_internal {"Outputter '#{@name}' level is #{LNAMES[_level]}"}
|
41
|
+
end
|
42
|
+
|
43
|
+
# Set the levels to log. All others will be ignored
|
44
|
+
def only_at(*levels)
|
45
|
+
raise ArgumentError, "Gimme some levels!", caller if levels.empty?
|
46
|
+
raise ArgumentError, "Can't log only_at ALL", caller if levels.include? ALL
|
47
|
+
levels.each {|level| Log4rTools.validate_level(level)}
|
48
|
+
@level = levels.sort.first
|
49
|
+
OutputterFactory.create_methods self, levels
|
50
|
+
Logger.log_internal {
|
51
|
+
"Outputter '#{@name}' writes only on " +\
|
52
|
+
levels.collect{|l| LNAMES[l]}.join(", ")
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
# Dynamically change the formatter. You can just specify a Class
|
57
|
+
# object and the formatter will invoke +new+ or +instance+
|
58
|
+
# on it as appropriate.
|
59
|
+
|
60
|
+
def formatter=(_formatter)
|
61
|
+
if _formatter.kind_of?(Formatter)
|
62
|
+
@formatter = _formatter
|
63
|
+
elsif _formatter.kind_of?(Class) and _formatter <= Formatter
|
64
|
+
if _formatter.respond_to? :instance
|
65
|
+
@formatter = _formatter.instance
|
66
|
+
else
|
67
|
+
@formatter = _formatter.new
|
68
|
+
end
|
69
|
+
else
|
70
|
+
raise TypeError, "Argument was not a Formatter!", caller
|
71
|
+
end
|
72
|
+
Logger.log_internal {"Outputter '#{@name}' using #{@formatter.class}"}
|
73
|
+
end
|
74
|
+
|
75
|
+
# Call flush to force an outputter to write out any buffered
|
76
|
+
# log events. Similar to IO#flush, so use in a similar fashion.
|
77
|
+
|
78
|
+
def flush
|
79
|
+
end
|
80
|
+
|
81
|
+
#########
|
82
|
+
protected
|
83
|
+
#########
|
84
|
+
|
85
|
+
# Validates the common hash arguments. For now, that would be
|
86
|
+
# +:level+, +:formatter+ and the string equivalents
|
87
|
+
def validate_hash(hash)
|
88
|
+
@mutex = Mutex.new
|
89
|
+
# default to root level and DefaultFormatter
|
90
|
+
if hash.empty?
|
91
|
+
self.level = Logger.root.level
|
92
|
+
@formatter = DefaultFormatter.new
|
93
|
+
return
|
94
|
+
end
|
95
|
+
self.level = (hash[:level] or hash['level'] or Logger.root.level)
|
96
|
+
self.formatter = (hash[:formatter] or hash['formatter'] or DefaultFormatter.new)
|
97
|
+
end
|
98
|
+
|
99
|
+
#######
|
100
|
+
private
|
101
|
+
#######
|
102
|
+
|
103
|
+
# This method handles all log events passed to a typical Outputter.
|
104
|
+
# Overload this to change the overall behavior of an outputter. Make
|
105
|
+
# sure that the new behavior is thread safe.
|
106
|
+
|
107
|
+
def canonical_log(logevent)
|
108
|
+
synch { write(format(logevent)) }
|
109
|
+
end
|
110
|
+
|
111
|
+
# Common method to format data. All it does is call the resident
|
112
|
+
# formatter's format method. If a different formatting behavior is
|
113
|
+
# needed, then overload this method.
|
114
|
+
|
115
|
+
def format(logevent)
|
116
|
+
# @formatter is guaranteed to be DefaultFormatter if no Formatter
|
117
|
+
# was specified
|
118
|
+
@formatter.format(logevent)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Abstract method to actually write the data to a destination.
|
122
|
+
# Custom outputters should overload this to specify how the
|
123
|
+
# formatted data should be written and to where.
|
124
|
+
|
125
|
+
def write(data)
|
126
|
+
end
|
127
|
+
|
128
|
+
def synch; @mutex.synchronize { yield } end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
# Version: $Id: outputterfactory.rb,v 1.1.1.1 2004/03/19 03:31:09 fando Exp $
|
3
|
+
|
4
|
+
require "log4r/base"
|
5
|
+
require "log4r/repository"
|
6
|
+
require "log4r/logger"
|
7
|
+
|
8
|
+
module Log4r
|
9
|
+
class Outputter
|
10
|
+
|
11
|
+
class OutputterFactory #:nodoc:
|
12
|
+
include Singleton
|
13
|
+
|
14
|
+
# handles two cases: logging above a level (no second arg specified)
|
15
|
+
# or logging a set of levels (passed into the second argument)
|
16
|
+
def self.create_methods(out, levels=nil)
|
17
|
+
Logger.root # force levels to be loaded
|
18
|
+
|
19
|
+
# first, undefine all the log levels
|
20
|
+
for mname in LNAMES
|
21
|
+
undefine_log(mname.downcase, out)
|
22
|
+
end
|
23
|
+
if not levels.nil? and levels.include? OFF
|
24
|
+
raise TypeError, "Can't log only_at OFF", caller[1..-1]
|
25
|
+
end
|
26
|
+
return out if out.level == OFF
|
27
|
+
|
28
|
+
if levels.nil? # then define the log methods for lev >= outlev
|
29
|
+
for lev in out.level...LEVELS
|
30
|
+
define_log(LNAMES[lev].downcase, lev, out)
|
31
|
+
end
|
32
|
+
else # define the logs only for assigned levels
|
33
|
+
for lev in levels
|
34
|
+
define_log(LNAMES[lev].downcase, lev, out)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
return out
|
38
|
+
end
|
39
|
+
|
40
|
+
# we need to synch the actual write/format for thread safteyness
|
41
|
+
def self.define_log(mname, level, out)
|
42
|
+
return if mname == 'off' || mname == 'all'
|
43
|
+
mstr = %-
|
44
|
+
def out.#{mname}(logevent)
|
45
|
+
canonical_log(logevent)
|
46
|
+
end
|
47
|
+
-
|
48
|
+
module_eval mstr
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.undefine_log(mname, out)
|
52
|
+
return if mname == 'off' || mname == 'all'
|
53
|
+
mstr = "def out.#{mname}(logevent); end"
|
54
|
+
module_eval mstr
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
require 'log4r/lib/drbloader'
|
3
|
+
require 'log4r/outputter/outputter'
|
4
|
+
|
5
|
+
module Log4r
|
6
|
+
# See log4r/logserver.rb
|
7
|
+
class RemoteOutputter < Outputter
|
8
|
+
|
9
|
+
def initialize(_name, hash={})
|
10
|
+
super(_name, hash)
|
11
|
+
@uri = (hash[:uri] or hash['uri'])
|
12
|
+
@buffsize = (hash[:buffsize] or hash['buffsize'] or 1).to_i
|
13
|
+
@buff = []
|
14
|
+
connect
|
15
|
+
end
|
16
|
+
|
17
|
+
if HAVE_ROMP
|
18
|
+
include ROMPClient
|
19
|
+
else
|
20
|
+
def initialize(*args)
|
21
|
+
raise RuntimeError, "LogServer not supported. ROMP is required", caller
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
# Call flush to send any remaining LogEvents to the remote server.
|
27
|
+
def flush
|
28
|
+
synch { send_buffer }
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def canonical_log(logevent)
|
34
|
+
synch {
|
35
|
+
@buff.push logevent
|
36
|
+
send_buffer if @buff.size >= @buffsize
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|