Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
Finally, loggers also have filtering. The idea is to filter out, or discard, all categories of messages below a certain priority. The categories I mentioned earlier (debug, info, warn, error, or fatal) are in priority order. (Debug is the lowest and fatal, the highest.) You can filter all messages at or below a specified level simply by telling the logger to do so—either in your code or in the configuration file.
Turning to Listing 2, the first line specifies the filter level (
DEBUGFILECONSOLEREMOTE
The remainder of the configuration file specifies properties for the appenders. For example, Listing 2's second line says
that the file appender named
FILEcom.apache.log4j.FileAppender
The rest of the configuration file does the same for the other appenders. The CONSOLE appender sends messages to the console, and the
REMOTEREMOTE
At runtime, log4j creates all the required classes for you, hooks them up as necessary, and passes the arguments you specify in the configuration file to the newly created objects using JavaBean-style "setter" methods.
Listing 2. log4j.properties: A log4j configuration file
log4j.rootLogger=DEBUG, FILE, CONSOLE, REMOTE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.file=/tmp/logs/log.txt
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n
log4j.appender.REMOTE=com.holub.log4j.RemoteAppender
log4j.appender.REMOTE.Port=1234
log4j.appender.REMOTE.layout=org.apache.log4j.PatternLayout
log4j.appender.REMOTE.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n
One of log4j's major strengths is that the tool is easy to extend. My
RemoteAppenderSocketAppender
Log4j also comes with an elaborate standalone GUI called Chainsaw that you can use to view messages from a
SocketAppender
Archived Discussions (Read only)