Tuesday, November 10, 2015

send log messages to different log file

We often come across to situation where we need to spearate our log based on different scenario.
Here i will explain how to create another log file and use it for logging.
we can do this in two way either using xml or using properties. xml is better approach.

Using log4j.properties
cretae a log4j.properties file inside your resources folder of your project and paste below properties

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender

#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/ekf.log C:\\EKF.log

log4j.appender.file.File=${catalina.home}/logs/Sample.log
log4j.appender.file.MaxFileSize=1024MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


As we have use org.apache.log4j so we need to add the respecive dependency in our pom.xml

               <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>

</dependency>

Now all you to do is, write below code in class which logging message you want to send to Sample.log

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

final static Log logger = LogFactory.getLog(Test.class);

As in our properties we have defined INFO as rootLogger so when you use

logger.info("test your logger");

this inside your method log message will come under your Sample.log.

the time you will deploy your portlet that time itself it will create Sample.log file inside tomcat/log folder of your application.
And if you have use final static Log logger = LogFactory.getLog(Test.class); inside your class for logging all the log message will go to Sample.log

Using log4j.xml
cretae a log4j.xml file inside your resources folder of your project and paste below xml code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="file" class="org.apache.log4j.RollingFileAppender">
  <param name="append" value="false" />
  <param name="maxFileSize" value="10KB" />
  <param name="maxBackupIndex" value="5" />
  <!-- For Tomcat -->
  <param name="file" value="${catalina.home}/logs/XMLBaseSample.log" />
  <layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" 
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
  </layout>
</appender>

<root>
<level value="INFO" />
<appender-ref ref="file" />
</root>

</log4j:configuration>

add below dependency in your pom.xml

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

</dependency>


in this case it will create different file as  XMLBaseSample.log inside tomcat/log folder of your application.

in your class write below code to send logging messages to XMLBaseSample.log file

import org.apache.log4j.Logger;

private static Logger LOG = Logger.getLogger(Test.class);

and in class if  you have use this logger

private static Logger LOG = Logger.getLogger(Test.class);

and inside method if you use logger.info("test your logger using xml based approach");
 then you will see this log message inside XMLBaseSample.log

i have use different logger in both the case just to explain that we can achieve it with many ways.



0 comments:

Post a Comment