Log4j In 30 Minutes Or Less
The Who, What, When and Where ●
Part of the Jakarta project, a sub-project of Apache –
●
Jakarta is the sub-heading for all the Java oriented Apache projects
Open source, BSD style license library providing logging for Java programs
●
Precursors first appeared in 1996
●
Http://jakarta.apache.org/log4j/docs/index.html
The Basics ●
●
Logger –
A hierarchical tree with Appender, Level, and Layout settings at every level
–
Settings can also inherit to lower tree levels
Level –
●
●
Five built in levels (plus “all” and “off”)
Appenders –
Appenders handle log output
–
Many appenders available
Layouts
Loggers ●
●
The biggest difference between System.out.println and Log4J is loggers Creates a hierarchy that we use to set logging options –
Flow of settings is outward toward leaves of tree
–
Hierarchy based on dotted names (i.e. com.pdxinc is parent to com.pdxinc.nhinar) ●
The same scheme is used for Java classes so you can just use your class name for your logger name
Levels ●
●
Allows you to prioritize your logged messages –
Debug, Info, Warn, Error and Fatal
–
Other priorities possible but usually unnecessary
Determines which log items are actually logged –
For example, a given line of code might always attempt to log an item at Debug level but if the logger tree is not set to log items below Error level then the log message will be ignored and not make it to an appender
Appenders ●
●
Console, file, rolling file, GUI, TCP/IP, NT Event log, Unix Syslog daemon, JDBC, SMTP, JMS Multiple appenders can be used –
An appender could service only part of the tree
–
One appender could be used for auditing while another is used for general logging
Layouts ●
●
Method of customizing output from logger Simple, HTML, Pattern, TTCC, and XML layouts are included –
●
Pattern layout can be used to format according to a pattern string (ala printf)
Custom object renderers can be installed
Configuration ●
PropertiesConfigurator –
●
●
Some options cannot be set in the config file but the file is easier to edit and read than its XML equivalent
DOMConfigurator –
XML file specifies configuration
–
Can set up asynchronous logging
Runtime configurable –
Any option that can be set at startup can be set while a program is running
–
configureAndWatch() method allows configuration updates to a running program
An Example import org.apache.log4j.*; public class Demo { static Logger log = Logger.getLogger(Demo.class.getName()); public static void main(String[] args) { BasicConfigurator.configure(); log.info("Starting up..."); log.setLevel(Level.WARN); log.info("This message should not appear!"); try { // Divide by zero. int x = 5; int y = 20 / (5 - x); } catch (Exception e) { log.error("Oops!", e); } } }
Log Output From The Example 0 [main] INFO Demo - Starting up... 10 [main] ERROR Demo - Oops! java.lang.ArithmeticException: / by zero at Demo.main(Demo.java:17)
Tools ●
●
Chainsaw –
Bundled with Log4j 1.2
–
Captures log messages sent via TCP/IP
–
Can load a log generated in XML form
Lumbermill –
Captures log messages sent via TCP/IP
–
A little more attractive than Chainsaw and it has the nifty feature that it builds a logger tree on the fly and allows you to do filtering on the client machine
–
No XML log support
Optimization ●
When the generation of your logging message takes a long time, you might want to check to see if it will ever make it to an appender –
Functions like isDebugEnabled() and isEnabledFor() on the Logger class allow you to test first (testing is very fast) and then generate the string and log it if you know the message will actually be logged
Why Log4j? ●
Features
●
Easy to use, even with existing code
●
Optimized for speed
●
Thread-safe
●
Frequently used = more robust
●
Free and open source
●
Synchronous and asynchronous logging
Questions?