Open Source RDBMS - Seamless, Scalable, Stable and Free

English | Login |Register

Store Java Logs to Database Using log4j

log4j is one of the most powerful and commonly used logging system used when programming in Java. There are many different ways how you can utilize it in your project. One of the features of log4j framework is to store Java log messages to a JDBC-enabled database. Here we will show how to store the logs into CUBRID Database. Since CUBRID provides the JDBC interface, configuration log4j is pretty straightforward.

Downloads

Example

Before we get into configurations, let's create a sample code in Java.

  • Create a new directory where for this example.
  • Copy two libraries to this directory:
    1. log4j-1.2.16.jar ? This is the main log4j library. You can find it inside the log4j archive you have downloaded above.
    2. cubrid_jdbc.jar ? This is the main CUBRID JDBC driver. You can find it in the /jdbc directory which is located inside the main directory where you have installed CUBRID.
  • Now let's create a simple Java class that does some logging. Open your editor and create a new file called Tutorial.java.
  • import org.apache.log4j.Logger;
    import org.apache.log4j.LogManager;
    
    public class Tutorial {
        public static void main( String[] args ) {
            Logger log = LogManager.getLogger(Tutorial.class);
            log.info("Application successfully initialized");
            log.warn("Application is about to finish");
            try {
                if ( 1 / 0 == 0 ) {
                    System.out.println("Fry the FPU");
                }
            } catch( Exception ex) {
                log.error("Some exception has been thrown", ex);
            }
            log.fatal("Application has finished");
        }
    }
    

Configure log4j

In order to enable log4j we will have to create a configuration file. In this configuration we will define three appenders: Console, File and JDBC. This way, the messages will be written to all these appenders. Open text editor and put the following configuration (note that CUBRID JDBC specific code is written in bold). Save as log4j.properties in the same directory with Java file.

log4j.rootCategory=DEBUG, Console, File, CUBRID

# Console appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n

# File
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.file=tutorial.log
log4j.appender.File.MaxFileSize=100KB
log4j.appender.File.layout=org.apache.log4j.PatternLayout

# CUBRID Database
log4j.appender.CUBRID = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.CUBRID.driver = cubrid.jdbc.driver.CUBRIDDriver
log4j.appender.CUBRID.user = dba
log4j.appender.CUBRID.password =
log4j.appender.CUBRID.URL = jdbc:cubrid:localhost:30000:demodb:public::
log4j.appender.CUBRID.sql = INSERT INTO LOGS VALUES('%x','%d{yyyy-MM-dd  HH:mm:ss.SSS}','%C','%p','%m')
log4j.appender.CUBRID.layout=org.apache.log4j.PatternLayout

As you can see, configuring the JDBC appender requires specifying JDBC URL and also the connection credentials. Additionally we have set the format of the timestamp into log4j.appender.CUBRID.sql in order to create a database compatible DATETIME value.

Next, before we can use the JDBC logging with CUBRID we will have to create a new table to store the logs. By default, CUBRID comes with a sample database named demodb accessible to dba user with an empty password. We will use this database to store our table for logging. Open up the command line terminal and execute the following SQL statement using csql utility to create our table. You can, in fact, achieve the same results if you use the CUBRID Manager, CUBRID's main GUI-based database administration tool.

csql -u dba -c 'CREATE TABLE logs (user_id VARCHAR(20) NOT NULL, dated DATETIME NOT NULL, logger VARCHAR(50) NOT NULL, "level" VARCHAR(10) NOT NULL, message VARCHAR(1000) NOT NULL);' demodb

Now compile your Java source code into Java .class with the following command. This will output the Tutorial.class file.

javac -cp log4j-1.2.16.jar Tutorial.java

Then run the Tutorial.class.

java -cp .:log4j-1.2.16.jar:cubrid_jdbc.jar Tutorial

You will see the following output.

log4j-java-class-output.png

Let's look into our CUBRID Database Table. With csql utility select all records from our table.

csql -u dba -c "SELECT * FROM logs" demodb

You will see the following results.

log4j-log-select.png

Setting up log4j with CUBRID is very simple and straightforward as you can see. However, log4j by itself has one drawback - it does not log Java exceptions into the JDBC appender. If you navigate to log4j website, it states: "WARNING: This version of JDBCAppender is very likely to be completely replaced in the future. Moreoever, it does not log exceptions." So, we recommend you to consult the documentation before you decide to use the JDBC appender with any database.

See also

Getting Help

If you have any difficulties with log4j, post your question to CUBRID Forum at http://www.cubrid.org/?mid=forum&category=195533.

See also

Tutorials :: Analyzing JDBC Logs with LOG4JDBC

First, let's clarify what exactly this log4jdbc is useful for. Assume you work on a project written in Java which communicates with CUBRID JDBC (or an...

Tutorial :: Clj-DBCP Clojure Library for Connecting to CUBRID Database

Clj-DBCP is a Java-6/Clojure library to create a database connection pool, which is known to be a more efficient way to connect to multiple databases ...

CUBRID JDBC Tutorial (for CUBRID 8.3.1 and below)

You may write CUBRID powered applications in a variety of languages. The languages that probably most people use to write CUBRID applications are PHP ...




You are either using a very old browser or a browser that is not supported.
In order to browse cubrid.org you need to have one of the following browsers:



Internet Explorer: Mozilla Firefox: Google Chrome: