Sunday, September 13, 2009

Redirect Log Messages Of Same Class To Two Different Files

Hi Reader,


 This is gonna be my first post for log4j.
 Well, this is all about to log messages of a class to different log files.
 Here i am giving you a good tricky way to achieve this without creating
 custom logger or level.

 I got this idea from my project. BA provided the given scenario--
 When importing data from XML to database, if data imported successfully, then
 log to success.log and if unable to import then log it to failure.log. And the
 warning level must be kind of warn.

 Well, here i am not going to show the actual code.

  In the following illustration,

    1. MainLog.java -- The main class for which we need to log messages.

    2. MyLog.java -- A very simple class to trick log4j to redirct log to their own file.

    3. log4j.properties -- contains the log4j configuration. created two log file

          a. main.log
          contains log messages of MainLog.java.

          b. custom.log
          contains log messages when myWarn() of MyLog is called in MainLog.java

 In log4j.properties configure two log files and there appender--

    log4j.logger.src.MyWarn=WARN, W
    log4j.appender.W.File=C:\\_T\\custom.log

    log4j.logger.src.MainLog=WARN, R
    log4j.appender.R.File=C:\\_T\\main.log

 In MyLog.java did two things--

    1. Created logger for this class.
    2. Created a static method that simply call LOG.warn(msg).

    note : similarly you can create more methos that contain other level calls like info, error etc.

 In this demonstration, i will show you how to log two messages of same level (here warn) into two different log files.

 Here is the complete code--

------------------------------------------------------------------------------------
MyLog.java tricky class to redirect the log wherever MyLog.myWarn(msg) called

------------------------------------------------------------------------------------

package src;

import org.apache.log4j.Logger;

public class MyLog {

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

public static void myWarn(String msg) {
LOG.warn(msg);
}
}



-------------------------------------------------------------------------------------

MainLog.java the class for which logging is being done.

-------------------------------------------------------------------------------------

package src;

import org.apache.log4j.Logger;

public class MainLog {

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

public static void main(String[] args) {

LOG.warn("Log To main.log");

MyLog.myWarn("Msg To File custom.log");
}
}





-------------------------------------------------------------------------------------

log4j.properties Having looging configuration

-------------------------------------------------------------------------------------


log4j.rootLogger=WARN

# ***** A is set to be a ConsoleAppender.
log4j.appender.A=org.apache.log4j.ConsoleAppender
# ***** A uses PatternLayout.
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:\\_T\\main.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.Append=FALSE
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

log4j.logger.src.MyLog=WARN, W
log4j.logger.src.MainLog=WARN, R

log4j.appender.W=org.apache.log4j.RollingFileAppender
log4j.appender.W.File=C:\\_T\\custom.log
# Control the maximum log file size
log4j.appender.W.MaxFileSize=100KB
log4j.appender.W.Append=FALSE
# Archive log files (one backup file here)
log4j.appender.W.MaxBackupIndex=1
log4j.appender.W.layout=org.apache.log4j.PatternLayout
log4j.appender.W.layout.ConversionPattern=%p %t %c - %m%n




-------------------------------------------------------------------------------------

Hope this will help you.
Any comments/suggestion will be apprecitated.
So what do you need just write.. I am waiting.....


Thanks,
Tanzy. :)

1 comment:

Anonymous said...

Thanks a lot! It is exactly what I need, very helpful.
Boris