Log5J

Log5j is a small, fast and lightweight logging package in the style of Log4j but designed for Java versions 5.0 and beyond.

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;

Configuration

Log5j can be configured in one of three ways; All of the above means of configuration essentially resolve to a 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.

Log5j Properties

Log5j configuration properties specify logging levels, formats and log writers. Formats and writers usually also have configuration properties specific to them.

Both the keys and the values are case-sensitive.

Levels

To set the logging level for the com.example logger to INFO in a properties file, use:
    com.example.level=INFO
In 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.

Writers

Any number of writers can be set for any given logger. The value of a writer property may be the name of any class that extends Writer.

To set single writer for the logger com.example use:

    com.example.writer=org.log5j.writer.FileWriter
To 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.ConsoleWriter
The 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.

Formats

Formats can be set for any logger or writer. The value of a format property may be the name of any class that implements Format.

To set the format for the logger com.example use:

    com.example.format=org.log5j.format.XmlFormat
To set the format for the writer com.example.writer.a1 use:
    com.example.writer.a1.format=org.log5j.format.XmlFormat
Most 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.

Usage

Most people using log5j will only interact with the 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.