2006/03/24

Log4j example

**here has four examples about how to utilize log4j.

the core of log4j is log4j.properties, we can do some configuration in this property files, including log message format, output file name, appender

setting and so on.

1. com.ptc.demo.log4j.module1.Module1Test: write log information to console
2. com.ptc.demo.log4j.module2.Module2Test: write log information to log files and rotate the log file by day
3. com.ptc.demo.log4j.module3.Module3Test: write log information to log files and rotate the log file by file size
4. com.ptc.demo.log4j.module4.Module4Test: write log information to log file (in HTML format)


---------------------
output message
---------------------
2006-03-24 13:50:03,015 DEBUG com.ptc.demo.log4j.module1.Module1Test.sortByAsc(Module1Test.java:26) - [ant, cat, dog, dophin, tiger, zebra]
2006-03-24 13:50:03,015 DEBUG com.ptc.demo.log4j.module1.Module1Test.sortByDesc(Module1Test.java:41) - [zebra, tiger, dophin, dog, cat, ant]



log4j.properties
=========================================================================
#module1 -- output to console
log4j.category.com.ptc.demo.log4j.module1=DEBUG,module1
log4j.appender.module1=org.apache.log4j.ConsoleAppender
log4j.appender.module1.layout=org.apache.log4j.PatternLayout
log4j.appender.module1.layout.ConversionPattern=%d %5p %l - %m%n


#module2 -- output to log and rotate by day
log4j.category.com.ptc.demo.log4j.module2=DEBUG,module2
log4j.appender.module2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.module2.threshold=debug
log4j.appender.module2.File=C:\module2.log
log4j.appender.module2.DatePattern='.'yyyyMMdd
log4j.appender.module2.Append=true
log4j.appender.module2.layout=org.apache.log4j.PatternLayout
log4j.appender.module2.layout.ConversionPattern=%d %5p %l - %m%n


#module3 -- output to log and rotate by file size
log4j.category.com.ptc.demo.log4j.module3=DEBUG,module3
log4j.appender.module3=org.apache.log4j.RollingFileAppender
log4j.appender.module3.threshold=debug
log4j.appender.module3.File=C:\module3.log
#rotate size = 500 KB
log4j.appender.module3.MaxFileSize=500KB
#keep three backup files
log4j.appender.module3.MaxBackupIndex=3
log4j.appender.module3.layout=org.apache.log4j.PatternLayout
log4j.appender.module3.layout.ConversionPattern=%d %5p %l - %m%n


#module4 -- output to log and by HTML format
log4j.category.com.ptc.demo.log4j.module4=DEBUG,module4
log4j.appender.module4=org.apache.log4j.FileAppender
log4j.appender.module4.File=C:\module4.html
log4j.appender.module4.Append=true
log4j.appender.module4.layout=org.apache.log4j.HTMLLayout
=========================================================================


class file (each class file has the same content and only has different class name)
=========================================================================
package com.ptc.demo.log4j.module1;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Collections;

import org.apache.log4j.Logger;

/**
*
* @author Albert
*
*/
public class Module1Test {

private static Logger logger = Logger.getLogger(Module1Test.class.getName());

/**
* sorting by ascending
* @param str
*/
public void sortByAsc(String str[]){
List list = Arrays.asList(str);
Collections.sort(list);
logger.debug(list);
}

/**
* sorting by descending
* @param str
*/
public void sortByDesc(String str[]){
List list = Arrays.asList(str);
Collections.sort(list, new Comparator(){
public int compare(Object obj1, Object obj2){
return ((String)obj2).compareTo((String)obj1);
}
}
);
logger.debug(list);
}

/**
* @param args
*/
public static void main(String[] args) {
Module1Test module1Test = new Module1Test();

String str[] = new String[]{"dog","ant","zebra","cat","tiger","dophin"};
module1Test.sortByAsc(str);
module1Test.sortByDesc(str);
}

}
=========================================================================

No comments: