Spring启动时的Log4j.properties

问题描述 投票:19回答:3

如何在Spring启动中加载Custom Log4j.properties文件

我在application.properties中的代码就在这里

logging.file=E:/Apps_Tek/apps-webservices-log/apps-webservices.log
logging.level.*=INFO
logging.config=log4j.properties

我在log4j.properties中的代码就在这里

log4j.rootLogger=INFO,ConsoleAppender,FileAppender

log4j.appender.ConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern=%-7p %d [%t] %c [%X{userName}] [%X{accessToken}] - %m%n

log4j.appender.FileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.FileAppender.File=E:/Apps_Tek/apps-webservices-log/apps-webservices.log
log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern=%-7p %d [%t] %c [%X{userName}] [%X{accessToken}] - %m%n

但我没有得到任何预期的输出,即,spring boot没有加载log4j.properties文件。 Spring启动有自己的默认日志记录。

log4j.properties文件位于src/main/resources

我的问题是如何将log4j.properties文件与application.properties中的logging.config属性映射,如果它位于src / main / resources中。

请提出所有必要的更改建议。

在此先感谢您的帮助。

log4j
3个回答
41
投票

如果您希望spring boot使用log4j而不是自己的默认日志记录(logback),那么您必须排除默认日志记录并在pom.xml中包含spring boot的log4j依赖项:

<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-log4j</artifactId>
</dependency>   

这种方式将寻找位于src/main/resources的log4j.properties文件。

此外,如果您希望使用log4j.properties文件中的application.properties中定义的属性

例如,你想在你的log.file.path中定义application.properties并在log4j.properties上使用它

然后你必须在你的pom.xml:中定义过滤

<filters>
    <filter>src/main/resources/application.properties</filter>
</filters>
<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>log4j.properties</include>
        </includes>         
        <filtering>true</filtering>
    </resource>
</resources>

那样你就可以:

log4j.appender.file.File=${log.file.path}/${project.artifactId}.log

在你的log4j.properties文件中


2
投票

要排除默认日志记录并在pom.xml中包含spring boot的log4j依赖项:

    <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>

如果您希望使用log4j.properties文件中的application.properties中定义的log4j属性,则需要在application.properties文件中添加以下属性。

logging.config = src / main / resources / log4j2.properties

然后spring boot将从log4j2.properties文件中读取属性

在log4j2.properties文件中添加以下属性

name=PropertiesConfig
appenders = console, file

appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n

appender.file.type = File
appender.file.name = FileAppender
appender.file.fileName=/home/ubuntu/application.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern= %d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n

loggers=file
logger.file.name=com.project
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = FileAppender

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = ConsoleAppender

是否有任何其他工作不需要在application.properties文件中指定'logging.config = src / main / resources / log4j2.properties'。?

注意:我使用的Spring启动版本是2.1.3.RELEASE

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html


1
投票

我的猜测是你的pom.xml文件没有设置为包含正确的log4j依赖项。如果你这样做它应该自动工作。在spring-boot-sample-actuator-log4j上看到他们的例子。他们的项目包括工件spring-boot-starter-log4j,它应该允许你从当前的src / main / resources位置获取log4j.properties,你不再需要属性中的logging。*条目。您可能还需要排除他们的spring-boot-starter-logging,因为我看到他们也在这样做。如果这似乎没有解决它,请将您的POM文件发布到我能看到的位置。如果这对你有用,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.