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

时间:2018-08-02 09:21:02

标签: java maven spring-boot log4j2

我已经在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.XML

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

DemoApplication.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 *****");

      }

    }

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

这很奇怪,父包记录器“已启用记录器:输入主\ n \ n”被写入文件,而另一个父记录器“ **** Demo Application Started *****”未写入文件如上面的代码所示。并检查了子包,即com.java.app.endpoint记录器,即使那些也未写入文件的记录器。

并确定控制台日志以

的形式出现

2018-08-03 12:55:18.302信息11440 --- [nio-8088-exec-1] c.j.c.e.Classname:记录器消息

如果c.j.c.e.作为日志中类名的前缀而出现的原因为何未写入文件?

我可能做错了什么。有人可以帮忙吗?

5 个答案:

答案 0 :(得分:2)

在springframework.guru上的教程之后,我也面临这个问题。搜索完春季启动文档后,我用那些依赖项配置了pom.xml

class Api::V1::PaymentsController < ApplicationController
  before_action :authenticate_user!

  def create
     Stripe.api_key = ENV['STRIPE_SECRET_KEY_TEST']

    // render an error if there is an issue creating a customer
    customer = Stripe::Customer.create({
      email: current_user.email,
      source: request.params[:id]
    })

    stripe_plan = ENV['STRIPE_PLAN_ID_TEST']

    // render an error if there is an issue creating a subscription
    subscription = Stripe::Subscription.create({
      customer: customer.id,
      items: [{ plan: stripe_plan }],
    })

    current_user.subscription_plan = 1
    current_user.save

    if current_user.save
      render json: { 'success': true }, status: 200
    else
      render json: { 'error': 'Some error with saving user here' }, status: 500
    end
  end
end

,然后在application.properties文件中添加<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> 。之后,当我运行该应用程序时,我可以看到该日志出现在我的日志文件中。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

似乎有必要将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

答案 3 :(得分:0)

按如下所示更改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。

答案 4 :(得分:0)

Spring Boot支持Log4j 2进行日志配置,您可以从Configure Log4j for Logging了解如何配置

首先,您需要排除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>

然后在您的log4j2.json中有log4j2.xmllog4j2.yamlsrc/main/resources