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:

-- ::,:INFO Test worker (MarsLogTest.java:) - start logging ....
-- ::,:ERROR Test worker (MarsLogTest.java:) - o...meet a warnning here ...
-- ::,:WARN Test worker (MarsLogTest.java:) - warning timeout ...!

  

05-28 18:59