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 ...!

 

 

 

 

  

 

转载于:https://www.cnblogs.com/feika/p/6099796.html

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值