Logback与Spring Profile生成2个文件。

huangapple go评论69阅读模式
英文:

Logback with SpringProfile generating 2 files

问题

我尝试使用logback-spring.xml和springprofile如下所示,这是一个自定义的日志依赖项,用于服务,当我激活common-services配置文件时,生成了2个日志,而根据下面的配置应该生成1个文件,请问有什么问题?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property resource="bootstrap.yml" />
    <springProperty name="appName" source="spring.application.name" />
    <springProperty name="loglevel" source="logging.level.com.cialty" />
    <springProperty name="serverPort" source="server.port" />

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{ISO8601} %-5p [%c{3}] [%t] %m%n</pattern>
        </encoder>
    </appender>

    <appender name="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <springProfile name="common-services">
            <file>/appllogs/spp/${appName}-${serverPort}.log</file>
        </springProfile>
        <springProfile name="default">
            <file>/appllogs/spp/${appName}.log</file>
        </springProfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <springProfile name="common-services">
                <fileNamePattern>/appllogs/backup/${appName}/%d{yyyy-MM-dd}%i.log-${serverPort}.zip
                </fileNamePattern>
            </springProfile>
            <springProfile name="default">
                <fileNamePattern>/appllogs/backup/${appName}/%d{yyyy-MM-dd}%i.log.zip
                </fileNamePattern>
            </springProfile>
            <maxHistory>7</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>

        <encoder>
            <pattern>%d %-5p %m%n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework.cloud" level="ERROR" additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>
    <logger name="com.cialty" level="${loglevel}" additivity="false">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="APPLICATION" />
    </logger>

    <root level="${loglevel}">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="APPLICATION" />
    </root>
</configuration>

更新:添加了日志信息

08:01:46,061 |-WARN in Logger[org.springframework.core.env.PropertySourcesPropertyResolver] - No appenders present in context [default] for logger [org.springframework.core.env.PropertySourcesPropertyResolver].
08:01:46,062 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
...
(更多日志信息)
...
08:01:47,930 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
08:01:47,930 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@423b2b62 - Registering current configuration as safe fallback point

希望这能帮助你找出问题所在。

英文:

I'm trying to use logback-spring.xml with springprofile as below, this is a custom logging dependency goes into services, when I activate common-services profile I got 2 logs generated, while based on the config below 1 file should be generated, can anyone tell me what's wrong?

 &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
&lt;property resource=&quot;bootstrap.yml&quot; /&gt;
&lt;springProperty name=&quot;appName&quot;
source=&quot;spring.application.name&quot; /&gt;
&lt;springProperty name=&quot;loglevel&quot;
source=&quot;logging.level.com.cialty&quot; /&gt;
&lt;springProperty name=&quot;serverPort&quot; source=&quot;server.port&quot; /&gt;
&lt;appender name=&quot;CONSOLE&quot;
class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
&lt;encoder&gt;
&lt;pattern&gt;%d{ISO8601} %-5p [%c{3}] [%t] %m%n&lt;/pattern&gt;
&lt;/encoder&gt;
&lt;/appender&gt;
&lt;appender name=&quot;APPLICATION&quot;
class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;
&lt;springProfile name=&quot;common-services&quot;&gt;
&lt;file&gt;/appllogs/spp/${appName}-${serverPort}.log&lt;/file&gt;
&lt;/springProfile&gt;
&lt;springProfile name=&quot;default&quot;&gt;
&lt;file&gt;/appllogs/spp/${appName}.log&lt;/file&gt;
&lt;/springProfile&gt;
&lt;rollingPolicy
class=&quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&quot;&gt;
&lt;springProfile name=&quot;common-services&quot;&gt;
&lt;fileNamePattern&gt;/appllogs/backup/${appName}/%d{yyyy-MM-dd}%i.log-${serverPort}.zip
&lt;/fileNamePattern&gt;
&lt;/springProfile&gt;
&lt;springProfile name=&quot;default&quot;&gt;
&lt;fileNamePattern&gt;/appllogs/backup/${appName}/%d{yyyy-MM-dd}%i.log.zip
&lt;/fileNamePattern&gt;
&lt;/springProfile&gt;
&lt;maxHistory&gt;7&lt;/maxHistory&gt;
&lt;timeBasedFileNamingAndTriggeringPolicy
class=&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP&quot;&gt;
&lt;maxFileSize&gt;500MB&lt;/maxFileSize&gt;
&lt;/timeBasedFileNamingAndTriggeringPolicy&gt;
&lt;totalSizeCap&gt;1GB&lt;/totalSizeCap&gt;
&lt;/rollingPolicy&gt;
&lt;encoder&gt;
&lt;pattern&gt;%d %-5p %m%n&lt;/pattern&gt;
&lt;/encoder&gt;
&lt;/appender&gt;
&lt;logger name=&quot;org.springframework.cloud&quot; level=&quot;ERROR&quot;
additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
&lt;/logger&gt;
&lt;logger name=&quot;com.cialty&quot; level=&quot;${loglevel}&quot;
additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
&lt;appender-ref ref=&quot;APPLICATION&quot; /&gt;
&lt;/logger&gt;
&lt;root level=&quot;${loglevel}&quot;&gt;
&lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
&lt;appender-ref ref=&quot;APPLICATION&quot; /&gt;
&lt;/root&gt;
&lt;/configuration&gt;

UPDATE: Logs added

08:01:46,061 |-WARN in Logger[org.springframework.core.env.PropertySourcesPropertyResolver] - No appenders present in context [default] for logger [org.springframework.core.env.PropertySourcesPropertyResolver].
08:01:46,062 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
08:01:46,062 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [CONSOLE]
08:01:46,066 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
08:01:46,079 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
08:01:46,081 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [APPLICATION]
08:01:46,089 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1641672672 - setting totalSizeCap to 1 GB
08:01:46,091 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1641672672 - Will use zip compression
08:01:46,092 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1641672672 - Will use the pattern /appllogs/backup/specialty-redis-schedule/%d{yyyy-MM-dd}%i.log-8443 for the active file
08:01:46,094 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7e094740 - The date pattern is &#39;yyyy-MM-dd&#39; from file name pattern &#39;/appllogs/backup/specialty-redis-schedule/%d{yyyy-MM-dd}%i.log-8443.zip&#39;.
08:01:46,094 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7e094740 - Roll-over at midnight.
08:01:46,095 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7e094740 - Setting initial period to Wed Aug 05 08:01:46 CDT 2020
08:01:46,095 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7e094740 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
08:01:46,095 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7e094740 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
08:01:46,097 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
08:01:46,098 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[APPLICATION] - Active log file name: /appllogs/spp/specialty-redis-schedule-8443.log
08:01:46,098 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[APPLICATION] - File property is set to [/appllogs/spp/specialty-redis-schedule-8443.log]
08:01:46,100 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework.cloud] to OFF
08:01:46,100 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@1ac85b0c - Propagating OFF level on Logger[org.springframework.cloud] onto the JUL framework
08:01:46,101 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[org.springframework.cloud]
08:01:46,101 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [APPLICATION] to Logger[org.springframework.cloud]
08:01:46,101 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.cialty] to INFO
08:01:46,101 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@1ac85b0c - Propagating INFO level on Logger[com.cialty] onto the JUL framework
08:01:46,101 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.cialty] to false
08:01:46,101 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[com.cialty]
08:01:46,101 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [APPLICATION] to Logger[com.cialty]
08:01:46,101 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
08:01:46,104 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@1ac85b0c - Propagating INFO level on Logger[ROOT] onto the JUL framework
08:01:46,104 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[ROOT]
08:01:46,104 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [APPLICATION] to Logger[ROOT]
08:01:46,104 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
08:01:46,104 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@16fb356 - Registering current configuration as safe fallback point
2020-08-05 08:01:46,597 INFO  [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker] [main] Bean &#39;org.springframework.retry.annotation.RetryConfiguration&#39; of type [org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$85af8ffe] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
08:01:47,923 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
08:01:47,923 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [CONSOLE]
08:01:47,923 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
08:01:47,924 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
08:01:47,924 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [APPLICATION]
08:01:47,925 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@691540605 - setting totalSizeCap to 1 GB
08:01:47,926 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@691540605 - Will use zip compression
08:01:47,926 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@691540605 - Will use the pattern /appllogs/backup/specialty-redis-schedule/%d{yyyy-MM-dd}%i.log for the active file
08:01:47,926 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@127a7272 - The date pattern is &#39;yyyy-MM-dd&#39; from file name pattern &#39;/appllogs/backup/specialty-redis-schedule/%d{yyyy-MM-dd}%i.log.zip&#39;.
08:01:47,926 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@127a7272 - Roll-over at midnight.
08:01:47,927 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@127a7272 - Setting initial period to Wed Aug 05 08:01:47 CDT 2020
08:01:47,927 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@127a7272 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
08:01:47,927 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@127a7272 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
08:01:47,928 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
08:01:47,928 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[APPLICATION] - Active log file name: /appllogs/spp/specialty-redis-schedule.log
08:01:47,929 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[APPLICATION] - File property is set to [/appllogs/spp/specialty-redis-schedule.log]
08:01:47,930 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework.cloud] to OFF
08:01:47,930 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@ff23ae7 - Propagating OFF level on Logger[org.springframework.cloud] onto the JUL framework
08:01:47,930 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[org.springframework.cloud]
08:01:47,930 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [APPLICATION] to Logger[org.springframework.cloud]
08:01:47,930 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.cialty] to INFO
08:01:47,930 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@ff23ae7 - Propagating INFO level on Logger[com.cialty] onto the JUL framework
08:01:47,930 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.cialty] to false
08:01:47,930 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[com.cialty]
08:01:47,930 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [APPLICATION] to Logger[com.cialty]
08:01:47,930 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
08:01:47,930 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@ff23ae7 - Propagating INFO level on Logger[ROOT] onto the JUL framework
08:01:47,930 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[ROOT]
08:01:47,930 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [APPLICATION] to Logger[ROOT]
08:01:47,930 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
08:01:47,930 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@423b2b62 - Registering current configuration as safe fallback point

答案1

得分: 0

不确定这是否是Spring Boot的一个错误,还是预期的行为,但问题出在我激活配置文件的方式上。

spring:
 profiles:
    active:
     - common-services  <------这将使Spring生成2个文件,因为默认配置文件也将被激活。

spring:
 profiles:
    active: common-services <------这将只激活common-services配置文件,从而只生成1个文件。
英文:

Not sure if this is a bug in Spring-Boot or it's an expected behavior but the problem is in the way I was activating the profile

spring:
profiles:
active: 
- common-services  &lt;------This will make spring generates 2 files as the default profile will be active as well.
spring:
profiles:
active: common-services &lt;------This will activate common-services only which results to generate 1 file only. 

huangapple
  • 本文由 发表于 2020年8月5日 20:45:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63265537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定