log4rails 1.1.11
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.
- checksums.yaml +7 -0
- data/LICENSE.bsd +12 -0
- data/README.md +37 -0
- data/lib/log4r/GDC.rb +41 -0
- data/lib/log4r/MDC.rb +59 -0
- data/lib/log4r/NDC.rb +86 -0
- data/lib/log4r/base.rb +74 -0
- data/lib/log4r/config.rb +9 -0
- data/lib/log4r/configurator.rb +224 -0
- data/lib/log4r/formatter/formatter.rb +105 -0
- data/lib/log4r/formatter/log4jxmlformatter.rb +65 -0
- data/lib/log4r/formatter/patternformatter.rb +145 -0
- data/lib/log4r/lib/drbloader.rb +52 -0
- data/lib/log4r/lib/xmlloader.rb +24 -0
- data/lib/log4r/log4r-rails.yaml +60 -0
- data/lib/log4r/logevent.rb +28 -0
- data/lib/log4r/logger.rb +206 -0
- data/lib/log4r/loggerfactory.rb +89 -0
- data/lib/log4r/logserver.rb +28 -0
- data/lib/log4r/outputter/bufferedsyslogoutputter.rb +47 -0
- data/lib/log4r/outputter/consoleoutputters.rb +18 -0
- data/lib/log4r/outputter/datefileoutputter.rb +117 -0
- data/lib/log4r/outputter/emailoutputter.rb +143 -0
- data/lib/log4r/outputter/fileoutputter.rb +57 -0
- data/lib/log4r/outputter/iooutputter.rb +55 -0
- data/lib/log4r/outputter/outputter.rb +134 -0
- data/lib/log4r/outputter/outputterfactory.rb +60 -0
- data/lib/log4r/outputter/remoteoutputter.rb +40 -0
- data/lib/log4r/outputter/rollingfileoutputter.rb +234 -0
- data/lib/log4r/outputter/scribeoutputter.rb +37 -0
- data/lib/log4r/outputter/staticoutputter.rb +30 -0
- data/lib/log4r/outputter/syslogoutputter.rb +126 -0
- data/lib/log4r/outputter/udpoutputter.rb +53 -0
- data/lib/log4r/railtie.rb +211 -0
- data/lib/log4r/rdoc/GDC +14 -0
- data/lib/log4r/rdoc/MDC +16 -0
- data/lib/log4r/rdoc/NDC +41 -0
- data/lib/log4r/rdoc/configurator +243 -0
- data/lib/log4r/rdoc/emailoutputter +103 -0
- data/lib/log4r/rdoc/formatter +39 -0
- data/lib/log4r/rdoc/log4jxmlformatter +21 -0
- data/lib/log4r/rdoc/log4r +89 -0
- data/lib/log4r/rdoc/logger +175 -0
- data/lib/log4r/rdoc/logserver +85 -0
- data/lib/log4r/rdoc/outputter +108 -0
- data/lib/log4r/rdoc/patternformatter +128 -0
- data/lib/log4r/rdoc/scribeoutputter +16 -0
- data/lib/log4r/rdoc/syslogoutputter +29 -0
- data/lib/log4r/rdoc/win32eventoutputter +7 -0
- data/lib/log4r/rdoc/yamlconfigurator +20 -0
- data/lib/log4r/repository.rb +88 -0
- data/lib/log4r/staticlogger.rb +49 -0
- data/lib/log4r/version.rb +4 -0
- data/lib/log4r/yamlconfigurator.rb +198 -0
- data/lib/log4rails.rb +22 -0
- metadata +97 -0
@@ -0,0 +1,105 @@
|
|
1
|
+
# :include: ../rdoc/formatter
|
2
|
+
#
|
3
|
+
# Version:: $Id$
|
4
|
+
|
5
|
+
require "singleton"
|
6
|
+
|
7
|
+
require "log4r/base"
|
8
|
+
|
9
|
+
module Log4r
|
10
|
+
|
11
|
+
# Formatter is an abstract class and a null object
|
12
|
+
class Formatter
|
13
|
+
def initialize(hash={})
|
14
|
+
end
|
15
|
+
# Define this method in a subclass to format data.
|
16
|
+
def format(logevent)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# SimpleFormatter produces output like this:
|
21
|
+
#
|
22
|
+
# WARN loggername> Danger, Will Robinson, danger!
|
23
|
+
#
|
24
|
+
# Does not write traces and does not inspect objects.
|
25
|
+
|
26
|
+
class SimpleFormatter < Formatter
|
27
|
+
def format(event)
|
28
|
+
sprintf("%*s %s> %s\n", MaxLevelLength, LNAMES[event.level],
|
29
|
+
event.name, event.data)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# BasicFormatter produces output like this:
|
34
|
+
#
|
35
|
+
# WARN loggername: I dropped my Wookie!
|
36
|
+
#
|
37
|
+
# Or like this if trace is on:
|
38
|
+
#
|
39
|
+
# WARN loggername(file.rb at 12): Hot potato!
|
40
|
+
#
|
41
|
+
# Also, it will pretty-print any Exception it gets and
|
42
|
+
# +inspect+ everything else.
|
43
|
+
#
|
44
|
+
# Hash arguments include:
|
45
|
+
#
|
46
|
+
# +depth+:: How many lines of the stacktrace to display.
|
47
|
+
|
48
|
+
class BasicFormatter < SimpleFormatter
|
49
|
+
@@basicformat = "%*s %s"
|
50
|
+
|
51
|
+
def initialize(hash={})
|
52
|
+
@depth = (hash[:depth] or hash['depth'] or 7).to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
def format(event)
|
56
|
+
buff = sprintf(@@basicformat, MaxLevelLength, LNAMES[event.level],
|
57
|
+
event.name)
|
58
|
+
buff << (event.tracer.nil? ? "" : "(#{event.tracer[0]})") + ": "
|
59
|
+
buff << format_object(event.data) + "\n"
|
60
|
+
buff
|
61
|
+
end
|
62
|
+
|
63
|
+
# Formats data according to its class:
|
64
|
+
#
|
65
|
+
# String:: Prints it out as normal.
|
66
|
+
# Exception:: Produces output similar to command-line exceptions.
|
67
|
+
# Object:: Prints the type of object, then the output of
|
68
|
+
# +inspect+. An example -- Array: [1, 2, 3]
|
69
|
+
|
70
|
+
def format_object(obj)
|
71
|
+
if obj.kind_of? Exception
|
72
|
+
return "Caught #{obj.class}: #{obj.message}\n\t" +
|
73
|
+
(obj.backtrace.nil? ? [] : obj.backtrace[0...@depth]).join("\n\t")
|
74
|
+
elsif obj.kind_of? String
|
75
|
+
return obj
|
76
|
+
else # inspect the object
|
77
|
+
return "#{obj.class}: #{obj.inspect}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Formats objects the same way irb does:
|
83
|
+
#
|
84
|
+
# loggername:foo.rb in 12>
|
85
|
+
# [1, 3, 4]
|
86
|
+
# loggername:foo.rb in 13>
|
87
|
+
# {1=>"1"}
|
88
|
+
#
|
89
|
+
# Strings don't get inspected. just printed. The trace is optional.
|
90
|
+
|
91
|
+
class ObjectFormatter < Formatter
|
92
|
+
def format(event)
|
93
|
+
buff = event.logger.name
|
94
|
+
buff << (event.tracer.nil? ? "" : ":#{event.tracer[0]}") + ">\n"
|
95
|
+
buff << (event.data.kind_of?(String) ? event.data : event.data.inspect)
|
96
|
+
buff << "\n"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Outputters that don't define a Formatter will get this, which
|
101
|
+
# is currently BasicFormatter
|
102
|
+
class DefaultFormatter < BasicFormatter
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# :include: ../rdoc/log4jxmlformatter
|
2
|
+
#
|
3
|
+
# == Other Info
|
4
|
+
#
|
5
|
+
# Version:: $Id$
|
6
|
+
|
7
|
+
require "log4r/formatter/formatter"
|
8
|
+
|
9
|
+
require "rubygems"
|
10
|
+
begin
|
11
|
+
require "builder"
|
12
|
+
rescue LoadError
|
13
|
+
puts "builder gem is required to use log4jxmlformatter, i.e. gem install builder"
|
14
|
+
end
|
15
|
+
|
16
|
+
module Log4r
|
17
|
+
|
18
|
+
class Log4jXmlFormatter < BasicFormatter
|
19
|
+
|
20
|
+
def format(logevent)
|
21
|
+
logger = logevent.fullname.gsub('::', '.')
|
22
|
+
timestamp = (Time.now.to_f * 1000).to_i
|
23
|
+
level = LNAMES[logevent.level]
|
24
|
+
message = format_object(logevent.data)
|
25
|
+
exception = message if logevent.data.kind_of? Exception
|
26
|
+
file, line, method = parse_caller(logevent.tracer[0]) if logevent.tracer
|
27
|
+
|
28
|
+
builder = Builder::XmlMarkup.new
|
29
|
+
xml = builder.log4j :event, :logger => logger,
|
30
|
+
:timestamp => timestamp,
|
31
|
+
:level => level,
|
32
|
+
:thread => '' do |e|
|
33
|
+
e.log4j :NDC, NDC.get
|
34
|
+
e.log4j :message, message
|
35
|
+
e.log4j :throwable, exception if exception
|
36
|
+
e.log4j :locationInfo, :class => '',
|
37
|
+
:method => method,
|
38
|
+
:file => file,
|
39
|
+
:line => line
|
40
|
+
e.log4j :properties do |p|
|
41
|
+
MDC.get_context.each do |key, value|
|
42
|
+
p.log4j :data, :name => key, :value => value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
xml
|
47
|
+
end
|
48
|
+
|
49
|
+
#######
|
50
|
+
private
|
51
|
+
#######
|
52
|
+
|
53
|
+
def parse_caller(line)
|
54
|
+
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ line
|
55
|
+
file = Regexp.last_match[1]
|
56
|
+
line = Regexp.last_match[2].to_i
|
57
|
+
method = Regexp.last_match[3]
|
58
|
+
[file, line, method]
|
59
|
+
else
|
60
|
+
[]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# :include: ../rdoc/patternformatter
|
2
|
+
#
|
3
|
+
# == Other Info
|
4
|
+
#
|
5
|
+
# Version:: $Id$
|
6
|
+
|
7
|
+
require "log4r/formatter/formatter"
|
8
|
+
require "log4r/GDC"
|
9
|
+
require "log4r/MDC"
|
10
|
+
require "log4r/NDC"
|
11
|
+
|
12
|
+
module Log4r
|
13
|
+
# See log4r/formatter/patternformatter.rb
|
14
|
+
class PatternFormatter < BasicFormatter
|
15
|
+
|
16
|
+
# Arguments to sprintf keyed to directive letters<br>
|
17
|
+
# %c - event short name<br>
|
18
|
+
# %C - event fullname<br>
|
19
|
+
# %d - date<br>
|
20
|
+
# %g - Global Diagnostic Context (GDC)<br>
|
21
|
+
# %t - trace<br>
|
22
|
+
# %m - message<br>
|
23
|
+
# %h - thread name<br>
|
24
|
+
# %p - process ID aka PID<br>
|
25
|
+
# %M - formatted message<br>
|
26
|
+
# %l - Level in string form<br>
|
27
|
+
# %x - Nested Diagnostic Context (NDC)<br>
|
28
|
+
# %X - Mapped Diagnostic Context (MDC), syntax is "%X{key}"<br>
|
29
|
+
# %% - Insert a %<br>
|
30
|
+
DirectiveTable = {
|
31
|
+
"c" => 'event.name',
|
32
|
+
"C" => 'event.fullname',
|
33
|
+
"d" => 'format_date',
|
34
|
+
"g" => 'Log4r::GDC.get()',
|
35
|
+
"t" => '(event.tracer.nil? ? "no trace" : event.tracer[0])',
|
36
|
+
"T" => '(event.tracer.nil? ? "no trace" : event.tracer[0].split(File::SEPARATOR)[-1])',
|
37
|
+
"m" => 'event.data',
|
38
|
+
"h" => '(Thread.current[:name] or Thread.current.to_s)',
|
39
|
+
"p" => 'Process.pid.to_s',
|
40
|
+
"M" => 'format_object(event.data)',
|
41
|
+
"l" => 'LNAMES[event.level]',
|
42
|
+
"x" => 'Log4r::NDC.get()',
|
43
|
+
"X" => 'Log4r::MDC.get("DTR_REPLACE")',
|
44
|
+
"%" => '"%"'
|
45
|
+
}
|
46
|
+
|
47
|
+
# Matches the first directive encountered and the stuff around it.
|
48
|
+
#
|
49
|
+
# * $1 is the stuff before directive or "" if not applicable
|
50
|
+
# * $2 is the directive group or nil if there's none
|
51
|
+
# * $3 is the %#.# match within directive group
|
52
|
+
# * $4 is the .# match which we don't use (it's there to match properly)
|
53
|
+
# * $5 is the directive letter
|
54
|
+
# * $6 is the stuff after the directive or "" if not applicable
|
55
|
+
# * $7 is the remainder
|
56
|
+
|
57
|
+
DirectiveRegexp = /([^%]*)((%-?\d*(\.\d+)?)([cCdgtTmhpMlxX%]))?(\{.+?\})?(.*)/
|
58
|
+
|
59
|
+
# default date format
|
60
|
+
ISO8601 = "%Y-%m-%d %H:%M:%S"
|
61
|
+
|
62
|
+
attr_reader :pattern, :date_pattern, :date_method
|
63
|
+
|
64
|
+
# Accepts the following hash arguments (either a string or a symbol):
|
65
|
+
#
|
66
|
+
# [<tt>pattern</tt>] A pattern format string.
|
67
|
+
# [<tt>date_pattern</tt>] A Time#strftime format string. See the
|
68
|
+
# Ruby Time class for details.
|
69
|
+
# [+date_method+]
|
70
|
+
# As an option to date_pattern, specify which
|
71
|
+
# Time.now method to call. For
|
72
|
+
# example, +usec+ or +to_s+.
|
73
|
+
# Specify it as a String or Symbol.
|
74
|
+
#
|
75
|
+
# The default date format is ISO8601, which looks like this:
|
76
|
+
#
|
77
|
+
# yyyy-mm-dd hh:mm:ss => 2001-01-12 13:15:50
|
78
|
+
|
79
|
+
def initialize(hash={})
|
80
|
+
super(hash)
|
81
|
+
@pattern = (hash['pattern'] or hash[:pattern] or nil)
|
82
|
+
@date_pattern = (hash['date_pattern'] or hash[:date_pattern] or nil)
|
83
|
+
@date_method = (hash['date_method'] or hash[:date_method] or nil)
|
84
|
+
@date_pattern = ISO8601 if @date_pattern.nil? and @date_method.nil?
|
85
|
+
PatternFormatter.create_format_methods(self)
|
86
|
+
end
|
87
|
+
|
88
|
+
# PatternFormatter works by dynamically defining a <tt>format</tt> method
|
89
|
+
# based on the supplied pattern format. This method contains a call to
|
90
|
+
# Kernel#sptrintf with arguments containing the data requested in
|
91
|
+
# the pattern format.
|
92
|
+
#
|
93
|
+
# How is this magic accomplished? First, we visit each directive
|
94
|
+
# and change the %#.# component to %#.#s. The directive letter is then
|
95
|
+
# used to cull an appropriate entry from the DirectiveTable for the
|
96
|
+
# sprintf argument list. After assembling the method definition, we
|
97
|
+
# run module_eval on it, and voila.
|
98
|
+
|
99
|
+
def PatternFormatter.create_format_methods(pf) #:nodoc:
|
100
|
+
# first, define the format_date method
|
101
|
+
if pf.date_method
|
102
|
+
module_eval "def pf.format_date; Time.now.#{pf.date_method}; end"
|
103
|
+
else
|
104
|
+
module_eval <<-EOS
|
105
|
+
def pf.format_date
|
106
|
+
Time.now.strftime "#{pf.date_pattern}"
|
107
|
+
end
|
108
|
+
EOS
|
109
|
+
end
|
110
|
+
# and now the main format method
|
111
|
+
ebuff = "def pf.format(event)\n sprintf(\""
|
112
|
+
_pattern = pf.pattern.dup
|
113
|
+
args = [] # the args to sprintf which we'll append to ebuff lastly
|
114
|
+
while true # work on each match in turn
|
115
|
+
match = DirectiveRegexp.match _pattern
|
116
|
+
ebuff << match[1] unless match[1].empty?
|
117
|
+
break if match[2].nil?
|
118
|
+
# deal with the directive by inserting a %#.#s where %#.# is copied
|
119
|
+
# directy from the match
|
120
|
+
ebuff << match[3] + "s"
|
121
|
+
|
122
|
+
if ( match[5] == 'X' && match[6] != nil ) then
|
123
|
+
|
124
|
+
# MDC matches, need to be able to handle String, Symbol or Number
|
125
|
+
match6sub = /[\{\}\"]/
|
126
|
+
mdcmatches = match[6].match(/\{(:?)(\d*)(.*)\}/)
|
127
|
+
|
128
|
+
if ( mdcmatches[1] == "" && mdcmatches[2] == "" )
|
129
|
+
match6sub = /[\{\}]/ # don't remove surrounding "'s if String
|
130
|
+
end
|
131
|
+
|
132
|
+
args <<
|
133
|
+
DirectiveTable[match[5]].gsub("DTR_REPLACE", match[6]).gsub(match6sub,'')
|
134
|
+
else
|
135
|
+
args << DirectiveTable[match[5]] # cull the data for our argument list
|
136
|
+
end
|
137
|
+
break if match[7].empty?
|
138
|
+
_pattern = match[7]
|
139
|
+
end
|
140
|
+
ebuff << '\n", ' + args.join(', ') + ")\n"
|
141
|
+
ebuff << "end\n"
|
142
|
+
module_eval ebuff
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#:nodoc:
|
2
|
+
module Log4r
|
3
|
+
begin
|
4
|
+
require 'romp'
|
5
|
+
HAVE_ROMP = true
|
6
|
+
rescue LoadError
|
7
|
+
HAVE_ROMP = false
|
8
|
+
end
|
9
|
+
|
10
|
+
if HAVE_ROMP
|
11
|
+
|
12
|
+
module ROMPServer #:nodoc:
|
13
|
+
private
|
14
|
+
def start_server(_uri, accept)
|
15
|
+
@server = ROMP::Server.new(_uri, accept) # what if accept is nil?
|
16
|
+
@server.bind(self, "Log4r::LogServer")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ROMPClient #:nodoc:
|
21
|
+
private
|
22
|
+
def connect
|
23
|
+
begin
|
24
|
+
@client = ROMP::Client.new(@uri, false)
|
25
|
+
@remote_logger = @client.resolve("Log4r::LogServer")
|
26
|
+
rescue Exception => e
|
27
|
+
Logger.log_internal(-2) {
|
28
|
+
"RemoteOutputter '#{@name}' failed to connect to #{@uri}!"
|
29
|
+
}
|
30
|
+
Logger.log_internal {e}
|
31
|
+
self.level = OFF
|
32
|
+
end
|
33
|
+
end
|
34
|
+
# we use propagated = true
|
35
|
+
def send_buffer
|
36
|
+
begin
|
37
|
+
@buff.each {|levent|
|
38
|
+
lname = LNAMES[levent.level].downcase
|
39
|
+
@remote_logger.oneway(lname, levent, true)
|
40
|
+
}
|
41
|
+
rescue Exception => e
|
42
|
+
Logger.log_internal(-2) {"RemoteOutputter '#{@name}' can't log!"}
|
43
|
+
Logger.log_internal {e}
|
44
|
+
self.level = OFF
|
45
|
+
ensure @buff.clear
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#:nodoc:
|
2
|
+
module Log4r
|
3
|
+
begin
|
4
|
+
require 'rexml/document'
|
5
|
+
HAVE_REXML = true
|
6
|
+
rescue LoadError
|
7
|
+
HAVE_REXML = false
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
if Log4r::HAVE_REXML
|
12
|
+
module REXML #:nodoc: all
|
13
|
+
class Element
|
14
|
+
def value_of(elmt)
|
15
|
+
val = attributes[elmt]
|
16
|
+
if val.nil?
|
17
|
+
sub = elements[elmt]
|
18
|
+
val = sub.text unless sub.nil?
|
19
|
+
end
|
20
|
+
val
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# default log4r configuration for rails.
|
2
|
+
log4r_config:
|
3
|
+
loggers:
|
4
|
+
- name : 'rails'
|
5
|
+
level : DEBUG
|
6
|
+
additive : false
|
7
|
+
trace : false
|
8
|
+
outputters :
|
9
|
+
- console
|
10
|
+
|
11
|
+
- name : 'rails::db'
|
12
|
+
level : DEBUG
|
13
|
+
additive : false
|
14
|
+
trace : false
|
15
|
+
outputters :
|
16
|
+
- console
|
17
|
+
|
18
|
+
- name : 'rails::controllers'
|
19
|
+
level : DEBUG
|
20
|
+
additive : false
|
21
|
+
trace : false
|
22
|
+
outputters :
|
23
|
+
- console
|
24
|
+
|
25
|
+
- name : 'rails::params'
|
26
|
+
level : INFO
|
27
|
+
additive : false
|
28
|
+
trace : false
|
29
|
+
outputters :
|
30
|
+
- console
|
31
|
+
|
32
|
+
- name : 'rails::models'
|
33
|
+
level : DEBUG
|
34
|
+
additive : false
|
35
|
+
trace : false
|
36
|
+
outputters :
|
37
|
+
- console
|
38
|
+
|
39
|
+
- name : 'rails::cache'
|
40
|
+
level : DEBUG
|
41
|
+
additive : false
|
42
|
+
trace : false
|
43
|
+
outputters :
|
44
|
+
- console
|
45
|
+
|
46
|
+
- name : 'rails::mailers'
|
47
|
+
level : DEBUG
|
48
|
+
additive : false
|
49
|
+
trace : false
|
50
|
+
outputters :
|
51
|
+
- console
|
52
|
+
|
53
|
+
# define all outputters (incl. formatters)
|
54
|
+
outputters:
|
55
|
+
- type: StdoutOutputter
|
56
|
+
name: console
|
57
|
+
formatter:
|
58
|
+
date_pattern: '%H:%M:%S'
|
59
|
+
pattern : '%d %l: %m'
|
60
|
+
type : PatternFormatter
|