繁体   English   中英

Spring Boot - log4j2.properties 创建日志文件但不将日志写入文件

[英]Spring Boot - log4j2.properties creating log files but not writing the logs in file

我在 springboot 应用程序中使用了 log4j2.properties 文件。 正在创建日志文件,但日志未写入文件。

请在下面找到详细信息:

log4j2.properties

name=PropertiesConfig
property.filename = C:/Logs
appenders = console, file

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/app.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

loggers=file
logger.file.name=com.java.app //Parent Package name for the application 
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

POM文件

<!-- Logging -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

         <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.11.1</version>
    </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.11.1</version>
    </dependency>

演示应用程序.java

package com.java.app;

    @SpringBootApplication
    public class DemoApplication extends SpringBootServletInitializer {

      private final static Logger log = LogManager.getLogger(DemoApplication.class);

      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(DemoApplication.class);
      }

      public static void main(String[] args) {

        log.info("Logger enabled: Entering main \\n\\n");
        SpringApplication.run(DemoApplication.class, args);
        log.info("**** Demo Application Started *****");

      }

    }

日志出现在控制台中,但没有写入文件,因为我没有遇到问题。

奇怪的是,父包记录器“Logger enabled: Entering main \\n\\n”被写入文件,另一个父记录器“**** Demo Application Started *****”没有写入文件作为代码如上所示。 并且还检查了子包,即com.java.app.endpoint即使那些也没有写入文件。

并且还确定控制台日志即将到来

2018-08-03 12:55:18.302 INFO 11440 --- [nio-8088-exec-1] cjceClassname:记录器消息

如果 cjce 作为日志中类名的前缀,为什么没有写入文件?

我可能做错了什么。 任何人都可以请帮忙。

在springframework.guru上的教程之后,我也面临这个问题。 搜索Spring Boot文档后,我使用这些依赖项配置pom.xml

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

并在application.properties文件中添加logging.config=src/main/resources/log4j2.properties 之后,当我运行该应用程序时,我可以看到该日志出现在我的日志文件中。

使用Spring Boot可以在application.properties指定log4j2.properties

logging.config=src/main/resources/log4j2.properties

似乎也有必要将log4j设置为标准的Apache日志记录。 通常,它使用logback。 所以我不得不添加

<dependency> 
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId> 
     <exclusions> 
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId> 
       </exclusion>
  </exclusions> 

到我的pom.xml

如下更改POM.xml文件,它应该可以工作。

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
             <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

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

        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>

请注意,建议使用log4j2.xml代替log4j2.properties。

Spring Boot支持Log4j 2进行日志记录配置,您可以从如何配置Log4j进行日志记录中进行配置

首先,您需要排除Spring Boot日志记录

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

然后在您的src/main/resources包含log4j2.jsonlog4j2.xmllog4j2.yaml

要使用外部配置,您只需放置log4j.propertieslogback-spring.xmllogback.xml并将以下属性添加到application.properties文件

logging.config=classpath:logback-spring.xml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM