log4j2+slf4j+junit
dependencies:
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21' compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7' compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.7' compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.7' compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: '2.7' testCompile("junit:junit")
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <!-- 输出到控制台 --> <Console name="Console" target="SYSTEM_OUT"> <!-- 需要记录的级别 --> <!-- <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" /> --> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n" /> </Console> <!-- 输出到文件,按天或者超过80MB分割 --> <RollingFile name="RollingFile" fileName="logs/xxx.log" filePattern="logs/$${date:yyyy-MM}/xjj-%d{yyyy-MM-dd}-%i.log.gz"> <!-- 需要记录的级别 --> <!-- <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" /> --> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n" /> <Policies> <OnStartupTriggeringPolicy /> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="80 MB" /> </Policies> </RollingFile> </Appenders> <Loggers> <Root level="trace"> <!-- 全局配置 --> <AppenderRef ref="Console" /> <AppenderRef ref="RollingFile"/> </Root> <!-- 为sql语句配置特殊的Log级别,方便调试 --> <Logger name="mars" level="info" additivity="false"> <AppenderRef ref="Console" /> </Logger> </Loggers> </Configuration>
java sample code:
package mars; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by szhaoyu on 16/11/24. */ public class MarsLogTest { Logger logger = LoggerFactory.getLogger(MarsLogTest.class); @Test public void testLog() { logger.info("start logging ...."); logger.error("o...meet a warnning here ..."); logger.debug("Ok. I'm debugging here.."); logger.warn("warning timeout ...!"); } }
output:
2016-11-24 22:23:21,060:INFO Test worker (MarsLogTest.java:15) - start logging .... 2016-11-24 22:23:21,065:ERROR Test worker (MarsLogTest.java:16) - o...meet a warnning here ... 2016-11-24 22:23:21,065:WARN Test worker (MarsLogTest.java:18) - warning timeout ...!