- Before logging is enabled, you have to configure log4j first. Configuring Log4j means adding appenders (destinations/targets/handlers) to categories (loggers) and assigning a layout (formatter) to each appender.
- Categories can be created and configured in any order. In particular, a category will find and link to its descendants even if it is instantiated after them.
- You will usually configure Log4j just once which is during application initialization, usually by reading a configuration file.
- You can also configure a category programmatically to log to a particular appender using a category object‘s
addAppender() method. Add as many as you want (or none if you don‘t). This method is usually not recommended as it involves changing the source code and recompiling it. The better way is to use external configuration files such as a Java properties file or an XML file.
- Beware that appenders are not singletons, they are additive! A category inherits all the appenders from its ancestors also (by default). If you add an appender to a category and it writes to the same underlying stream (console, same file etc.) as some other appender, the same log message will appear twice (or more) in the log. In addition, if two categories in a hierarchy are configured to use the same appender name, Log4j will write twice to that appender. Use
cat.setAdditivity(false) on a category to disable inheriting of appenders. Then, log messages will only be sent to the appenders specifically configured for that category.
- Calling the static
BasicConfigurator.configure() method configures log4j to log to console just like a system.out.println(...) statement would. This method is hardwired to add to the root category a ConsoleAppender printing on the console. The output will be formatted using a PatternLayout set to the pattern %-4r [%t] %-5p %c %x - %m%n . This is usually not needed as using log4j.properties file is simpler and more flexible.
- The default file which configures log4j is
log4j.properties . Place it anywhere in the application classpath.
- Use one of the static
PropertyConfigurator.configure(...) methods to read in the configuration information from a Java properties file. The existing configuration is neither cleared nor reset. To do so, call the static BasicConfigurator.resetConfiguration() method before calling this method. This has the potential to make long-term code management more complicated. For starters, stick with one log4j.properties in each application‘s root directory.
- A sample log4j configuration file:
# Set root category priority to DEBUG and set its only appender to A1
log4j.rootCategory=DEBUG, A1
# A1 is set to be a ConsoleAppender (writes to system console).
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
This sample file configures log4j in exactly the same way as the BasicConfigurator.configure() method. But this way, you can later change, for example, DEBUG to FATAL in the properties file and not have to recompile any Java code!
- Another more complex log4j configuration file:
#### Use two appenders, one to log to console, another to log to a file
log4j.rootCategory=debug, stdout, R
# Print only messages of priority WARN or higher for your category
log4j.category.your.category.name=WARN
# Specifically inherit the priority level
#log4j.category.your.category.name=INHERITED
#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller‘s file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#### Second appender writes to a file
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
Here, output will also be appended to the "example.log" file. This file will be rolled over when it reaches 100KB. When roll-over occurs, the old version of example.log is automatically moved to example.log.1. Also available is DailyRollingFileAppender to roll a log file every minute, hour, day, week, month or twice-a-day.
- For long-lived programs such as servers, use
configureAndWatch(String configFilename, long delay_in_milliseconds) to configure using a Java properties file. Log4j will keep monitoring the property file for any changes every so many milliseconds, and if the file changes, Log4j will update the configuration.
- When moving code to production, for speedy performance, you can disable logging wholesale equal to or below a certain priority for all categories in a hierarchy. For example, To log nothing at all ever, specify
log.disable=FATAL in the configuration properties file.
Only exception is if log.disableOverride property is not set to false . So, a system administrator can (temporarily) specify log.disableOverride=true in the configuration file to override log.disable property and log all messages to troubleshoot something.
|