Appuim項目實戰—log4j配置
1. 在maven中配置log4j的依賴
|
在src新建一個log4j.properties內容如下
log4j.rootLogger=DEBUG,console,FILE log4j.rootLogger=info,stdout,tracelog log4j.appender.Root.Encoding=UTF-8 log4j.additivity.org.apache=false log4j.appender.tracelog=org.apache.log4j.FileAppender log4j.appender.tracelog.Encoding=UTF-8 log4j.appender.tracelog.File=./log/project.log log4j.appender.D.Threshold = DEBUG ## è????oDEBUG?o§?????¥????????¥??? log4j.appender.tracelog.layout=org.apache.log4j.PatternLayout log4j.appender.tracelog.layout.ConversionPattern=%d{yyyy-MM-dd-HH:mm:ss}(%F:%L)%m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%-5p%d{yyyy |
實例log
package until; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class logTest { public static Logger logger = Logger.getLogger(logTest.class); public logTest() { PropertyConfigurator.configure("log4j.properties"); } public static void logInfo(Object message) { logger.info(message); } public static void logError(Object message) { logger.error(message); // Assert.assertTrue(false);可以擴展講下,斷言的幾種方式 } public static void logWarn(Object message) { logger.warn(message); } } |
使用log
package until; public class test { public static void main(String[] args) { logTest.logInfo("12"); } } |
效果如圖
項目截圖
Appuim項目實戰—log4j配置