1
import
java.util.
*
;
2 import java.io. * ;
3 import org.apache.log4j.Logger;
4 import org.apache.log4j.BasicConfigurator;
5 import org.apache.log4j.PropertyConfigurator;
6 import org.apache.log4j.Priority;
7
8 public class TestLog4j
9 {
10 protected TestLog4j ()
11 {
12 String path = System.getProperty ( " user.dir " , " . " );
13 System.out.println( " !!!! " + path);
14 path += " \\..\\source\\TestLog4j\\log4j.properties " ;
15 System.out.println ( " ??? " + path);
16 PropertyConfigurator.configure (path);
17 Logger log = Logger.getLogger (getClass().getName ());
18 log.info ( " test in TestLog4j.java " );
19 // return logger;
20 }
21 }
22
2 import java.io. * ;
3 import org.apache.log4j.Logger;
4 import org.apache.log4j.BasicConfigurator;
5 import org.apache.log4j.PropertyConfigurator;
6 import org.apache.log4j.Priority;
7
8 public class TestLog4j
9 {
10 protected TestLog4j ()
11 {
12 String path = System.getProperty ( " user.dir " , " . " );
13 System.out.println( " !!!! " + path);
14 path += " \\..\\source\\TestLog4j\\log4j.properties " ;
15 System.out.println ( " ??? " + path);
16 PropertyConfigurator.configure (path);
17 Logger log = Logger.getLogger (getClass().getName ());
18 log.info ( " test in TestLog4j.java " );
19 // return logger;
20 }
21 }
22
log4j的使用非常广泛。在这个程序中程序读取配置文件,配置文件内容如下:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
#log4j.rootLogger
=
DEBUG, A1
2 log4j.rootLogger = INFO, A1
3 #log4j.appender.A1 = org.apache.log4j.ConsoleAppender
4 log4j.appender.A1 = org.apache.log4j.FileAppender
5 #可以滚动
6 log4j.appender.A1 = org.apache.log4j.RollingFileAppender
7 log4j.appender.A1.File = c:\\example.log
8 #到500k则将新建gisWS1.log 依此类推
9 log4j.appender.A1.MaxFileSize = 500KB
10 #最多允许建立10个这样的日志文件,当gisWS10.log满500k 则重新覆盖example1.log
11 log4j.appender.A1.MaxBackupIndex = 10
12 log4j.appender.A1.layout = org.apache.log4j.PatternLayout
13 #log4j.appender.A1.layout.ConversionPattern = %- 4r [ % t] %- 5p % c % x - % m % n
14 log4j.appender.A1.layout.ConversionPattern = [ % d] [ % t] % m % n
2 log4j.rootLogger = INFO, A1
3 #log4j.appender.A1 = org.apache.log4j.ConsoleAppender
4 log4j.appender.A1 = org.apache.log4j.FileAppender
5 #可以滚动
6 log4j.appender.A1 = org.apache.log4j.RollingFileAppender
7 log4j.appender.A1.File = c:\\example.log
8 #到500k则将新建gisWS1.log 依此类推
9 log4j.appender.A1.MaxFileSize = 500KB
10 #最多允许建立10个这样的日志文件,当gisWS10.log满500k 则重新覆盖example1.log
11 log4j.appender.A1.MaxBackupIndex = 10
12 log4j.appender.A1.layout = org.apache.log4j.PatternLayout
13 #log4j.appender.A1.layout.ConversionPattern = %- 4r [ % t] %- 5p % c % x - % m % n
14 log4j.appender.A1.layout.ConversionPattern = [ % d] [ % t] % m % n
配置文件的含义如注释所言。
log4j使用了单态设计模式,所以在同一个项目,的另外一个程序中也要打印日志。直接可以用:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
Logger logger
=
Logger.getLogger (Main.
class
.getName ());
2 logger.debug ( " Start of the main() in TestLog4j " );
3 logger.info ( " Just testing a log message with priority set to INFO " );
4 logger.warn ( " Just testing a log message with priority set to WARN " );
5 logger.error ( " Just testing a log message with priority set to ERROR " );
6 logger.fatal ( " Just testing a log message with priority set to FATAL " );
7 logger.log (Priority.WARN, " Testing a log message use a alternate form " );
8 logger.debug (TestLog4j. class .getName ());
这样日记配置全局起作用,日志也写到一个地方的。
2 logger.debug ( " Start of the main() in TestLog4j " );
3 logger.info ( " Just testing a log message with priority set to INFO " );
4 logger.warn ( " Just testing a log message with priority set to WARN " );
5 logger.error ( " Just testing a log message with priority set to ERROR " );
6 logger.fatal ( " Just testing a log message with priority set to FATAL " );
7 logger.log (Priority.WARN, " Testing a log message use a alternate form " );
8 logger.debug (TestLog4j. class .getName ());
虽然随JDK版本的升高,已经带有非常好用的日志记录API类,但是log4j的使用已经深入人心。掌握log4j的使用是非常有必要的。