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

本文是小编为大家收集整理的关于Spring boot中的Log4j.properties的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何将自定义log4j.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

但是我没有得到任何预期的输出,即春季启动没有加载log4j.properties文件. Spring Boot有自己的默认记录.

log4j.properties文件在src/main/resources

我的问题是如何将log4j.properties文件映射到application.config属性.

请建议所有必需的更改.

感谢提前的任何帮助.

推荐答案

如果要使用弹簧启动来使用log4j而不是其自己的默认记录(logback),则必须排除默认日志记录,并在pom.xml>中包含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文件.

另外,如果您想使用应用程序中定义的属性.

例如,您要在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文件中

其他推荐答案

排除默认日志记录,并在pom.xml中包含弹簧启动的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属性.

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

然后,Spring Boot将读取log4j2.properties file

的属性

在log4j22.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

是否还有其他任何工作都不需要指定 'logging.config = src/main/main/resources/log4j2.properties'inspect.properties file ..?

注意:我正在使用的春季启动版本是2.1.3.Release

参考:/Docs.spring.io/spring-boot/docs/current/referent/html/howto-logging.html

其他推荐答案

我的猜测是您的pom.xml文件未设置为包含正确的log4j依赖项.如果这样做,它应该会自动工作.在 Spring-boot-sample-actuator-log4j .他们的项目包括Artifact Spring-boot-starter-log4j,然后应允许您的log4j.properties从当前的SRC/MAIN/Resources位置拾取,并且您不再需要登录.*属性中的条目.我也可能需要排除他们的弹簧启动启动游戏,因为我也看到他们也在这样做.如果这似乎无法解决,请在我可以看到的位置中发布您的POM文件.请让我知道这是否适合您.

本文地址:https://www.itbaoku.cn/post/1574758.html

问题描述

How to load Custom Log4j.properties file in Spring boot

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

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

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 file is in src/main/resources

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.

推荐答案

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>   

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

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

for example, you want to have log.file.path defined in your application.properties and use it on log4j.properties

Then you have to define filtering inside your 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>

That way you can have:

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

in your log4j.properties file

其他推荐答案

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

其他推荐答案

My guess is that your pom.xml file is not set up to include the correct log4j dependency. If you do that it should automatically work. See their example at spring-boot-sample-actuator-log4j. Their project includes the artifact spring-boot-starter-log4j which should then allow your log4j.properties to be picked up from the current src/main/resources location and you should no longer need the logging.* entries in your properties. It may also be possible that you need to exclude their spring-boot-starter-logging as I see they are doing that as well. If this does not seem to resolve it please post your POM file in a location I can see it. Please let me know if this works for you.