Spring Boot中的Log4j.properties [英] Log4j.properties in Spring boot

查看:363
本文介绍了Spring Boot中的Log4j.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to load Custom Log4j.properties file in Spring boot

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

My code in application.properties is here

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

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

My code in log4j.properties is here

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 Boot具有自己的默认日志记录.

But i am not getting any expected output i.e., spring boot is not loading log4j.properties file. Spring boot is having its own default logging.

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

log4j.properties file is in src/main/resources

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

My question is how to map log4j.properties file with logging.config property in application.properties if it is in src/main/resources.

请提出所有必需的更改.

Please suggest all the required changes.

非常感谢您提前提供帮助.

Thanks for any help in advance.

推荐答案

如果您想让Spring Boot使用log4j而不是其自己的默认日志记录(logback),则必须排除默认日志记录,并在spring boot中包括log4j依赖项您的pom.xml:

If you want spring boot to use log4j instead of its own default logging (logback) then you have 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-log4j</artifactId>
</dependency>   

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

that way is going to look for your log4j.properties file located in src/main/resources.

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

Also if you wish to use properties defined in you application.properties inside the log4j.properties file

例如,您想在application.properties中定义log.file.path并在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文件中

这篇关于Spring Boot中的Log4j.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆