java - 从 log4j 迁移到 lof4j2 时的 SpringBoot 问题

标签 java spring spring-boot log4j2

之前(工作正常)

log4j.properties

log4j.rootLogger=DEBUG, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/spring-boot-mqtt.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [%t] [%c{1}] - %m%n

和 Maven 依赖,

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
    </dependency>

之后(不起作用)

log4j2.properties

name = PropertiesConfig
property.filename = logs
appenders = file

appender.file.type = File
appender.file.name = ROLLING
appender.file.append = true
appender.file.fileName=${filename}/spring-boot-mqtt.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5p] [%t][%c{2}] - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = file
rootLogger.appenderRef.file.ref = ROLLING

以及我的 Maven 依赖项,

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.5</version>
    </dependency>

当我在独立的 java 应用程序中对 log4j2 使用相同的配置时,它工作正常。但是在 spring-boot 应用程序中,生成了一个文件,但它仍然是空的。我在控制台中看到相同的默认登录。此外,属性中的 layout.pattern 似乎没有任何影响。

注意:使用 Slf4j

最佳答案

关于写入文件,您应该检查 documentation :

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a logging.file or logging.path property (for example in your application.properties).

关于java - 从 log4j 迁移到 lof4j2 时的 SpringBoot 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36082584/

相关文章:

http - 通过 http 使用执行器监控 spring boot

java - android中读取联系人的两个版本之间的冲突

java - 我正在尝试从数据库中获取数据,其中 date = jdatechooser :

java - http 的正确 JSON 格式、正确的 JSON 转义宪章

java - XMLless JPA 2.1 中 EntityManager 为 null

java - 从 java 客户端应用程序的请求中设置的 CookieStore 检索 cookie

Java尝试创建一个类来将ArrayList打印到文件

java - 直接类型的 Java 可变参数与通过扩展的通配符泛型之间有什么区别?

java - 优化/限制缓存Spring的大小

java - Spring Boot - 限制创建的连接数