简体   繁体   English

logback-spring.xml 配置springProfile时创建多个日志文件

[英]Logback-spring.xml creating multiple log files when configured with springProfile

I am getting an issue while adding <springProfile> in my logback-spring.xml.在我的 logback-spring.xml 中添加<springProfile>时遇到问题。 In my case, I need to get the file name as well as path from the application.properties file and provide a date based rolling policy.就我而言,我需要从 application.properties 文件中获取文件名和路径,并提供基于日期的滚动策略。 So I can't specify the file name and path in logback-spring.xml.所以我不能在logback-spring.xml中指定文件名和路径。

So whenever I run the project specific to the profile from my IDE or by executable jar file, I am getting two log files created, one at the current working directory and the other at the specified file path location mentioned in the application.properties file.因此,每当我从 IDE 或可执行 jar 文件运行特定于配置文件的项目时,我都会创建两个日志文件,一个位于当前工作目录,另一个位于 application.properties 文件中提到的指定文件路径位置。

I have three profiles based on the three different environments ie Local, Dev and Prod.我有三个基于三种不同环境的配置文件,即本地、开发和生产。

for local I am using spring OOTB application.properties file.对于本地,我正在使用 spring OOTB application.properties 文件。

spring.profiles.active=local
server.port=8080

#logging 

logging.level.root=info
logging.level.com.myApp=trace
logging.file.path=C:/Spring/logs
logging.file.name=app-Log
logging.config=classpath:logback-spring.xml

and for dev and prod i am using application-dev.properties and application-prod.properties respectively.对于 dev 和 prod,我分别使用 application-dev.properties 和 application-prod.properties。

application-dev.properties应用程序-dev.properties

spring.profiles.active=dev
server.port=9090

#logging 

logging.level.root=info
logging.level.com.myApp=trace
logging.file.path=C:/Spring/logs/dev
logging.file.name=app-Log
logging.config=classpath:logback-spring.xml

Here is my logback-spring.xml.这是我的 logback-spring.xml。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include
        resource="org/springframework/boot/logging/logback/base.xml" />

    <springProfile name="prod">
        <property resource="application-prod.properties" />
        <appender name="SAVE-TO-FILE"
            class="ch.qos.logback.core.rolling.RollingFileAppender">

            <file>${logging.file.path}/${logging.file.name}.log</file>

            <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <Pattern>
                    %d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %X{clientapp} %-5level %-40.40logger{39} : %msg%n
                </Pattern>
            </encoder>

            <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>
                    ${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
                </fileNamePattern>
            </rollingPolicy>

        </appender>
        <root level="info">
            <appender-ref ref="SAVE-TO-FILE" />
        </root>
    </springProfile>

    <springProfile name="uat">
        <property resource="application-uat.properties" />
        <appender name="SAVE-TO-FILE"
            class="ch.qos.logback.core.rolling.RollingFileAppender">

            <file>${logging.file.path}/${logging.file.name}.log</file>

            <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <Pattern>
                    %d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread] %-5level %-40.40logger{39} : %msg%n
                </Pattern>
            </encoder>

            <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>
                    ${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
                </fileNamePattern>
            </rollingPolicy>

        </appender>
        <root level="info">
            <appender-ref ref="SAVE-TO-FILE" />
        </root>
    </springProfile>



    <springProfile name="local">
        <property resource="application.properties" />
        <appender name="SAVE-TO-FILE"
            class="ch.qos.logback.core.rolling.RollingFileAppender">

            <file>${logging.file.path}/${logging.file.name}.log</file>

            <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <Pattern>
                    %d{dd-MM-yyyy HH:mm:ss.SSS} %X{host} %X{port} [%thread]  %-5level  %-40.40logger{39} : %msg%n
                </Pattern>
            </encoder>

            <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>
                    ${logging.file.path}/${logging.file.name}_%d{dd-MM-yyyy}.log
                </fileNamePattern>
            </rollingPolicy>

        </appender>
        <root level="info">
            <appender-ref ref="SAVE-TO-FILE" />
        </root>
    </springProfile>

</configuration>

So after a long time I got to know the extra log file was created because of the property inside application.properties files.因此,经过很长时间,我才知道由于 application.properties 文件中的属性而创建了额外的日志文件。

The below property is honored by spring to create a file also I was mentioning the file name as well as the file path in the logback-spring.xml.以下属性由 spring 创建一个文件,我还提到了 logback-spring.xml 中的文件名和文件路径。

logging.file.name=app-Log

So whenever I run the application spring see this above property and creates the file in the current working directory and one other file in the specified location in the logback-spring.xml file.因此,每当我运行应用程序 spring 时,请查看上述属性并在当前工作目录中创建文件,并在 logback-spring.xml 文件中的指定位置创建另一个文件。

${logging.file.path}/${logging.file.name}.log

So if you are mentioning the file name and path do not use the reserved words.因此,如果您提到文件名和路径,请不要使用保留字。 I know its a very silly mistake but if it helps anyone then this will not be in vain.我知道这是一个非常愚蠢的错误,但如果它对任何人都有帮助,那么这不会是徒劳的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM