【问题标题】:Logging : Log4j2 Implementation using Properties file in Spring Boot日志记录:在 Spring Boot 中使用属性文件实现 Log4j2
【发布时间】:2019-01-06 16:06:44
【问题描述】:

我使用 Spring Boot 和 log4j2 进行日志记录,因为我希望将日志写入文件而不是控制台。所以我实现了 log4j2.properties 并保存在 Spring Boot 项目的资源文件夹下。

当我启动 Spring Boot 应用程序时,我看不到日志文件和日志。

如果在 application.properties 的配置下方添加,则日志不会进入控制台和文件。

logging.config=classpath:log4j2.properties

但在 log4j2.properties 文件中添加了控制台和文件附加程序。

以下是详细信息:

log4j2.properties

# Root logger option
log4j.rootLogger=INFO, file, DEBUG, stdout


# configuration to print into file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:/Logs/app.log
log4j.appender.file.MaxFileSize=12MB
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} %-5p %c{1}:%L - %m%n


# configuration to print on console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

DemoApplication.java

@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) {

    SpringApplication.run(DemoApplication.class, args);
    log.info("**** Demo Application Started *****");

  }

}

POM.XML

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

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

      <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-log4j2</artifactId>
      </dependency>

      <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>

谁能帮帮我。

【问题讨论】:

  • 你有解决办法吗

标签: java maven spring-boot log4j2


【解决方案1】:

尝试在 application.properties 文件中添加以下内容:

logging.level.my.base.package=TRACE
logging.level.org.springframework=INFO
logging.level.org.hibernate=INFO 

my.base.package 代表整个项目的基础包

【讨论】:

    【解决方案2】:

    你添加到 application.properties 中了吗?

    logging.config=classpath:log4j2.properties

    在这里你可以找到完整的example

    最好的问候。

    【讨论】:

    • 是的,即使文件没有创建。知道为什么文件没有创建。谢谢@Renan Souza
    • 这里提供的所有代码。请尝试使用上面的代码。 @Renan Souza
    猜你喜欢
    • 2019-08-31
    • 2017-01-26
    • 2017-12-20
    • 2014-10-30
    • 2021-11-19
    • 2017-08-08
    • 2015-09-02
    • 2019-10-16
    • 2016-07-19
    相关资源
    最近更新 更多