The LOG4J API provides the Org.apache.log4j.jdbc.JDBCAppender object, which is capable of logging information in the specified database.
Jdbcappender configuration:
| Property
Description |
BufferSize |
Sets the size of the buffer. Default size is 1 |
Driver |
Sets the driver class to the specified string. If you do not specify a driver class, the default is Sun.jdbc.odbc.JdbcOdbcDriver |
Layout |
Sets the layout to use. The default layout is Org.apache.log4j.PatternLayout |
Password |
Sets the database password. |
Sql |
Specifies that the SQL statement executes every time the event occurs. This could be insert,update or delete |
Url |
Set the JDBC URL |
User |
Set Database user name |
Log table configuration:
Start using a JDBC-based log to create a table where log information is saved. The following is the SQL statement that creates the log table:
CREATE TABLELOGS (user_id VARCHAR( -) not NULL, DATED DATE not NULL, LOGGERVARCHAR( -) not NULL, Level VARCHAR(Ten) not NULL, MESSAGEVARCHAR( +) not NULL );
Example configuration file:
The following is a sample configuration file that will be used to log messages to a log table log4j.properties Jdbcappender
# Define The root logger with Appender Filelog4j.rootlogger=DEBUG, db# Define the DB appenderlog4j.appender.DB=org.apache.log4j.jdbc.jdbcappender# Set jdbc URLlog4j.appender.DB.URL=jdbc:mysql://Localhost/dbname# Set Database Driverlog4j.appender.DB.driver=com.mysql.jdbc.driver# Set Database user name and Passwordlog4j.appender.DB.user=User_namelog4j.appender.DB.password=password# Set The SQL statement to be Executed.log4j.appender.DB.sql=INSERT into LOGS VALUES ('%x ', '%d ', '%c ', '%p ', '%m ') # Define the layout forfile Appenderlog4j.appender.DB.layout=org.apache.log4j.patternlayout
Using the MySQL database, you must use the actual dbname, the user ID, and the database password of the log table created in it. The SQL statement uses the log table name and input values to the table, which requires the INSERT statement to be executed.
Jdbcappender does not require a clearly defined layout. Instead, use Patternlayout to pass it to the SQL statement
If you want to have an XML configuration file equivalent to the above log4j.properties file, you can refer to the content here:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "Log4j.dtd"><log4j:configuration><Appendername= "DB"class= "Org.apache.log4j.jdbc.JDBCAppender"> <paramname= "url"value= "Jdbc:mysql://localhost/dbname"/> <paramname= "Driver"value= "Com.mysql.jdbc.Driver"/> <paramname= "User"value= "user_id"/> <paramname= "Password"value= "Password"/> <paramname= "SQL"value= "INSERT into LOGS VALUES ('%x ', '%d ', '%c ', '%p ', '%m ')"/> <Layoutclass= "Org.apache.log4j.PatternLayout"> </Layout></Appender><Loggername= "Log4j.rootlogger"additivity= "false"> < Levelvalue= "DEBUG"/> <Appender-refref= "DB"/></Logger></log4j:configuration>
Sample program:
The following Java class is a very simple Java application using the LOG4J Log Library example, initialized, and then used.
ImportOrg.apache.log4j.Logger;ImportJava.sql.*;ImportJava.io.*;ImportJava.util.*; Public classlog4jexample{/*Get Actual class name to being printed on*/ StaticLogger log =Logger.getlogger (log4jexample.class. GetName ()); Public Static voidMain (string[] args)throwsioexception,sqlexception{Log.debug ("Debug"); Log.info ("Info"); }}
Compile and run:
Here are the steps to compile and run the above program. Ensure that path and classpath are set appropriately before compiling and executing.
All libraries should be available in path classpath as well as log4j.properties files. So there are the following points:
Create the log4j.properties as shown in.
Create the Log4jexample.java as shown, and compile it.
Executes the log4jexample binary run program.
Now check the dbname database inside the log table and find the following entry (record):
Mysql> Select * fromLOGS;+---------+------------+--------------+-------+---------+| user_id |DATED|LOGGER| Level |MESSAGE|+---------+------------+--------------+-------+---------+| | .- to- - |Log4jexample|DEBUG|Debug|| | .- to- - |Log4jexample|INFO|Info|+---------+------------+--------------+-------+---------+2Rowsinch Set(0.00Sec
Note: Here x is used to generate the line of events for this record threads the associated output of NDC (nested diagnostic context). Use NDC to differentiate client server-side components from processing multiple clients. Check the LOG4J manual for more information.
log4j Tutorial 12, logging to a database