简体   繁体   English

Spring 开机不搜索 log4j2 配置文件

[英]Spring boot not searching for log4j2 configuration file

I am trying to use Apache Log4j 2.12.1 with Spring Boot 2.2.1 but when I run, it doesn't give any warning about missing log4j 2 configuration file.我正在尝试将 Apache Log4j 2.12.1 与 Spring Boot 2.2.1 一起使用,但是当我运行时,它没有给出任何关于缺少 log4j 2 配置文件的警告。 Below is the code for logging(Simple once class to test if it works or note):下面是日志记录的代码(简单一次 class 测试是否有效或注意):

package com.example.Log4jdemoSpringboot;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Log4jDemoSpringBootApplication {

    static Logger logger = LogManager.getLogger(Log4jDemoSpringBootApplication.class);
    public static void main(String[] args) {
        SpringApplication.run(Log4jDemoSpringBootApplication.class, args);
        logger.info("info");
        logger.warn("warn");
        logger.error("error");
        logger.debug("debug");
        logger.fatal("fatal");
    }

}

and Below is the output:下面是 output:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.1.RELEASE)

2019-11-18 12:19:20.844  INFO 13136 --- [           main] c.e.L.Log4jDemoSpringBootApplication     : Starting Log4jDemoSpringBootApplication on Gurpreet-PC with PID 13136 (C:\Development\eclipse-workspace\Log4j-demo-Spring-boot\target\classes started by Gurpreet in C:\Development\eclipse-workspace\Log4j-demo-Spring-boot)
2019-11-18 12:19:20.872  INFO 13136 --- [           main] c.e.L.Log4jDemoSpringBootApplication     : No active profile set, falling back to default profiles: default
2019-11-18 12:19:26.946  INFO 13136 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-11-18 12:19:27.009  INFO 13136 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-11-18 12:19:27.011  INFO 13136 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-18 12:19:27.442  INFO 13136 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-11-18 12:19:27.444  INFO 13136 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 6320 ms
2019-11-18 12:19:28.213  INFO 13136 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-18 12:19:28.925  INFO 13136 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-18 12:19:28.937  INFO 13136 --- [           main] c.e.L.Log4jDemoSpringBootApplication     : Started Log4jDemoSpringBootApplication in 10.398 seconds (JVM running for 13.269)
2019-11-18 12:19:28.945  INFO 13136 --- [           main] c.e.L.Log4jDemoSpringBootApplication     : info
2019-11-18 12:19:28.949  WARN 13136 --- [           main] c.e.L.Log4jDemoSpringBootApplication     : warn
2019-11-18 12:19:28.952 ERROR 13136 --- [           main] c.e.L.Log4jDemoSpringBootApplication     : error
2019-11-18 12:19:28.953 ERROR 13136 --- [           main] c.e.L.Log4jDemoSpringBootApplication     : fatal

However if I use same configuration with simple java projects, it works.但是,如果我对简单的 java 项目使用相同的配置,它就可以工作。

I had the issue, spring boot didn't search the configuration xml file.我遇到了问题,spring 引导没有搜索配置 xml 文件。 I added this line to application.properties (log4j2.xml is in src/main/resources)我将此行添加到 application.properties(log4j2.xml 在 src/main/resources 中)

logging.config=classpath:log4j2.xml

Just sharing as I also faced the same issue with my log4j2 configuration.只是分享,因为我的 log4j2 配置也面临同样的问题。 I got the following solution.我得到了以下解决方案。

  1. Run command: mvn dependency:tree This command will tell you which dependencies are loading before your configuration.运行命令: mvn dependency:tree 此命令将告诉您在配置之前加载了哪些依赖项。

  2. Adding exclusions to the dependency in pom.xml loading before your log dependency, add either of the following mine worked with the option a.在您的日志依赖项之前加载 pom.xml 中的依赖项的排除项,添加以下任何一个使用选项 a。

a-一个-

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

b-乙-

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

After this all worked well and my log4j2.xml configuration loaded successfully.之后一切正常,我的 log4j2.xml 配置成功加载。 Make sure the configuration file is in the classpath.确保配置文件在类路径中。

Hope this is what are looking for希望这是正在寻找的
This is something that I have used in Spring boot logging In application.properties这是我在 Spring 引导日志中使用的东西 在 application.properties

#logging.level.root=WARN
logging.level.org.springframework=DEBUG
logging.level.com.appicantion.name=DEBUG

#output to a temp_folder/file
logging.file=${java.io.tmpdir}/application.log

# Logging pattern for the console
logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} - %msg%n

# Logging pattern for file
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%

Your Answer: In log4j2你的答案:在 log4j2

add log4j2.xml in src/main/resources folder.在 src/main/resources 文件夹中添加 log4j2.xml。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
        <Property name="APP_LOG_ROOT">c:/temp</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}" />
        </Console>

        <RollingFile name="appLog"
            fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"
            filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${LOG_PATTERN}" />
            <Policies>
                <SizeBasedTriggeringPolicy size="19500KB" />
            </Policies>
            <DefaultRolloverStrategy max="1" />
        </RollingFile>

    </Appenders>
    <Loggers>

        <Logger name="com.application.app" additivity="false">
            <AppenderRef ref="appLog" />
            <AppenderRef ref="Console" />
        </Logger>

        <Root level="debug">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

https://howtodoinjava.com/spring-boot2/logging/spring-boot-log4j2-config/ https://howtodoinjava.com/spring-boot2/logging/spring-boot-log4j2-config/

This issue also really annoyed me but solution is close enough: Spring by default using Logback and if you want to use Log4J2 or other loggers first you need to exclude logback from Spring library in Maven and add to resource folder "log4J2.xml" configuration file.这个问题也让我很恼火,但解决方案已经足够接近了:Spring 默认使用 Logback,如果你想先使用 Log4J2 或其他记录器,你需要从 Maven 的 Spring 库中排除 logback 并添加到资源文件夹“log4J2.xml”配置文件. You can find instruction here on step 5 - https://www.baeldung.com/spring-boot-logging您可以在此处找到有关步骤 5 的说明 - https://www.baeldung.com/spring-boot-logging

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

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