|
|||||||||
PREV NEXT | FRAMES NO FRAMES All Classes |
See:
Description
Packages | |
---|---|
org.log5j | Provides the Logger and Level classes, the interface for all
Format objects and the abstract base class for all Writer
objects. |
org.log5j.format | Provides concrete implementations of the Format interface to format
log messages in various ways. |
org.log5j.writer | Provides concrete implementations of the abstract Writer class to log
messages to various destinations. |
Log5j is a small, fast and lightweight logging package in the style of Log4j but designed for Java versions 5.0 and beyond.
It is free, open source and distributed under the Apache License, Version 2.0.
It has certain advantages over the java.util.logging
package
and Log4j;
Object
s
rather than String
s as arguments. This means String
representations do not have to be constructed until after the logging
levels have been checked and it has been ascertained the message will in
fact be logged. This should make your code faster when messages are not
being logged.Object
s as arguments all the logging methods can be
called with any primitive value as well as object references.logger.debug(string1 + string2 + ...)
which are generally
highly inefficient.log5j.properties
.log5j.xml
.Properties
, the fully
qualified class name of which is specified by the system property
log5j.class
.Properties
object at runtime. Both the properties and
the XML configuration files must follow the rules laid down by this class.
A configuration class that extends Properties
must have
a public constructor with no parameters. All configuration properties
must be set in the constructor.
Configuration properties are read just once, when the Log5j classes are first loaded.
Both the keys and the values are case-sensitive.
com.example
logger to INFO
in a properties file, use:
com.example.level=INFOIn an XML properties file it would be:
<entry key="com.example.level">INFO</entry>In a configuration class, the following line in the constructor would achieve the same effect:
setProperty("com.example.level", "INFO");The default level can be set with the key "
.level
". If it is
not set it defaults to WARN
.
Writer
.
To set single writer for the logger com.example
use:
com.example.writer=org.log5j.writer.FileWriterTo set multiple writers, for a single logger unique keys must be used. This is done by appending a number to the key as below;
com.example.writer.1=org.log5j.writer.FileWriter com.example.writer.2=org.log5j.writer.ConsoleWriterThe numbers must be integers. (1, 2, 3, 10, 11, etc. )
Most writer classes require certain configuration properties of their own. These are specified in the javadocs for those classes.
The default writer can be set with the key ".writer
". If it
is not set it defaults to org.log5j.writer.ConsoleWriter
.
Format
.
To set the format for the logger com.example
use:
com.example.format=org.log5j.format.XmlFormatTo set the format for the writer
com.example.writer.a1
use:
com.example.writer.a1.format=org.log5j.format.XmlFormatMost format classes require certain configuration properties of their own. These are specified in the javadocs for those classes.
The default format can be set with the key ".format
". If it
is not set it defaults to org.log5j.format.SimpleFormat
.
Logger
class in
their code. A Logger
may be instantiated thus;
Logger logger = Logger.getLogger(getClass());or,
Logger logger = Logger.getLogger("my.logger.name");Any object may be used as an argument to
Logger.getLogger(Object object)
. If the argument is an
instance of Class
the name of the class will be used as
the name of the Logger
. Otherwise the object's
toString()
method will be called and the result used as the name
of the Logger
.
Messages can be logged using printf style formatting, or objects and primitives may be logged directly depending the on the methods being used.
A Logger
may be used thus:
logger.debug(someObject); logger.info(someObject1, someObject2); logger.warn(1); logger.error(2, true); logger.fatal(3, null, someObject);In fact any logging method can be called with any number of arguments of any type.
Alternatively the formatting methods may be used:
logger.debugf("This is an object: %s", someObject); logger.infof("Two objects: %s %s", someObject1, someObject2); logger.warnf("A numeric: %i", 1); logger.errorf("Or a float: %f", 1.123); logger.fatalf("All 3 types: %i %f %s", 1, 2.3, someObject);
Such formatting can fail with a number of different exceptions. In this case the format string, the arguments and the exception will be logged without formatting.
|
|||||||||
PREV NEXT | FRAMES NO FRAMES All Classes |