package com.chase.test; import org.apache.log4j.Logger; public class Log4jTest { /** * Log4j */ protected final Logger logger = Logger.getLogger(getClass()); public <T> void testLog(T t) { logger.info(t); } public void end() { logger.info("程序结束!!!"); System.out.println("end+++++"); } public static void main(String[] args) { Log4jTest log4jTest = new Log4jTest(); log4jTest.testLog("程序启动"); log4jTest.testLog(12580); log4jTest.testLog(12580l); log4jTest.end(); } }
输出:
2013-10-14 17:44:40,921 INFO [com.chase.test.Log4jTest] - <程序启动> 2013-10-14 17:44:40,921 INFO [com.chase.test.Log4jTest] - <12580> 2013-10-14 17:44:40,921 INFO [com.chase.test.Log4jTest] - <12580> 2013-10-14 17:44:40,921 INFO [com.chase.test.Log4jTest] - <程序结束!!!> end+++++
log4j.properties配置:
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml! # For all other servers: Comment out the Log4J listener in web.xml to activate Log4J. log4j.rootLogger=info, stdout, logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender
###日志存放位置,默认为程序根目录,具体到某个盘符 log4j.appender.logfile.File=/logs/chase.log log4j.appender.logfile.MaxFileSize=512KB # Keep three backup files. log4j.appender.logfile.MaxBackupIndex=3 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
记得导入:log4j.jar包