Spring启动时的Log4j.properties

时间:2015-03-24 05:43:10

标签: log4j

如何在Spring启动时加载自定义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中。

请建议所有必要的更改。

感谢您提前提供任何帮助。

4 个答案:

答案 0 :(得分:40)

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

<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文件中

答案 1 :(得分:1)

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

答案 2 :(得分:1)

To exclude default logging and include the log4j dependency for spring boot in your pom.xml:

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

if you wish to use log4j properties defined in you application.properties inside the log4j.properties file need to add below property in application.properties file.

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

Then spring boot will read the properties from log4j2.properties file

In log4j2.properties file add the below 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

Is there any other work around which doesnot require to specify 'logging.config = src/main/resources/log4j2.properties' inside application.properties file..?

Note : Spring boot version i am using is 2.1.3.RELEASE

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

答案 3 :(得分:0)

如果有人遇到同样的问题,我没有 spring-boot-starter 依赖项,所以我不得不将其添加到 spring-boot-starter-web 依赖,这是它的样子:

<!-- Spring Boot Starter Web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- Excluding logback dependencies to use l4j2 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然后添加 Log4j2 依赖项:

<!-- Add Log4j2 Dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

从现在开始,您的 log4j2.properties 文件将用于记录配置。

相关问题