log4j无法打印日志

添加依赖

    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-apiartifactId>
        <version>2.9.1version>
    dependency>
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-coreartifactId>
        <version>2.9.1version>
    dependency>

    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-slf4j-implartifactId>
        <version>2.9.1version>
    dependency>

日志配置文件

log4j2.xml 位于reources目录下


<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        Console>
    Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        Root>
    Loggers>
Configuration>

使用

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Description: 发送短信工具类测试 
* * @author zhaofeng * @version 1.0 * @date: 2019/8/23 10:11 * @since JDK 1.8 */
public class SmsSenderUtilTest { private static Logger logger = LoggerFactory.getLogger(SmsSenderUtilTest.class); @Test public void testLog() { logger.info("测试打印日志:{}", System.currentTimeMillis()); } }

运行结果

18:22:34.425 [main] INFO  cn.analysys.ems.jar.SmsSenderUtilTest - 测试打印日志:1566555754410

Process finished with exit code 0

参考文章

聊一聊log4j2配置文件log4j2.xml

你可能感兴趣的:(后端)