Log4j is an open source project that allows the developers to control which log statements are output with arbitrary granularity. It is fully configurable at runtime using external configuration files. The advantages of the logging are providing a precise context about a run of the application, the generation of the logging output requires no human intervention, log output can be saved in persistent medium. The drawback of logging is slowing down the application.
Log4j has three main components. They are loggers, appenders and layouts. These three components are used to log messages according to message type and level and to control at run time how these messages are formatted and when they are reported. The advantage of logging API than System.out.println(); is it's ability to disable certain log statements while allowing others to print unhindered( logging space, that is, the space of all possible logging statements, is categorized according to some developer-chosen criteria ).
The root logger is the top of the logger hierarchy. It always exists and cannot be retrieved by name. Logger.getRootLogger method retrives the root of hierarchy. All other loggers are instantiated and retrieved with the class static Logger.getLogger method. Some methods in the logger class are stated below.
public void trace(Object message);
public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);
public void log(Level l, Object message);
Loggers may be assigned levels. They are TRACE, DEBUG, INFO, WARN, ERROR and FATAL defined in the org.apache.log4j.level class. We can define our own level by subclassing the Level class. Root logger has an asigned level. If a logger is not assigned a level, it inherits from the ancestors. Logging requests are made by invoking one of the printing methods of a logger instance. These printing methods are debug, info, warn, error, fatal and log.By definition, the printing method determines the level of a logging request. A logging request is said to be enabled if its level is higher than or equal to the level of its logger. Otherwise, the request is said to be disabled. A logger without an assigned level will inherit one from the hierarchy. The order of the levels is DEBUG < INFO < WARN < ERROR < FATAL. Calling the getLogger method with the same name will always return a reference to the exact same logger object.
Appenders and Layouts
Log4j allows logging requests to print to multiple destinations. An output destination is called an appender. Currently, appenders exist for the console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons. The addAppender method adds an appender to a given logger. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy.
Output format can be customized using a layout with an appender. The layout formats the logging request according to the user's wishes and an appender sends the formatted output to its destination.
PatternLayout with the conversion pattern "%r [%t] %-5p %c - %m%n" will output:176 [main] INFO org.foo.Bar - Located nearest gas station.
The first field is the number of milliseconds elapsed since the start of the program. The second field is the thread making the log request. The third field is the level of the log statement. The fourth field is the name of the logger associated with the log request. The text after the '-' is the message of the statement.
No comments:
Post a Comment