- log4j Tutorial
- I use log4j and would like to route the output of certain Loggers to specific files. I already have multiple appenders in place. Now, to make debugging easier, I want to tell log4j that the output.
- RequiresLayout boolean requiresLayout Configurators call this method to determine if the appender requires a layout. If this method returns true, meaning that layout is required, then the configurator will configure an layout using the configuration information at its disposal.If this method returns false, meaning that a layout is not required, then layout configuration will be skipped even.
- In this example you will see how to create a console and file appender and add it to the rootLogger. The log4j.properties file is shown below.
- Stackify was founded in 2012 with the goal to create an easy to use set of tools for developers to improve their applications. Now over 1,200 organizations in nearly 60 countries rely on Stackify’s tools to provide critical application performance and code insights so they can deploy better applications faster.
- log4j Useful Resources
- Selected Reading
RequiresLayout boolean requiresLayout Configurators call this method to determine if the appender requires a layout. If this method returns true, meaning that layout is required, then the configurator will configure an layout using the configuration information at its disposal.
The log4j API provides the org.apache.log4j.jdbc.JDBCAppender object, which can put logging information in a specified database.
JDBCAppender Configuration
Property | Description |
---|---|
bufferSize | Sets the buffer size. Default size is 1. |
driver | Sets the driver class to the specified string. If no driver class is specified, it defaults to sun.jdbc.odbc.JdbcOdbcDriver. |
layout | Sets the layout to be used. Default layout is org.apache.log4j.PatternLayout. |
password | Sets the database password. |
sql | Specifies the SQL statement to be executed every time a logging event occurs. This could be INSERT, UPDATE, or DELETE. |
URL | Sets the JDBC URL. |
user | Sets the database user name. |
Log Table Configuration
Before you start using JDBC based logging, you should create a table to maintain all the log information. Following is the SQL Statement for creating the LOGS table −
Sample Configuration File
Following is a sample configuration file log4j.properties for JDBCAppender which will is be used to log messages to a LOGS table.
For MySQL database, you would have to use the actual DBNAME, user ID and password, where you have created LOGS table. The SQL statement is to execute an INSERT statement with the table name LOGS and the values to be entered into the table.
JDBCAppender does not need a layout to be defined explicitly. Instead, the SQL statement passed to it uses a PatternLayout.
If you wish to have an XML configuration file equivalent to the above log4j.properties file, then here is the content −
Sample Program
The following Java class is a very simple example that initializes and then uses the Log4J logging library for Java applications.
Compile and Execute
Here are the steps to compile and run the above-mentioned program. Make sure you have set PATH and CLASSPATH appropriately before proceeding for compilation and execution.
All the libraries should be available in CLASSPATH and your log4j.properties file should be available in PATH. Follow the given steps −
- Create log4j.properties as shown above.
- Create log4jExample.java as shown above and compile it.
- Execute log4jExample binary to run the program.
Log4j Appender Class 9
Now check your LOGS table inside DBNAME database and you would find the following entries −
Log4j Appender Log Class
Note − Here x is used to output the Nested diagnostic Context (NDC) associated with the thread that generated the logging event. We use NDC to distinguish clients in server-side components handling multiple clients. Check Log4J Manual for more information on this.