Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring引导应用程序中正确使用log4j2_Java_Spring Boot_Logging_Log4j2_Slf4j - Fatal编程技术网

Java 如何在Spring引导应用程序中正确使用log4j2

Java 如何在Spring引导应用程序中正确使用log4j2,java,spring-boot,logging,log4j2,slf4j,Java,Spring Boot,Logging,Log4j2,Slf4j,我只想在我的应用程序中包含用于日志记录目的的log4j2。 因此,我在项目的pom.xml中包含了以下依赖项: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency

我只想在我的应用程序中包含用于日志记录目的的log4j2。 因此,我在项目的pom.xml中包含了以下依赖项:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
在许多平台上对其进行了更多的研究后,我发现了一种排除回登录依赖性的方法,我认为这是必要的 使用log4j2。但是在排除该jar之后,我还得到以下错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
为了解决这个问题: 我还排除了log4j到slf4j的依赖关系

最后它开始工作了,但现在我看到了大量与Spring boot Internal相关的日志消息。 例如:

2019-12-05 12:05:01.649 [DEBUG]  [restartedMain] - Included patterns for restart : []
2019-12-05 12:05:01.649 [DEBUG]  [restartedMain] - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
2019-12-05 12:05:01.649 [INFO ]  [restartedMain] - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-12-05 12:05:01.649 [DEBUG]  [restartedMain] - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3883fd6d
2019-12-05 12:05:01.666 [DEBUG]  [restartedMain] - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2019-12-05 12:05:01.686 [DEBUG]  [restartedMain] - Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory'
2019-12-05 12:05:02.037 [DEBUG]  [restartedMain] - Creating shared instance of singleton bean 'propertySourcesPlaceholderConfigurer'
2019-12-05 12:05:02.043 [DEBUG]  [restartedMain] - Creating shared instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator'
2019-12-05 12:05:02.055 [DEBUG]  [restartedMain] - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2019-12-05 12:05:02.056 [DEBUG]  [restartedMain] - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2019-12-05 12:05:02.059 [DEBUG]  [restartedMain] - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
我的pom.xml类似于:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.log4j2</groupId>
    <artifactId>log4j2-check</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>log4j2-check</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

只有在应用程序中使用log4j2,而不是在Spring boot中使用log4j2,如何才能或需要什么正确的配置。

我认为这是因为处理Spring boot内部文件的log4j2记录器包含在您的配置中

看一看-这是相关的定义


从技术上讲,我并不十分熟悉log2j2,但它似乎支持指令

,因此我确定了触发从spring boot打印意外消息的问题。 从log4j2配置中,我设置了以下内容

rootLogger.level=debug

此属性使log4j2打印spring引导设置为debug模式的所有内部消息

将其更改为以下内容:

rootLogger.level=info

解决了这个问题

现在,应用程序只打印级别大于info的记录器。 所以现在,它不会打印不需要的日志消息


我想说这不是问题,log4j2工作正常,但我的配置设置不符合我的要求。

如果您只想在调试时记录应用程序,请将根日志记录器级别设置为警告,然后为应用程序使用的包空间添加日志记录器,并将其设置为调试。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.log4j2</groupId>
    <artifactId>log4j2-check</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>log4j2-check</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
name=PropertiesConfig
property.filename = Test
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level]  [%t] - %msg%n
rootLogger.level = debug
rootLogger.appenderRefs = STDOUT
rootLogger.appenderRef.stdout.ref = STDOUT