使用log4j记录springboot应用程序

时间:2018-12-15 06:26:03

标签: java maven spring-boot log4j

在我的Java应用程序中,我正在使用maven和springboot, log4j进行日志记录。问题是记录。启动log4j之后,springboot(SpringApplication.run(Starter.class);)不再具有日志记录控制。请参阅下面的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>

1 个答案:

答案 0 :(得分:0)

感谢您的回答。

更改下面的线条效果很好。

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