Friday, 22 July 2016

Use log4j to log content on remote server

Many a times there is a requirement that we need to check the log details of a java program running on a different machines. Most of the time we are dependent on the availability of the machine where java program is running to see and understand actual error (to get local log file, generated on a specific machine). Instead of doing that we can use log4j to capture the live logs generated by java application running on different machines. That way we can independently monitor logs instead of waiting for someone to send us a local log file generated locally.

To achieve remote logging we can make use of "org.apache.log4j.net.SimpleSocketServer" class of log4j.

Here are the steps we need to follow to perform remote logging.

1) Add log4j dependency in pom.xml file
2) Configure log4j-server.properties
3) Start "SimpleSocketServer" to listen remote logging events
4) Configure log4j.properties at client program to specify where log events should be captured
5) Use "org.apache.log4j.Logger" class to add logs
6) Run and verify output

Here are the steps in detail to capture logs on remote server:

1) Add log4j dependency in pom.xml file

In our example we are going to make use of log4j-1.2.17.jar file to start our "SimpleSocketServer" server.

Since our demo application we are going to create in maven we will use below dependency in pom.xml file to automatically download log4j-1.2.17.jar file.

<dependency> 
<groupId>log4j</groupId> 
<artifactId>log4j</artifactId> 
<version>1.2.17</version> 
</dependency>

If you like to use ant to build your project then manually you can download log4j-1.2.17.jar file and add jar file in project's build path.

2) Configure log4j-server.properties

When we start our "SimpleSocketServer" we need to inform to "SimpleSocketServer" that how socket server should store the log events.

In our application log4j-server.properties file looks like this:

#Two arguments we need to specify in below line 1st logger level (DEBUG) in our case, 2nd appender name via which we configure other properties in our case (file)
log4j.rootLogger=DEBUG, file

#Define how the socket server should store the log events
log4j.appender.file=org.apache.log4j.RollingFileAppender

#Here is the name of the file which will capture the log events
log4j.appender.file.File=centralized-error.log

#Below two properties specifies that what should be the maximum size of a log file and up to how many files we need to capture
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=10

#Below properties are used to specify the format of the log events.
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSSS} %p %C %x = %m%n
#log4j.appender.file.layout.ConversionPattern=[%d] [%t] [%m]%n


3) Start "SimpleSocketServer" to listen remote logging events

Now, we can start our "SimpleSocketServer" with few input to it. Below are the inputs we need to provide:

1) Port number : 4713 (in our example)
2) configuration file to "SimpleSocketServer" : log4j-server.properties (in our example)

To start "SimpleSocketServer" we need to put "log4j-1.2.17.jar" and "log4j-server.properties" to a project directory called "C:/remoteloggingexample".

Now we can start our "SimpleSocketServer" as below:

C:/remoteloggingexample>java -classpath log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer 4713 log4j-server.properties

4) Configure log4j.properties at client program to specify where log events should be captured

So far we are all set with the server side setup. Now, we need to configure properties at the client side / in our java application.

#Define the log4j configuration for local application
log4j.rootLogger=ALL, server

#We will use socket appender
log4j.appender.server=org.apache.log4j.net.SocketAppender

#Port where socket server will be listening for the log events
log4j.appender.server.Port=4713

#Host name or IP address of socket server
log4j.appender.server.RemoteHost=192.168.10.132


5) Use "org.apache.log4j.Logger" class to add logs

Lets create a dummy program to verify that events are being captured in the log file or not.

import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.apache.log4j.PropertyConfigurator;

public class StudentDemo {

static Logger logger = Logger.getLogger(StudentDemo.class);

static {
PropertyConfigurator.configure("log4j.properties");
}

public static void main(String[] args) {
// These logs will be sent to socket server as configured in log4j.properties
NDC.push("student1 : " + args[0]);
for (int i = 1; i <= 10; i++) {
logger.debug("Student loop started \t" + i);
}
}

}

Note: NDC.push("student1 : " + args[0]); is used to uniquely identify the details of the java application. Since at remote location log file is going to be common across all the places where our java application runs. NDC.push statement helps us to filter the log records.

6) Run and verify output

Once we run the client program we can see that all the logs are being stored on the remote server in our case "192.168.10.132" and a file named "centralized-error.log" will be generated on the remote server which we specified in the "log4j-server.properties" server property file. All the logs which are logged by client program i.e. "StudentDemo" program in our case would be available in "centralized-error.log" file.

We are done!

Cheers!
Henal Saraiya

No comments:

Post a Comment