= Configuring Log4r with Log4r::Configurator
The Configurator class allows one to set up Log4r via XML.
Additionally, Configurator contains methods to configure any Log4r
defaults. In particular, Configurator provides a method to
customize the logging levels.
Log4r is also configurable using YAML. For that, there is
a class similar to Configurator called Log4r::YamlConfigurator. Please see
log4r/yamlconfigurator.rb for details.
REXML is required for XML configuration. Get REXML at
http://www.ruby-lang.org/en/raa-list.rhtml?name=REXML
To use the Configurator class,
require 'log4r/configurator'
== Custom Levels
Suppose you want the following levels and ranks:
Foo < Bar < Baz
This is easily accomplished:
Configurator.custom_levels('Foo', 'Bar', :Baz)
The method accepts strings or symbols. However, custom levels must have names
that are valid for Ruby constants. Also, custom levels should be set before
anything else is done with Log4r, otherwise the default levels will be loaded.
You can set custom levels in XML. That's covered in the following section.
== XML Configuration
If you have REXML, you can configure Log4r with XML.
To do this, first write an XML configuration (which you can learn by
studying this document and the examples provided in the distribution)
and then load up the XML from within your program as follows:
Configurator.load_xml_file('/path/to/file.xml')
The Log4r XML configuration system is very flexible and powerful. In fact,
it is somewhat preferable to configuring Log4r in Ruby. In order to take
full advantage of this feature, there are several concepts one must know.
They are covered in the following three sections.
=== Concept: XML Directives
The expressive power of Ruby has enabled a feature I call
XML directives. An XML directive is a name-value pair belonging to
some element. It
may be represented as an attribute (name="value") of the element, or
as a child (value) of the element. Therefore, you are
free to specify information about an object as either an attribute
or an element. An example should clarify:
Is equivalent to:
You can assume this behavior except where noted elsewhere in the API.
=== Concept: XML Parameters
A scheme which I call XML parameters enables one to utilize the XML
configuratin system for custom Outputters and Formatters.
This requires no extra work on your part, so long as your objects
are set up using hash arguments and can decode string values. That is, once
you've written a custom Outputter, it is automatically configurable in XML
without having to write any extra code.
An XML parameter is analogous to a hash argument to some object's new
method. Consider these hash arguments to FileOutputter:
:filename => '/path/to/logs/my.log'
:trunc => 'true'
We can specify them in XML like this:
/path/to/logs/my.log
...
The name of the element/attribute is just the name of the parameter. Note that
the input will be a string, thus it's wise to convert the data in from
strings in any custom classes (to_i for integers, etc). Now let's suppose you
have defined a custom Outputter named MyOutputter with the following
additional hash args:
:myarg1 => 'foo'
:myarg2 => 123
Automagically, you can configure your Outputter like so:
foo
...
Isn't that nice? :-)
=== Concept: Variable Substitution
To kill the need for preprocessors, Configurator provides a means of variable
substitution for XML parameters at runtime. If you specify
#{foo} in an XML parameter value, Configurator will replace it with
the value of 'foo' in its parameter hashtable. The primary idea is that you
can figure stuff out in your program,
say the log path, and relay that information to the XML while it's being
loaded. Secondarily, it is a way to have aliases within an XML document.
There are two ways to tell Configurator about these variables. The first
method we'll cover is done within a Ruby program with Configurator[].
Configurator['logpath'] = '/path/to/logs'
Thereafter, any occurence of #{logpath} in each and every XML
parameter will be substituted with '/path/to/logs'. For example:
#{logpath}/mylog.log
Becomes,
/path/to/logs/mylog.log
Aside from Configurator[], another way to define XML parameter variables
is to define parameters under the element
of an XML configuration:
=== Pre_config: Global Level
or
DEBUG
Here, level is an XML directive of global.
=== Pre_config: Parameters
Parameters are variables that will be substituted later on. Please
see the Concept: Variable Substitution section above. Parameters
are XML Directives, which means they can be expressed using elements
or attributes. Here is an example:
...
value3value3
...
=== Pre_config: Complete Example
Foo,Bar, Baz
/var/log/foo%l [%d] %m
== Configuring Log4r Objects
The XML configuration grammar for Loggers, Outputters and the like are
covered in the usage guidelines for those classes.
== Order Doesn't Matter
You can (it is hoped) define any of the XML objects in any order desired.