【问题标题】:Logging springboot application with log4j使用 log4j 记录 Spring Boot 应用程序
【发布时间】:2018-12-15 06:26:02
【问题描述】:

在我的 java 应用程序中,我使用 maven 和 springboot, log4j 进行日志记录。问题是记录。启动springboot(SpringApplication.run(Starter.class);) 后,log4j 不再具有日志记录控制。请参阅下面的main()。启动springboot 后,它会遵循属性文件中的logging.level.*.。不知道我做错了什么。

public static void main(String[] args) {
    logger.info("started"); //It logs this with log4j
    SpringApplication.run(Starter.class);
    logger.info("started");//but it does not log this. 
}


<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
    <Property name="logPath">${sys:user.home}</Property>
    <Property name="logPattern">[%-5level] %d{HH:mm:ss.SSS} %logger{36}.[ %C{5} , %M , %F,%L ] - %msg%n</Property>
</Properties>
<Appenders>
    <RollingRandomAccessFile name="prodLog" fileName="${logPath}/log.log"
                             filePattern="${logPath}/log/%d{yyyy-MM-dd}-%i.log.zip">
        <PatternLayout
                pattern="${logPattern}"/>
        <Policies>
            <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
            <SizeBasedTriggeringPolicy size="10 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="5"/>
    </RollingRandomAccessFile>

    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout
                pattern="${logPattern}"/>
    </Console>
</Appenders>
<Loggers>
    <AsyncRoot level="DEBUG" includeLocation="true">
        <AppenderRef ref="STDOUT"/>
        <AppenderRef ref="prodLog"/>
    </AsyncRoot>
</Loggers>

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testApp.test</groupId>
<artifactId>testApp</artifactId>
<version>1.1.2-SNAPSHOT</version>
<properties/>
<packaging>jar</packaging>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/>
</parent>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>10</source>
                <target>10</target>
                <release>10</release>
                <compilerArgs>
                    <arg>-Xlint:all,-processing</arg>
                </compilerArgs>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</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-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
</dependencies>
</project>

【问题讨论】:

标签: java maven spring-boot log4j


【解决方案1】:

感谢您的回答。

更改下面的行效果很好。

 <Loggers>
    <AsyncRoot level="DEBUG" includeLocation="true">
        <AppenderRef ref="prodLog" level="INFO"/>
        <AppenderRef ref="STDOUT" level="WARN"/>
    </AsyncRoot>
</Loggers>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2015-02-08
    • 1970-01-01
    • 2017-04-25
    • 2018-02-23
    • 2015-09-02
    相关资源
    最近更新 更多