Logging to database using java logging framework

This post explains how to log messages to the database. Create a table with below schema

CREATE TABLE logging (
loggername VARCHAR(50) NULL DEFAULT NULL,
threadId INT(11) NULL DEFAULT NULL,
loglevel VARCHAR(50) NULL DEFAULT NULL,
logmessage VARCHAR(50) NULL DEFAULT NULL
)

Create a custom handler which will log the message to the database.

In our example the class name is CustomDatabaseHandler which extends java logging framework provided class StreamHandler.

Below is the complete code

CustomDatabaseHandler


1  package logging;
2  
3  import java.io.IOException;
4  import java.sql.Connection;
5  import java.sql.DriverManager;
6  import java.sql.PreparedStatement;
7  import java.sql.SQLException;
8  import java.util.logging.LogRecord;
9  import java.util.logging.StreamHandler;
10 
11 public class CustomDatabaseHandler extends StreamHandler {
12  private Connection con; 
13  
14  public CustomDatabaseHandler(String fileName) throws IOException {
15      try{  
16          Class.forName("com.mysql.jdbc.Driver");  
17          con = DriverManager.getConnection("jdbc:mysql://localhost:3306/examples","root","root");
18      } catch(SQLException excep) {
19          excep.printStackTrace();
20      } catch(ClassNotFoundException excep) {
21          excep.printStackTrace();
22      }
23  }
24  
25  @Override
26  public synchronized void publish(LogRecord record) {
27      super.publish(record);
28      try {
29          
30          PreparedStatement preparedStatement = con.prepareStatement("insert into logging (loggername, threadId, loglevel, logmessage) values (?,?,?,?)");
31          preparedStatement.setString(1, record.getLoggerName());
32          preparedStatement.setInt(2, record.getThreadID());
33          preparedStatement.setString(3, record.getLevel().getName());
34          preparedStatement.setString(4, record.getMessage());
35          
36          
37          preparedStatement.execute();
38      } catch(SQLException excep) {
39          excep.printStackTrace();
40      }
41  }
42  
43  @Override
44  public synchronized void close() throws SecurityException {
45      super.close();
46      try {
47          con.close();
48      } catch(SQLException excep) {
49          excep.printStackTrace();
50      }
51  }
52 }

We have to override publish and close method as shown in the above code.

The main program is as shown below

Main Code


1  package logging;
2  
3  import java.io.IOException;
4  import java.util.logging.Logger;
5  
6  public class LoggingDemo7 {
7   public static void main(String[] args) throws IOException {
8       Logger logger = Logger.getLogger("logger");
9       CustomDatabaseHandler customDatabaseHandler = new CustomDatabaseHandler("fileName");
10      
11      try {
12          logger.addHandler(customDatabaseHandler);
13          
14          logger.info("Hello my name is Hello World");
15          
16          logger.removeHandler(customDatabaseHandler);
17      } finally {
18          customDatabaseHandler.close();
19      }
20  }
21 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s