log4r 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/doc/content/contact.html +22 -0
  2. data/doc/content/contribute.html +21 -0
  3. data/doc/content/index.html +90 -0
  4. data/doc/content/license.html +56 -0
  5. data/doc/content/manual.html +449 -0
  6. data/doc/dev/README.developers +55 -0
  7. data/doc/dev/checklist +23 -0
  8. data/doc/dev/things-to-do +5 -0
  9. data/doc/images/log4r-logo.png +0 -0
  10. data/doc/images/logo2.png +0 -0
  11. data/doc/log4r.css +111 -0
  12. data/doc/templates/main.html +147 -0
  13. data/examples/README +19 -0
  14. data/examples/customlevels.rb +34 -0
  15. data/examples/fileroll.rb +40 -0
  16. data/examples/log4r_yaml.yaml +0 -0
  17. data/examples/logclient.rb +25 -0
  18. data/examples/logserver.rb +18 -0
  19. data/examples/moderate.xml +29 -0
  20. data/examples/moderateconfig.rb +66 -0
  21. data/examples/myformatter.rb +23 -0
  22. data/examples/outofthebox.rb +21 -0
  23. data/examples/rrconfig.xml +63 -0
  24. data/examples/rrsetup.rb +42 -0
  25. data/examples/simpleconfig.rb +39 -0
  26. data/examples/xmlconfig.rb +25 -0
  27. data/examples/yaml.rb +30 -0
  28. data/src/log4r.rb +17 -0
  29. data/src/log4r/base.rb +74 -0
  30. data/src/log4r/config.rb +9 -0
  31. data/src/log4r/configurator.rb +224 -0
  32. data/src/log4r/formatter/formatter.rb +105 -0
  33. data/src/log4r/formatter/patternformatter.rb +107 -0
  34. data/src/log4r/lib/drbloader.rb +52 -0
  35. data/src/log4r/lib/xmlloader.rb +24 -0
  36. data/src/log4r/logevent.rb +28 -0
  37. data/src/log4r/logger.rb +194 -0
  38. data/src/log4r/loggerfactory.rb +89 -0
  39. data/src/log4r/logserver.rb +28 -0
  40. data/src/log4r/outputter/consoleoutputters.rb +18 -0
  41. data/src/log4r/outputter/datefileoutputter.rb +110 -0
  42. data/src/log4r/outputter/emailoutputter.rb +116 -0
  43. data/src/log4r/outputter/fileoutputter.rb +49 -0
  44. data/src/log4r/outputter/iooutputter.rb +55 -0
  45. data/src/log4r/outputter/outputter.rb +132 -0
  46. data/src/log4r/outputter/outputterfactory.rb +59 -0
  47. data/src/log4r/outputter/remoteoutputter.rb +40 -0
  48. data/src/log4r/outputter/rollingfileoutputter.rb +126 -0
  49. data/src/log4r/outputter/staticoutputter.rb +30 -0
  50. data/src/log4r/outputter/syslogoutputter.rb +75 -0
  51. data/src/log4r/rdoc/configurator +243 -0
  52. data/src/log4r/rdoc/emailoutputter +103 -0
  53. data/src/log4r/rdoc/formatter +39 -0
  54. data/src/log4r/rdoc/log4r +89 -0
  55. data/src/log4r/rdoc/logger +175 -0
  56. data/src/log4r/rdoc/logserver +85 -0
  57. data/src/log4r/rdoc/outputter +108 -0
  58. data/src/log4r/rdoc/patternformatter +128 -0
  59. data/src/log4r/rdoc/syslogoutputter +29 -0
  60. data/src/log4r/rdoc/yamlconfigurator +20 -0
  61. data/src/log4r/repository.rb +65 -0
  62. data/src/log4r/staticlogger.rb +49 -0
  63. data/src/log4r/yamlconfigurator.rb +0 -0
  64. data/tests/README +10 -0
  65. data/tests/testall.rb +6 -0
  66. data/tests/testbase.rb +49 -0
  67. data/tests/testconf.xml +37 -0
  68. data/tests/testcustom.rb +27 -0
  69. data/tests/testformatter.rb +27 -0
  70. data/tests/testlogger.rb +196 -0
  71. data/tests/testoutputter.rb +111 -0
  72. data/tests/testpatternformatter.rb +21 -0
  73. data/tests/testxmlconf.rb +45 -0
  74. metadata +127 -0
@@ -0,0 +1,126 @@
1
+
2
+ # :nodoc:
3
+ # Version:: $Id: rollingfileoutputter.rb,v 1.1.1.1 2004/03/19 03:31:10 fando Exp $
4
+
5
+ require "log4r/outputter/fileoutputter"
6
+ require "log4r/staticlogger"
7
+
8
+ module Log4r
9
+
10
+ # RollingFileOutputter - subclass of FileOutputter that rolls files on size
11
+ # or time. Additional hash arguments are:
12
+ #
13
+ # [<tt>:maxsize</tt>] Maximum size of the file in bytes.
14
+ # [<tt>:trunc</tt>] Maximum age of the file in seconds.
15
+ class RollingFileOutputter < FileOutputter
16
+
17
+ attr_reader :count, :maxsize, :maxtime, :startTime#,i:baseFilename
18
+
19
+ def initialize(_name, hash={})
20
+ @count = 0
21
+ super(_name, hash)
22
+ if hash.has_key?(:maxsize) || hash.has_key?('maxsize')
23
+ _maxsize = (hash[:maxsize] or hash['maxsize']).to_i
24
+ if _maxsize.class != Fixnum
25
+ raise TypeError, "Argument 'maxsize' must be an Fixnum", caller
26
+ end
27
+ if _maxsize == 0
28
+ raise TypeError, "Argument 'maxsize' must be > 0", caller
29
+ end
30
+ @maxsize = _maxsize
31
+ end
32
+ if hash.has_key?(:maxtime) || hash.has_key?('maxtime')
33
+ _maxtime = (hash[:maxtime] or hash['maxtime']).to_i
34
+ if _maxtime.class != Fixnum
35
+ raise TypeError, "Argument 'maxtime' must be an Fixnum", caller
36
+ end
37
+ if _maxtime == 0
38
+ raise TypeError, "Argument 'maxtime' must be > 0", caller
39
+ end
40
+ @maxtime = _maxtime
41
+ @startTime = Time.now
42
+ end
43
+ @baseFilename = File.basename(@filename)
44
+ # roll immediately so all files are of the form "000001-@baseFilename"
45
+ roll
46
+ # initialize the file size counter
47
+ @datasize = 0
48
+ end
49
+
50
+ #######
51
+ private
52
+ #######
53
+
54
+ # perform the write
55
+ def write(data)
56
+ # we have to keep track of the file size ourselves - File.size doesn't
57
+ # seem to report the correct size when the size changes rapidly
58
+ @datasize += data.size + 1 # the 1 is for newline
59
+ super
60
+ roll if requiresRoll
61
+ end
62
+
63
+ # construct a new filename from the count and baseFilname
64
+ def makeNewFilename
65
+ # note use of hard coded 6 digit counter width - is this enough files?
66
+ pad = "0" * (6 - @count.to_s.length) + count.to_s
67
+ newbase = @baseFilename.sub(/(\.\w*)$/, pad + '\1')
68
+ @filename = File.join(File.dirname(@filename), newbase)
69
+ Logger.log_internal {"File #{@filename} created"}
70
+ end
71
+
72
+ # does the file require a roll?
73
+ def requiresRoll
74
+ if !@maxsize.nil? && @datasize > @maxsize
75
+ @datasize = 0
76
+ return true
77
+ end
78
+ if !@maxtime.nil? && (Time.now - @startTime) > @maxtime
79
+ @startTime = Time.now
80
+ return true
81
+ end
82
+ false
83
+ end
84
+
85
+ # roll the file
86
+ def roll
87
+ begin
88
+ @out.close
89
+ rescue
90
+ Logger.log_internal {
91
+ "RollingFileOutputter '#{@name}' could not close #{@filename}"
92
+ }
93
+ end
94
+ @count += 1
95
+ makeNewFilename
96
+ @out = File.new(@filename, (@trunc ? "w" : "a"))
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+
103
+ # this can be found in examples/fileroll.rb as well
104
+ if __FILE__ == $0
105
+ require 'log4r'
106
+ include Log4r
107
+
108
+
109
+ timeLog = Logger.new 'WbExplorer'
110
+ timeLog.outputters = RollingFileOutputter.new("WbExplorer", { "filename" => "TestTime.log", "maxtime" => 10, "trunc" => true })
111
+ timeLog.level = DEBUG
112
+
113
+ 100.times { |t|
114
+ timeLog.info "blah #{t}"
115
+ sleep(1.0)
116
+ }
117
+
118
+ sizeLog = Logger.new 'WbExplorer'
119
+ sizeLog.outputters = RollingFileOutputter.new("WbExplorer", { "filename" => "TestSize.log", "maxsize" => 16000, "trunc" => true })
120
+ sizeLog.level = DEBUG
121
+
122
+ 10000.times { |t|
123
+ sizeLog.info "blah #{t}"
124
+ }
125
+
126
+ end
@@ -0,0 +1,30 @@
1
+ # :nodoc:
2
+ module Log4r
3
+
4
+ class Outputter
5
+ # Retrieve an outputter.
6
+ def self.[](name)
7
+ out = @@outputters[name]
8
+ if out.nil?
9
+ return case name
10
+ when 'stdout' then StdoutOutputter.new 'stdout'
11
+ when 'stderr' then StderrOutputter.new 'stderr'
12
+ else nil end
13
+ end
14
+ out
15
+ end
16
+ def self.stdout; Outputter['stdout'] end
17
+ def self.stderr; Outputter['stderr'] end
18
+ # Set an outputter.
19
+ def self.[]=(name, outputter)
20
+ @@outputters[name] = outputter
21
+ end
22
+ # Yields each outputter's name and reference.
23
+ def self.each
24
+ @@outputters.each {|name, outputter| yield name, outputter}
25
+ end
26
+ def self.each_outputter
27
+ @@outputters.each_value {|outputter| yield outputter}
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,75 @@
1
+ # :include: ../rdoc/syslogoutputter
2
+ #
3
+ # Version:: $Id: syslogoutputter.rb,v 1.1.1.1 2004/03/19 03:31:10 fando Exp $
4
+ # Author:: Steve Lumos
5
+ # Author:: Leon Torres
6
+
7
+ require 'log4r/formatter/formatter'
8
+ require 'log4r/outputter/outputter'
9
+ require 'log4r/configurator'
10
+ require 'syslog'
11
+
12
+ module Log4r
13
+
14
+ # syslog needs to set the custom levels
15
+ if LNAMES.size > 1
16
+ raise LoadError, "Must let syslogger.rb define custom levels"
17
+ end
18
+ # tune log4r to syslog priorities
19
+ Configurator.custom_levels("DEBUG", "INFO", "NOTICE", "WARNING", "ERR", "CRIT", "ALERT", "EMERG")
20
+
21
+ class SyslogOutputter < Outputter
22
+ include Syslog::Constants
23
+
24
+ # maps log4r levels to syslog priorities (logevents never see ALL and OFF)
25
+ SYSLOG_LEVELS = [
26
+ nil,
27
+ LOG_DEBUG,
28
+ LOG_INFO,
29
+ LOG_WARNING,
30
+ LOG_ERR,
31
+ LOG_CRIT,
32
+ nil
33
+ ]
34
+
35
+ # There are 3 hash arguments
36
+ #
37
+ # [<tt>:ident</tt>] syslog ident, defaults to _name
38
+ # [<tt>:logopt</tt>] syslog logopt, defaults to LOG_PID | LOG_CONS
39
+ # [<tt>:facility</tt>] syslog facility, defaults to LOG_USER
40
+ def initialize(_name, hash={})
41
+ super(_name, hash)
42
+ ident = (hash[:ident] or hash['ident'] or _name)
43
+ logopt = (hash[:logopt] or hash['logopt'] or LOG_PID | LOG_CONS).to_i
44
+ facility = (hash[:facility] or hash['facility'] or LOG_USER).to_i
45
+ @syslog = Syslog.open(ident, logopt, facility)
46
+ end
47
+
48
+ def closed?
49
+ return !@syslog.opened?
50
+ end
51
+
52
+ def close
53
+ @syslog.close unless @syslog.nil?
54
+ @level = OFF
55
+ OutputterFactory.create_methods(self)
56
+ Logger.log_internal {"Outputter '#{@name}' closed Syslog and set to OFF"}
57
+ end
58
+
59
+ private
60
+
61
+ def canonical_log(logevent)
62
+ pri = SYSLOG_LEVELS[logevent.level]
63
+ o = logevent.data
64
+ if o.kind_of? Exception then
65
+ msg = "#{o.class} at (#{o.backtrace[0]}): #{o.message}"
66
+ elsif o.respond_to? :to_str then
67
+ msg = o.to_str
68
+ else
69
+ msg = o.inspect
70
+ end
71
+
72
+ @syslog.log(pri, '%s', msg)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,243 @@
1
+ = Configuring Log4r with Log4r::Configurator
2
+
3
+ The Configurator class allows one to set up Log4r via XML.
4
+ Additionally, Configurator contains methods to configure any Log4r
5
+ defaults. In particular, Configurator provides a method to
6
+ customize the logging levels.
7
+
8
+ Log4r is also configurable using YAML. For that, there is
9
+ a class similar to Configurator called Log4r::YamlConfigurator. Please see
10
+ log4r/yamlconfigurator.rb for details.
11
+
12
+ REXML is required for XML configuration. Get REXML at
13
+ http://www.ruby-lang.org/en/raa-list.rhtml?name=REXML
14
+
15
+ To use the Configurator class,
16
+
17
+ require 'log4r/configurator'
18
+
19
+ == Custom Levels
20
+
21
+ Suppose you want the following levels and ranks:
22
+
23
+ Foo < Bar < Baz
24
+
25
+ This is easily accomplished:
26
+
27
+ Configurator.custom_levels('Foo', 'Bar', :Baz)
28
+
29
+ The method accepts strings or symbols. However, custom levels must have names
30
+ that are valid for Ruby constants. Also, custom levels should be set before
31
+ anything else is done with Log4r, otherwise the default levels will be loaded.
32
+
33
+ You can set custom levels in XML. That's covered in the following section.
34
+
35
+ == XML Configuration
36
+
37
+ If you have REXML, you can configure Log4r with XML.
38
+ To do this, first write an XML configuration (which you can learn by
39
+ studying this document and the examples provided in the distribution)
40
+ and then load up the XML from within your program as follows:
41
+
42
+ Configurator.load_xml_file('/path/to/file.xml')
43
+
44
+ The Log4r XML configuration system is very flexible and powerful. In fact,
45
+ it is somewhat preferable to configuring Log4r in Ruby. In order to take
46
+ full advantage of this feature, there are several concepts one must know.
47
+ They are covered in the following three sections.
48
+
49
+ === Concept: XML Directives
50
+
51
+ The expressive power of Ruby has enabled a feature I call
52
+ <i>XML directives</i>. An XML directive is a name-value pair belonging to
53
+ some element. It
54
+ may be represented as an attribute (name="value") of the element, or
55
+ as a child (<name>value</name>) of the element. Therefore, you are
56
+ free to specify information about an object as either an attribute
57
+ or an element. An example should clarify:
58
+
59
+ <object data="value"/>
60
+
61
+ Is equivalent to:
62
+
63
+ <object>
64
+ <data>value</data>
65
+ </object>
66
+
67
+ You can assume this behavior except where noted elsewhere in the API.
68
+
69
+ === Concept: XML Parameters
70
+
71
+ A scheme which I call <i>XML parameters</i> enables one to utilize the XML
72
+ configuratin system for custom Outputters and Formatters.
73
+ This requires <b>no</b> extra work on your part, so long as your objects
74
+ are set up using hash arguments and can decode string values. That is, once
75
+ you've written a custom Outputter, it is automatically configurable in XML
76
+ without having to write any extra code.
77
+
78
+ An XML parameter is analogous to a hash argument to some object's <tt>new</tt>
79
+ method. Consider these hash arguments to FileOutputter:
80
+
81
+ :filename => '/path/to/logs/my.log'
82
+ :trunc => 'true'
83
+
84
+ We can specify them in XML like this:
85
+
86
+ <outputter type="FileOutputter" trunc="true">
87
+ <filename>/path/to/logs/my.log</filename>
88
+ ...
89
+
90
+ The name of the element/attribute is just the name of the parameter. Note that
91
+ the input will be a string, thus it's wise to convert the data in from
92
+ strings in any custom classes (to_i for integers, etc). Now let's suppose you
93
+ have defined a custom Outputter named MyOutputter with the following
94
+ additional hash args:
95
+
96
+ :myarg1 => 'foo'
97
+ :myarg2 => 123
98
+
99
+ Automagically, you can configure your Outputter like so:
100
+
101
+ <outputter type="MyOutputter" myarg2="123">
102
+ <myarg1>foo</myarg1>
103
+ ...
104
+
105
+ Isn't that nice? <tt>:-)</tt>
106
+
107
+ === Concept: Variable Substitution
108
+
109
+ To kill the need for preprocessors, Configurator provides a means of variable
110
+ substitution for XML parameters at runtime. If you specify
111
+ <tt>#{foo}</tt> in an XML parameter value, Configurator will replace it with
112
+ the value of 'foo' in its parameter hashtable. The primary idea is that you
113
+ can figure stuff out in your program,
114
+ say the log path, and relay that information to the XML while it's being
115
+ loaded. Secondarily, it is a way to have aliases within an XML document.
116
+
117
+ There are two ways to tell Configurator about these variables. The first
118
+ method we'll cover is done within a Ruby program with Configurator[].
119
+
120
+ Configurator['logpath'] = '/path/to/logs'
121
+
122
+ Thereafter, any occurence of <tt>#{logpath}</tt> in each and every XML
123
+ parameter will be substituted with '/path/to/logs'. For example:
124
+
125
+ <filename>#{logpath}/mylog.log</filename>
126
+
127
+ Becomes,
128
+
129
+ <filename>/path/to/logs/mylog.log</filename>
130
+
131
+ Aside from Configurator[], another way to define XML parameter variables
132
+ is to define <tt>parameters</tt> under the <tt><pre_config></tt> element
133
+ of an XML configuration:
134
+
135
+ <pre_config>
136
+ <parameter name="logpath" value="/path/to/logs'/>
137
+ <parameter name="other" value="somethingelse'/>
138
+ ...
139
+ </pre_config>
140
+
141
+ Alternatively,
142
+
143
+ <pre_config>
144
+ <parameters>
145
+ <logpath>/path/to/logs</logpath>
146
+ <other>somethingelse</other>
147
+ ...
148
+ </parameters>
149
+ ...
150
+
151
+ The end result is the same as using Configurator[]. However, this method
152
+ is not dynamic. Configurator[] should be used when you want to set variables
153
+ from within Ruby.
154
+
155
+ = XML Grammar
156
+
157
+ And now, here's the XML grammar we use to configure Log4r.
158
+
159
+ == Root Element
160
+
161
+ The root element is <tt><log4r_config></tt>. It can be embedded as a node of
162
+ any other element in an XML file. For instance:
163
+
164
+ <my-xml-thing>
165
+ <customize-libraries>
166
+ <log4r_config>
167
+ <!-- log4r configuratin goes here -->
168
+ </log4r_config>
169
+ ...
170
+
171
+ == Pre-config element
172
+
173
+ The pre_config element is a child of log4r_config and contains:
174
+
175
+ * 'custom_levels' element
176
+ * 'global' element
177
+ * 'parameters' element
178
+ * any number of 'parameter' elements
179
+
180
+ === Pre_config: Custom Levels
181
+
182
+ The custom_levels element is not an <i>XML directive</i> of pre_config. It
183
+ <b>must</b> be specified like this:
184
+
185
+ <custom_levels>Foo, Bar, Baz</custom_levels>
186
+
187
+ And <b>not</b> like this:
188
+
189
+ <!-- NOT SUPPORTED -->
190
+ <custom_levels levels="Foo, Bar, Baz"/>
191
+
192
+ === Pre_config: Global Level
193
+
194
+ <global level="DEBUG"/>
195
+
196
+ or
197
+
198
+ <global><level>DEBUG</level></global>
199
+
200
+ Here, level is an XML directive of global.
201
+
202
+ === Pre_config: Parameters
203
+
204
+ Parameters are variables that will be substituted later on. Please
205
+ see the <b>Concept: Variable Substitution</b> section above. Parameters
206
+ are <i>XML Directives</i>, which means they can be expressed using elements
207
+ or attributes. Here is an example:
208
+
209
+ <parameter name="param name 1" value="param value 1">
210
+ <parameter name="param name 2" value="param value 2">
211
+ ...
212
+ <parameters>
213
+ <param3>value3</param3>
214
+ <param4>value3</param4>
215
+ ...
216
+
217
+ === Pre_config: Complete Example
218
+
219
+ <log4r_config>
220
+
221
+ <pre_config>
222
+ <custom_levels>
223
+ Foo,Bar, Baz
224
+ </custom_levels>
225
+ <global level="Bar"/>
226
+ <parameters>
227
+ <logpath>/var/log/foo</logpath>
228
+ <mypattern>%l [%d] %m</mypattern>
229
+ </parameters>
230
+ </pre_config>
231
+
232
+ <!-- define some outputters and loggers -->
233
+
234
+ </log4r_config>
235
+
236
+ == Configuring Log4r Objects
237
+
238
+ The XML configuration grammar for Loggers, Outputters and the like are
239
+ covered in the usage guidelines for those classes.
240
+
241
+ == Order Doesn't Matter
242
+
243
+ You can (it is hoped) define any of the XML objects in any order desired.