Spring Boot-log4j2.properties创建日志文件,但不将日志写入文件 [英] Spring Boot - log4j2.properties creating log files but not writing the logs in file

查看:217
本文介绍了Spring Boot-log4j2.properties创建日志文件,但不将日志写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used log4j2.properties file with springboot application. Log file was creating but logs are not written into the file.

请找到以下详细信息:

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

      }

    }

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

Logs are appearing in the console but not written into file as i am not getting the issue.

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

It's strange, parent package logger "Logger enabled: Entering main \n\n" is written into the file and the other parent logger "**** Demo Application Started *****" is not written into the file as the code is shown above. and also checked for the sub package i.e com.java.app.endpoint loggers even those also not written into the file.

并确定控制台日志以

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

2018-08-03 12:55:18.302 INFO 11440 --- [nio-8088-exec-1] c.j.c.e.Classname : logger message

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

If c.j.c.e. coming as prefix to the class name in logs those are not written into the file why?

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

I might be doing something wrong. Can anyone please help on this.

推荐答案

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

I also face with this problem following the tutorial on springframework.guru. After searching on spring boot docs then I config my pom.xml with those dependencies

<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.之后,当我运行该应用程序时,我可以看到该日志显示在我的日志文件中.

and add logging.config=src/main/resources/log4j2.properties in the application.properties file. After that I can see the log appear on my log file when I run the application.

这篇关于Spring Boot-log4j2.properties创建日志文件,但不将日志写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆