Spring 4.1.7带有配置文件的登录

时间:2019-06-13 09:45:56

标签: spring logback

在Spring 4.1.7(非Spring Boot)中设置“ springProfile”(环境明智的日志记录dev,prod,local)是否可行。如果是,您可以提供一个示例来写入控制台和文件。我尝试使用以下代码无法正常工作。

<configuration>
    <appender
        name="CONSOLE"
        class="ch.qos.logback.core.ConsoleAppender"></appender>
    <appender
                                name="appender1"
                                class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/log/file1.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>log/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>30MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
    <appender
        name="FILE"
        class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="appender2" />
    </appender>
    <springProfile name="default, local, LOCAL, int, INT">
        <logger name="com.test.appender1" level="DEBUG" additivity="false">
            <appender-ref ref="FILE"/>
            <appender-ref ref="CONSOLE"/>
        </logger>
        <logger name="com.test.appender2" level="DEBUG" additivity="false">
            <appender-ref ref="FILE"/>
            <appender-ref ref="CONSOLE"/>
        </logger>
    </springProfile>
    <springProfile name="prod, PROD">
        <logger
                                                name="org.springframework"
                                                level="ERROR"
                                                additivity="false">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE" />
        </logger>
    </springProfile>
    <root level="DEBUG">
        <appender-ref ref="FILE" />
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

0 个答案:

没有答案