Wednesday, May 27, 2015

LOG4J XML to keep dated file appender for multiple debug logs, error logs

Bellow log4j XML file will help you to keep multiple dated debug logs and error logs

Prerequisites :     1. Create Java Project in your IDE
                            2. Use  biz.minaret and Log4J java libraries
                            3. Create log4j.xml file with bellow XML
                            4. Keep log4j.xml file in your project build path
                            5. You have to create java code with loggers for 2 debug logs and error log.



How it works : Once you added log4j  libraries  in to  your  build  path it will automatically find log4j.xml or log4j.properties in your build path. It will create folders called log-update , log-delete and log-error  in your build path.

                      
log4j.xml                         

Download it from here : log4j.xml


Java code

public class LoggerTest{
    private static final Logger updateLog = Logger.getLogger("appUpdateLog");    
    private static final Logger deleteLog = Logger.getLogger("appDeleteLog");
    private static final Logger errorsLog = Logger.getLogger("appErrorsLog");

    public static void main(String[] str) {
    do{
            deleteLog.debug("Adding a delete log record....");
            Thread.sleep(1000);
            updateLog.debug("Adding an update log record....");            

            String stringTmp = null;
            try{
                 stringTmp.split("");
             }catch(Exception e){
                 errorsLog.error("
Adding an error log record with stack trace....",e);
             }

        }while(true);      
    }
}





Thanks