1. 程式人生 > >Appuim項目實戰—log4j配置

Appuim項目實戰—log4j配置

otl gtest nco 新建 cor err args odin void

1. 在maven中配置log4j的依賴

  1. <!-- 配置log4j依賴 -->
  2. <dependency>
  3. <groupId>log4j</groupId>
  4. <artifactId>log4j</artifactId>
  5. <version>1.2.17</version>
  6. </dependency>

在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

-MM-dd-HH:mm:ss}(%F:%L)%m%n

實例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配置