We can configure log4j using both property file as well as xml file. Today we will look into log4j.xml example and get the details of log4j.xml configuration.
Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged; Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined; Loggers: this element contains a list of Logger instances. File Logging; Custom Pattern; Rotate File On Size; Rotate File Per Day; Rotate File Per Hour/Minute; Rotate & Archiving File On Size; Rotate & Archiving File Per Day; Rotate & Archiving File Per Hour/Minute; Multiple Appender; Multiple Logger; Append Config; Additivity Config. Test ConsoleAppender Configuration Let’s write a quick java program and write the logs in console using above configuration. Now put the log4j.properties file in project root folder or resources folder as per your need – and run the application. You will get below log message in console.
- Log4j Appender Properties Configuration A simple example showing appenders configuration through property file. It’s defining all the above xml based configuration.
- Log4j provides the ability to 'advertise' appender configuration details for all file-based appenders as well as socket-based appenders. For example, for file-based appenders, the file location and the pattern layout in the file are included in the advertisement.
- Here is a sample configuration for the JPAAppender. The first XML sample is the Log4j configuration file, the second is the persistence.xml file. EclipseLink is assumed here, but any JPA 2.1 or higher provider will do. You should always create a separate persistence unit for logging, for two reasons.
Table of Contents
- 1 log4j.xml
log4j.xml
Here is a typical log4j.xml example file.
If you are using Eclipse, you will notice that it’s not showing you XML element options, this is because it’s not able to find the
log4j.dtd
file. Something like below image.Change the DOCTYPE declaration to below to fix this. You would need internet connection to validate the log4j.xml file for this.
log4j.xml example configuration attributes
Let’s look at the log4j.xml example configuration attributes –
threshold
and debug
.The “threshold” attribute takes the minimum level below which all logging statements are disabled. This overrides all logger level configurations, use this only when you want to disable some lower level logging for all the loggers. In above case, all the trace and debug logs will be disabled even if any of the logger level is defined as debug.
The “debug” attribute is used to switch the printing of internal log4j logging statements. You should keep it false to avoid bulk logging by log4j framework. If you will change it to true, you will get some logs like below.
log4j.xml example – appender
The next important part is appender element. Here we define logging strategy, we can have multiple appenders defined in log4j.xml configuration.
Every appender has a distinct name and class to be used for logging. Then we set the parameters used by the appender class, every class has it’s own set of parameter that can be defined.
For example, “Target” is parameter in
ConsoleAppender
whereas “File”, “MaxFileSize”, “MaxBackupIndex” are parameters of RollingFileAppender
.log4j.xml example – layout
Next part is layout where we define logging pattern through
PatternLayout
parameter ConversionPattern
. This gets prefixed to all the logging messages. In above file appender, we are prefixing log messages with date, thread name, class name and line number. Below is a sample output for this pattern:log4j xml configuration filter and logger
Next is the filter section where we can define our own custom filters or use any existing filters, for more details read log4j filters.
Next part is logger, we can have multiple loggers too. The “name” attribute is used to define the package where this logger will be used. Note that if there are multiple logger matches to a class, then most specific one is used.
log4j xml additivity
The “additivity” attribute is a very important one – if it’s true then logging goes through hierarchy. For example if we have loggers defined as below.
And we are logging from some class in
com.journaldev.log4j.model
package, then it will be using appenders “file”, “jdbc” and “console” because of logger hierarchy. This can generate a lot of redundant logs, so we usually keep it false. It’s default value is “true” though.A logger can use multiple appenders too.
log4j xml root logger
Finally root logger needs to be defined, this is the default level and appenders being used if there are no logger match found.
Log4j File Appender Configuration Key
Before I conclude this tutorial, one more point is that log4j framework looks for
log4j.xml
or log4j.properties
file in the classpath. If you are using some different name for these configuration files then you need to configure them before using it. For standalone java programs, you can do it in main method before program starts execution. For web applications, you can do it through Servlet Context Listener classes.
Log4j2 Rolling File Appender Configuration
References: Download adobe after effects cs6 32 bit full crack.