关注各种黑科技
B站UP主,编程教学,游戏玩家,挂B,金牛座

Java入坑day07 Logger安装测试

老项目用的log4j,不过网上推荐logback,拿过来试试

在~/Projects/java/hello/src/test/java
里建了一个包com.devecho idea会自动创建层级目录
在里面建一个LoggerTest java class,会自动加上包名

package com.devecho;

import org.junit.Test;

public class LoggerTest {
    @Test
    public void test1(){
    }
}

idea 可以自动用opt+enter 弹出框自动添加mvn依赖


可能是我mvn源的问题 最新版本4.12没列出来

                <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

scope 可以限制作用域
https://mvnrepository.com/artifact/junit/junit
https://github.com/junit-team/junit4/wiki/Download-and-Install

输出log
依赖

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>

注意只添加slf4j-api artifact 还不行 还要加slf4j-log4j12或slf4j-sample,version到mvnrepository.com搜最新的就行,不写version可以,但是idea的mvn侧边栏会显示unkown错误

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”

package com.devecho;

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

public class LoggerTest {
    private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

    @Test
    public void test1(){
        logger.error("111");

    }
}

报错
log4j:WARN No appenders could be found for logger (com.devecho.LoggerTest).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

解决方式
https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/

在 src/main/resources/log4j.properties建立log4j.properties

# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Level定义
cmd+o 搜索类名
org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar!/org/slf4j/event/Level.class

注解方式
https://projectlombok.org/

package com.devecho;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class LoggerTest {
    @Test
    public void test1(){
        log.info("111");
    }
}

Slf4j直接用注解 是大写的在org.projectlombok里
Slf4j的依赖默认搜不到,自己添加
https://mvnrepository.com/artifact/org.projectlombok/lombok

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

最后还要在idea plugins里搜lombok插件才能用

以上 好想还在用log4j,网上说SLF4j 是日志门面? 后端可以接Log4j, logback等日志实现

切换方法 去除slf4j-log4j12保留slf4j-api ,添加 logback-core(可省略) logback-classic

        <!--<dependency>-->
            <!--<groupId>org.slf4j</groupId>-->
            <!--<artifactId>slf4j-log4j12</artifactId>-->
            <!--<version>1.7.25</version>-->
        <!--</dependency>-->

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>        
<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

Slf4j也会自动切换到logback, 具体用那个实现,居然是根据依赖定的。

配置
logback.xml或logback-test.xml

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

丢到resources里自动识别,resources好像包含在classpath里,所以可以随便放东西

另外在application.properties或application.yml里也能配,但是我的好像没起作用,用springboot生产的项目就可以
logging.pattern.console="%d - %msg"%n

https://stackify.com/logging-logback/

转载请注明出处
分享到: 更多 (0)

评论 0