mtn_log4r 1.1.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +15 -0
  2. data/doc/content/contact.html +22 -0
  3. data/doc/content/contribute.html +21 -0
  4. data/doc/content/index.html +90 -0
  5. data/doc/content/license.html +56 -0
  6. data/doc/content/manual.html +449 -0
  7. data/doc/dev/README.developers +55 -0
  8. data/doc/dev/checklist +23 -0
  9. data/doc/dev/things-to-do +5 -0
  10. data/doc/images/log4r-logo.png +0 -0
  11. data/doc/images/logo2.png +0 -0
  12. data/doc/log4r.css +111 -0
  13. data/doc/rdoc-log4r.css +696 -0
  14. data/doc/templates/main.html +147 -0
  15. data/examples/README +19 -0
  16. data/examples/ancestors.rb +53 -0
  17. data/examples/chainsaw_settings.xml +7 -0
  18. data/examples/customlevels.rb +34 -0
  19. data/examples/filelog.rb +25 -0
  20. data/examples/fileroll.rb +40 -0
  21. data/examples/gmail.rb +30 -0
  22. data/examples/gmail.yaml +95 -0
  23. data/examples/log4r_yaml.yaml +0 -0
  24. data/examples/logclient.rb +25 -0
  25. data/examples/logserver.rb +18 -0
  26. data/examples/moderate.xml +29 -0
  27. data/examples/moderateconfig.rb +66 -0
  28. data/examples/myformatter.rb +23 -0
  29. data/examples/outofthebox.rb +21 -0
  30. data/examples/rdoc-gen +2 -0
  31. data/examples/rrconfig.xml +63 -0
  32. data/examples/rrsetup.rb +42 -0
  33. data/examples/simpleconfig.rb +39 -0
  34. data/examples/syslogcustom.rb +52 -0
  35. data/examples/xmlconfig.rb +25 -0
  36. data/examples/yaml.rb +30 -0
  37. data/lib/log4r/GDC.rb +41 -0
  38. data/lib/log4r/MDC.rb +59 -0
  39. data/lib/log4r/NDC.rb +86 -0
  40. data/lib/log4r/base.rb +74 -0
  41. data/lib/log4r/config.rb +9 -0
  42. data/lib/log4r/configurator.rb +224 -0
  43. data/lib/log4r/formatter/formatter.rb +109 -0
  44. data/lib/log4r/formatter/log4jxmlformatter.rb +65 -0
  45. data/lib/log4r/formatter/patternformatter.rb +145 -0
  46. data/lib/log4r/lib/drbloader.rb +52 -0
  47. data/lib/log4r/lib/xmlloader.rb +24 -0
  48. data/lib/log4r/logevent.rb +28 -0
  49. data/lib/log4r/logger.rb +199 -0
  50. data/lib/log4r/loggerfactory.rb +89 -0
  51. data/lib/log4r/logserver.rb +28 -0
  52. data/lib/log4r/outputter/consoleoutputters.rb +18 -0
  53. data/lib/log4r/outputter/datefileoutputter.rb +117 -0
  54. data/lib/log4r/outputter/emailoutputter.rb +143 -0
  55. data/lib/log4r/outputter/fileoutputter.rb +56 -0
  56. data/lib/log4r/outputter/iooutputter.rb +55 -0
  57. data/lib/log4r/outputter/outputter.rb +134 -0
  58. data/lib/log4r/outputter/outputterfactory.rb +61 -0
  59. data/lib/log4r/outputter/rabbitoutputter.rb +70 -0
  60. data/lib/log4r/outputter/remoteoutputter.rb +40 -0
  61. data/lib/log4r/outputter/rollingfileoutputter.rb +234 -0
  62. data/lib/log4r/outputter/scribeoutputter.rb +37 -0
  63. data/lib/log4r/outputter/staticoutputter.rb +30 -0
  64. data/lib/log4r/outputter/syslogoutputter.rb +130 -0
  65. data/lib/log4r/outputter/udpoutputter.rb +53 -0
  66. data/lib/log4r/rdoc/GDC +14 -0
  67. data/lib/log4r/rdoc/MDC +16 -0
  68. data/lib/log4r/rdoc/NDC +41 -0
  69. data/lib/log4r/rdoc/configurator +243 -0
  70. data/lib/log4r/rdoc/emailoutputter +103 -0
  71. data/lib/log4r/rdoc/formatter +39 -0
  72. data/lib/log4r/rdoc/log4jxmlformatter +21 -0
  73. data/lib/log4r/rdoc/log4r +89 -0
  74. data/lib/log4r/rdoc/logger +175 -0
  75. data/lib/log4r/rdoc/logserver +85 -0
  76. data/lib/log4r/rdoc/outputter +108 -0
  77. data/lib/log4r/rdoc/patternformatter +128 -0
  78. data/lib/log4r/rdoc/scribeoutputter +16 -0
  79. data/lib/log4r/rdoc/syslogoutputter +29 -0
  80. data/lib/log4r/rdoc/win32eventoutputter +7 -0
  81. data/lib/log4r/rdoc/yamlconfigurator +20 -0
  82. data/lib/log4r/repository.rb +88 -0
  83. data/lib/log4r/staticlogger.rb +49 -0
  84. data/lib/log4r/version.rb +4 -0
  85. data/lib/log4r/yamlconfigurator.rb +198 -0
  86. data/lib/log4r.rb +18 -0
  87. data/tests/README +10 -0
  88. data/tests/testGDC.rb +24 -0
  89. data/tests/testMDC.rb +40 -0
  90. data/tests/testNDC.rb +25 -0
  91. data/tests/test_helper.rb +12 -0
  92. data/tests/testall.rb +6 -0
  93. data/tests/testbase.rb +48 -0
  94. data/tests/testchainsaw.rb +42 -0
  95. data/tests/testconf.xml +37 -0
  96. data/tests/testcustom.rb +30 -0
  97. data/tests/testformatter.rb +31 -0
  98. data/tests/testlogger.rb +200 -0
  99. data/tests/testoutputter.rb +143 -0
  100. data/tests/testpatternformatter.rb +76 -0
  101. data/tests/testthreads.rb +31 -0
  102. data/tests/testxmlconf.rb +48 -0
  103. data/tests/testyaml.rb +39 -0
  104. data/tests/testyaml_arrays.yaml +25 -0
  105. data/tests/testyaml_injection.yaml +22 -0
  106. metadata +193 -0
@@ -0,0 +1,130 @@
1
+ # :include: ../rdoc/syslogoutputter
2
+ #
3
+ # Version:: $Id$
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
+ SYSLOGNAMES = Hash.new
15
+
16
+ class SyslogOutputter < Outputter
17
+ include Syslog::Constants
18
+
19
+ # maps default log4r levels to syslog priorities (logevents never see ALL and OFF)
20
+ # SYSLOG Levels are:
21
+ # "DEBUG" => Syslog::LOG_DEBUG
22
+ # "INFO" => Syslog::LOG_INFO
23
+ # "NOTICE" => Syslog::LOG_NOTICE
24
+ # "WARN" => Syslog::LOG_WARN
25
+ # "ERROR" => Syslog::LOG_ERROR
26
+ # "FATAL" => Syslog::LOG_FATAL
27
+ # "ALERT" => Syslog::LOG_ALERT
28
+ # "EMERG" => Syslog::LOG_EMERG
29
+ SYSLOG_LEVELS_MAP = {
30
+ "DEBUG" => LOG_DEBUG,
31
+ "INFO" => LOG_INFO,
32
+ "NOTICE" => LOG_NOTICE, # by default NOTICE is not in log4r
33
+ "WARN" => LOG_WARNING,
34
+ "ERROR" => LOG_ERR,
35
+ "FATAL" => LOG_CRIT,
36
+ "ALERT" => LOG_ALERT, # by default ALERT is not in log4r
37
+ "EMERG" => LOG_EMERG, # by default EMERG is not in log4r
38
+ }
39
+
40
+ # mapping from Log4r default levels to syslog, by string name
41
+ # "DEBUG" => "DEBUG"
42
+ # "INFO" => "INFO"
43
+ # "WARN" => "WARN"
44
+ # "ERROR" => "ERROR"
45
+ # "FATAL" => "FATAL"
46
+ SYSLOG_LOG4R_MAP = {
47
+ "DEBUG" => "DEBUG",
48
+ "INFO" => "INFO",
49
+ "WARN" => "WARN",
50
+ "ERROR" => "ERROR",
51
+ "FATAL" => "FATAL"
52
+ # "NOTICE" => "INFO", # by default NOTICE is not in log4r
53
+ # "ALERT" => "FATAL", # by default ALERT is not in log4r
54
+ # "EMERG" => "FATAL" # by default EMERG is not in log4r
55
+ }
56
+
57
+ @levels_map = SYSLOG_LOG4R_MAP
58
+
59
+ # There are 3 hash arguments
60
+ #
61
+ # [<tt>:ident</tt>] syslog ident, defaults to _name
62
+ # [<tt>:logopt</tt>] syslog logopt, defaults to LOG_PID | LOG_CONS
63
+ # [<tt>:facility</tt>] syslog facility, defaults to LOG_USER
64
+ def initialize(_name, hash={})
65
+ super(_name, hash)
66
+ ident = (hash[:ident] or hash['ident'] or _name)
67
+ logopt = (hash[:logopt] or hash['logopt'] or LOG_PID | LOG_CONS).to_i
68
+ facility = (hash[:facility] or hash['facility'] or LOG_USER).to_i
69
+ map_levels_by_name_to_syslog()
70
+ if ( Syslog.opened? ) then
71
+ Logger.log_internal { "Syslog already initialized, to alter, " +
72
+ "you must close first"}
73
+ end
74
+ @syslog = ( Syslog.opened? ) ? Syslog : Syslog.open(ident, logopt, facility)
75
+ end
76
+
77
+ def closed?
78
+ return !@syslog.opened?
79
+ end
80
+
81
+ def close
82
+ @syslog.close unless @syslog.nil?
83
+ @level = OFF
84
+ OutputterFactory.create_methods(self)
85
+ Logger.log_internal {"Outputter '#{@name}' closed Syslog and set to OFF"}
86
+ end
87
+
88
+ # A single hash argument that maps custom names to syslog names
89
+ #
90
+ # [<tt>levels_map</tt>] A map that will create a linkage between levels
91
+ # in a hash and underlying syslog levels.
92
+ # By default, these are direct mapping of the log4r
93
+ # levels (e.g. "DEBUG" => "DEBUG")
94
+ # If you have defined your own custom levels, you
95
+ # should provide this underlying mapping, otherwise
96
+ # all messages will be mapped to the underlying syslog
97
+ # level of INFO by default.
98
+ # e.g.
99
+ # You have created custom levels called:
100
+ # <tt>Configurator.custom_levels "HIGH", "MEDIUM", "LOW"</tt>
101
+ # To map these to 'equivilent' syslog levels, after instantiatin
102
+ # a syslogoutputter:
103
+ # <tt>SyslogOutputter.map_levels_by_name_to_syslog(
104
+ # { "HIGH" => "ALERT", "MEDIUM" => "WARN", "LOW" => "INFO" }
105
+ # )</tt>
106
+ def map_levels_by_name_to_syslog( lmap = SYSLOG_LOG4R_MAP )
107
+ @levels_map = lmap
108
+ end
109
+
110
+ def get_levels_map()
111
+ return @levels_map
112
+ end
113
+
114
+ private
115
+
116
+ def canonical_log(logevent)
117
+ pri = SYSLOG_LEVELS_MAP[@levels_map[LNAMES[logevent.level]]] rescue pri = LOG_INFO
118
+ o = format(logevent)
119
+ if o.kind_of? Exception then
120
+ msg = "#{o.class} at (#{o.backtrace[0]}): #{o.message}"
121
+ elsif o.respond_to? :to_str then
122
+ msg = o.to_str
123
+ else
124
+ msg = o.inspect
125
+ end
126
+
127
+ @syslog.log(pri, '%s', msg)
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,53 @@
1
+ # :include: ../rdoc/outputter
2
+ #
3
+ # == Other Info
4
+ #
5
+ # Version:: $Id$
6
+ # Author:: Leon Torres <leon@ugcs.caltech.edu>
7
+
8
+ require "log4r/outputter/outputter"
9
+ require 'log4r/staticlogger'
10
+ require "socket"
11
+
12
+ module Log4r
13
+
14
+ class UDPOutputter < Outputter
15
+ attr_reader :host, :port
16
+ attr_accessor :udpsock
17
+
18
+ def initialize(_name, hash={})
19
+ super(_name, hash)
20
+ @host = (hash[:hostname] or hash["hostname"])
21
+ @port = (hash[:port] or hash["port"])
22
+
23
+ begin
24
+ Logger.log_internal {
25
+ "UDPOutputter will send to #{@host}:#{@port}"
26
+ }
27
+ @udpsock = UDPSocket.new
28
+ @udpsock.connect( @host, @port )
29
+ rescue Exception => e
30
+ Logger.log_internal(ERROR) {
31
+ "UDPOutputter failed to create UDP socket: #{e}"
32
+ }
33
+ Logger.log_internal {e}
34
+ self.level = OFF
35
+ raise e
36
+ end
37
+ end
38
+
39
+ #######
40
+ private
41
+ #######
42
+
43
+ def write(data)
44
+ @udpsock.send(data, 0)
45
+ rescue Exception => e
46
+ Logger.log_internal(ERROR) {
47
+ "UDPOutputter failed to send data to #{@host}:#{@port}, #{e}"
48
+ }
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,14 @@
1
+ = GDC
2
+
3
+ The GDC class implements a copy of the Global Diagnostic Context, which
4
+ is not part of the Apache Log4j library, as of this writing (10 Jan 2009).
5
+
6
+ The GDC is like the NDC and MDC classes, only it is global to the
7
+ application (see NDC and MDC for details on those classes).
8
+
9
+ The GDC is local to the main thread, and any new threads will return
10
+ the value of the current GDC set in the main thread.
11
+
12
+ Only the main thread can set the GDC, any other threads that
13
+ attempt to will raise an exception.
14
+
@@ -0,0 +1,16 @@
1
+ = MDC
2
+
3
+ The MDC class implements a copy of the Mapped Diagnostic Context, which
4
+ is part of the Apache Log4j library. See the NDC documentation for
5
+ more details. MDCs are much like NDCs, but instead of a stack context
6
+ it uses a map for holding this information.
7
+
8
+ This allows for selection of information out of the map when
9
+ the log message is being created.
10
+
11
+ MDCs are thread safe, and are unique to each thread.
12
+
13
+ An important difference between MDCs in Log4r vs Log4j is that they
14
+ only inherit from the main thread. Ruby treats all new threads as
15
+ being the children of the main thread, even if they are started
16
+ from a thread that is not main.
@@ -0,0 +1,41 @@
1
+ = NDC
2
+
3
+ The NDC class implements a copy of the Nested Diagnostic Context, which
4
+ is part of the Apache Log4j library. Nested Diagnostic Contexts were
5
+ derived from Neil Harrison's article on "Patterns for Logging
6
+ Diagnostic Messages", part of the book "Pattern Languages of Program
7
+ Design 3" edited by Martin et al.
8
+
9
+ NDCs in Log4r are thread safe.
10
+
11
+ NDCs in log4r are close enough to NDCs in Log4j that I include its
12
+ documentation directly:
13
+
14
+ ...
15
+ A Nested Diagnostic Context, or NDC in short, is an instrument to
16
+ distinguish interleaved log output from different sources. Log output
17
+ is typically interleaved when a server handles multiple clients
18
+ near-simultaneously.
19
+
20
+ Interleaved log output can still be meaningful if each log entry from
21
+ different contexts had a distinctive stamp. This is where NDCs come into
22
+ play.
23
+
24
+ Note that NDCs are managed on a per thread basis. NDC operations such as
25
+ push, pop(), clear(), getDepth() and setMaxDepth(int) affect the NDC of
26
+ the current thread only. NDCs of other threads remain unaffected.
27
+ ...
28
+
29
+ An important difference between NDCs in Log4r vs Log4j is that you
30
+ do not have to called NDC.remove() when exiting a thread.
31
+
32
+ This class will automatically create Thread specific storage for the
33
+ current thread on the first call to any of its methods, i.e.
34
+
35
+ NDC.push( "client accept" );
36
+
37
+ New threads may inherit the NDC of the parent thread by making use of
38
+ the clone_stack() and inherit() methods. By default, the NDC is not
39
+ inherited automatically. This is unlike MDCs, which will inherit from
40
+ the main thread.
41
+
@@ -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.
@@ -0,0 +1,103 @@
1
+ = EmailOutputter
2
+
3
+ This is an experimental class that sends a number of formatted log events as
4
+ an RFC 822 email. It should work fine if Net:SMTP doesn't cause any
5
+ problems. Just in case, create a logger named 'log4r' and give it an
6
+ outputter to see the logging statements made by this class. If it fails to
7
+ send email, it will set itself to OFF and stop logging.
8
+
9
+ In order to use it,
10
+
11
+ require 'log4r/outputter/emailoutputter'
12
+
13
+ == SMTP Configuration
14
+
15
+ All arguments to Net::SMTP.start are supported. Pass them as hash
16
+ parameters to +new+. The to field is specified as a comma-delimited
17
+ list of emails (padded with \s* if desired).
18
+
19
+ An example:
20
+
21
+ email_out = EmailOutputter.new 'email_out',
22
+ :server=>'localhost',
23
+ :port=>25,
24
+ :domain=>'somewhere.com',
25
+ :from=>'me@foo.bar',
26
+ :to=>'them@foo.bar, me@foo.bar, bozo@clown.net',
27
+ :subject=>'Log Report'
28
+
29
+ == LogEvent Buffer
30
+
31
+ EmailOutputter stores log messages in a buffer. When the buffer reaches a
32
+ certain number, the <tt>buffsize</tt>, it will send an email containing the
33
+ contents of the buffer. The default +buffsize+ is 100. To set +buffsize+,
34
+
35
+ email_out.buffsize = 1000 # set the buffsize to 1000
36
+
37
+ == Flush To Send Email
38
+
39
+ Flushing an EmailOutputter will mail out all the remaining LogEvents.
40
+ This is convenient for systems that encapsulate the shutdown process. It's a
41
+ good idea to do this for all outputters,
42
+
43
+ Outputter.each_outputter {|o| o.flush}
44
+
45
+ Alternatively, one can invoke flush on the outputter directly,
46
+
47
+ email_out.flush
48
+
49
+ It's also a good idea to notify the recepient of the email that
50
+ the system is shutting down. Before flushing, log a message
51
+ to the owner of this outputter,
52
+
53
+ log_with_email_out.info "The system is shutting down at #{Time.now}"
54
+
55
+ == Format When?
56
+
57
+ LogEvents may either be formatted as they come in or as the
58
+ email is being composed. To do the former, specify a value of +true+
59
+ to the hash parameter +formatfirst+. The default is to format
60
+ during email composition.
61
+
62
+ email_out.formatfirst = true # format as soon as LogEvents are received
63
+
64
+ == Immediate Notification
65
+
66
+ EmailOutputter can be configured to flush and send the email whenever the
67
+ logger sees a certain log priority. Use the +immediate_at+ hash parameter
68
+ and specify the levels as a comma-delimited list (like an XML element).
69
+ To trigger an email on FATAL and ERROR,
70
+
71
+ email_out.immediate_at = "FATAL, ERROR"
72
+
73
+ == Example
74
+
75
+ A security logger sends email to several folks, buffering up to 25
76
+ log events and sending immediates on CRIT and WARN
77
+
78
+ EmailOutputter.new 'security',
79
+ :to => 'bob@secure.net, frank@secure.net',
80
+ :buffsize => 25,
81
+ :immediate_at => 'WARN, CRIT'
82
+
83
+ == XML Configuration
84
+
85
+ See log4r/configurator.rb for details. Here's an example:
86
+
87
+ <outputter name="security" type="EmailOutputter"
88
+ buffsize="25" level="ALL">
89
+ <immediate_at>WARN, CRIT</immediate_at>
90
+ <server>localhost</server>
91
+ <from>me@secure.net</from>
92
+ <to>
93
+ bob@secure.net, frank@secure.net
94
+ </to>
95
+ ...
96
+ </outputter>
97
+
98
+ == To Do
99
+
100
+ This class could use some sophistication, in particular a means to compress
101
+ the logs, a way to set the subject dynamically (probably via a block method),
102
+ and a time trigger. When the time trigger is introduced, a +buffsize+
103
+ of 0 should mean ignore +buffsize+ to determine when to send the email.
@@ -0,0 +1,39 @@
1
+ = Formatters
2
+
3
+ Formatters are responsible for formatting LogEvent data.
4
+ An Outputter owning a Formatter will invoke the
5
+ Log4r::Formatter#format method prior to writing.
6
+
7
+ == Available Formatters
8
+
9
+ * Log4r::BasicFormatter - default
10
+ * Log4r::PatternFormatter - most flexible. See log4r/formatter/patternformatter.rb
11
+ * Log4r::SimpleFormatter - like BasicFormatter for Strings only (low noise)
12
+ * Log4r::ObjectFormatter - for inspecting objects
13
+ * Log4r::NullFormatter - twirls on its feet and does nothing
14
+
15
+ = XML Configuration
16
+
17
+ Specify the Formatter and its class (as +type+) under an
18
+ <tt><outputter></tt> directive:
19
+
20
+ <outputter name="someout" type="sometype">
21
+ <formatter type="Log4r::BasicFormatter"/>
22
+ </outputter>
23
+
24
+ As explained in log4r/configurator.rb, the hash arguments you would normally
25
+ pass to +new+ are specified as <i>XML parameters</i>. Only PatternFormatter
26
+ has any of these.
27
+
28
+ = Custom Formatting
29
+
30
+ Building a custom Formatter is extremely easy. Just define a class
31
+ that extends Formatter and override the Formatter#format method.
32
+ Then give it to any interested Outputters.
33
+
34
+ If you're interested in setting up your custom formatters in XML,
35
+ please take a look at log4r/configurator.rb.
36
+
37
+ == Data Available
38
+
39
+ See Log4r::LogEvent
@@ -0,0 +1,21 @@
1
+ = Configuring Log4r with Log4r::YamlConfigurator
2
+
3
+ The YamlConfigurator class allows one to set up Log4r via YAML.
4
+ It is used almost exactly as Log4r::Configurator and has the same features,
5
+
6
+ ycfg = YamlConfigurator # handy shorthand
7
+ ycfg['foo'] = bar # replaces instances of #{foo} in the YAML with bar
8
+ ycfg.load_yaml_file('foo.yaml')
9
+
10
+ Ruby 1.7 and 1.8 comes with a YAML parser. Hence, YAML can be used
11
+ to configure Log4r out of the box.
12
+
13
+ A comprehensive example of a Log4r YAML configuration is provided in the
14
+ examples directory.
15
+
16
+ To use this class:
17
+
18
+ require 'log4r/yamlconfigurator'
19
+
20
+ Thanks to Andreas Hund for making this possible.
21
+