December 2013 Posts

How to Log Log4J’s Message Logs Into Database

Today im going to do a simple log4j logging into mysql database, the only reason i want to log into database instead of into file is so that i could query the logs i have.

This is my java class that i use to do my testcase,

package com.edw.main;

import java.io.IOException;
import java.sql.SQLException;
import org.apache.log4j.Logger;

public class Main {

    private static Logger logger = Logger.getLogger(Main.class);

    private void doSomething() {
        logger.debug("im doing something");
        logger.error("im doing something - error -");
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.doSomething();
    }
}

and my simple table, to store all my log messages

CREATE TABLE `logs` (
  `thread_id` varchar(20) NOT NULL,
  `tanggal` datetime NOT NULL,
  `kelas` varchar(50) NOT NULL,
  `level` varchar(10) NOT NULL,
  `pesan` varchar(1000) NOT NULL,
  `ID` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

This is my log4j.properties,

# Define the root logger with appender file
log4j.rootLogger = DEBUG, DB

# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender

# Set JDBC URL
log4j.appender.DB.URL=jdbc:mysql://localhost/test

# Set Database Driver
log4j.appender.DB.driver=com.mysql.jdbc.Driver

# Set database user name and password
log4j.appender.DB.user=root
log4j.appender.DB.password=password

# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO LOGS VALUES('%x',now(),'%C:%L','%p','%m', null)

# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

And this is what the contents of my database

This is my Netbeans; Project structure, as you can see, i only need 2 jars, mysql driver and log4j jar.

Google+

How to Log X-Forwarded-For HTTP Header on Glassfish Application Server

Yesterday i was talking with my friend, on how Glassfish Application Server able to log the http request’s original user ip. Because my Glassfish is behind Apache ModProxy, so what is logging om my Glassfish’s access log file is my proxy’s ip address.

My apache modproxy ip is 192.168.56.102, while my original ip is 192.168.56.101. So im planning to see “192.168.56.101″ on my Glassfish access log instead of “192.168.56.102″ which is my proxy’s ip.

This is my Access Logging screenshot,

But before you do that, please make sure, you checked the Access Logging checkbox

And this is the result of my Glassfish’s Access Log File, which is located at, <glassfish installation folder>/glassfish/domains/domain1/logs/access

"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:40:11 +0700" "GET // HTTP/1.1" 200 563
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:40:12 +0700" "GET // HTTP/1.1" 200 563
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:40:12 +0700" "GET // HTTP/1.1" 200 563
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:40:13 +0700" "GET // HTTP/1.1" 200 563
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:40:13 +0700" "GET // HTTP/1.1" 200 563
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:40:13 +0700" "GET // HTTP/1.1" 200 563
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:40:13 +0700" "GET // HTTP/1.1" 200 563
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:40:13 +0700" "GET // HTTP/1.1" 200 563

As you can see, what is written on my access log is my proxy ip address instead of my original address.

After spending some time researching, i found out that Apache ModProxy have an “X-Forwarded-For” http header which contain the original user’s ip address. So this is my new Access Logging Format, you can see me logging “X-Forwarded-For” header on the end of my new logging format.

%client.name% %auth-user-name% %datetime% %request% %status% %response.length% %header.X-Forwarded-For%

And this is my lates access log file, you can see my original ip at the end of every access log.

"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:51:56 +0700" "GET // HTTP/1.1" 200 563 "192.168.56.101"
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:51:56 +0700" "GET // HTTP/1.1" 200 563 "192.168.56.101"
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:51:56 +0700" "GET // HTTP/1.1" 200 563 "192.168.56.101"
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:51:56 +0700" "GET // HTTP/1.1" 200 563 "192.168.56.101"
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:51:56 +0700" "GET // HTTP/1.1" 200 563 "192.168.56.101"
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:51:57 +0700" "GET // HTTP/1.1" 200 563 "192.168.56.101"
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:51:57 +0700" "GET // HTTP/1.1" 200 563 "192.168.56.101"
"192.168.56.102" "NULL-AUTH-USER" "20/Oct/2013:15:52:05 +0700" "GET // HTTP/1.1" 200 563 "192.168.56.101"

Hope it’d help others, have fun ;-)

Google+