这是我的示例logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_FILE" value="some file path here"/>
<property name="LOG_FILE_MAX_SIZE" value="50MB" />
<property name="LOG_FILE_MAX_HISTORY" value="30" />
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<springProfile name="!test & !prod">
<logger name="com.myapp" level="DEBUG" />
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<springProfile name="test">
<logger name="com.myapp" level="DEBUG" />
<root level="WARN">
<appender-ref ref="FILE" />
</root>
</springProfile>
<springProfile name="prod">
<logger name="com.myapp" level="INFO" />
<root level="WARN">
<appender-ref ref="FILE"/>
</root>
</springProfile>
我的意图是仅将内容记录到FILE以用于测试和产品配置文件,但是对于其他任何配置文件(即localhost),我希望我的日志仅在CONSOLE中获取。
使用上述设置,如果我使用本地主机配置文件启动我的spring boot应用程序(2.1.1.RELEASE版本),则仅按预期方式将其记录在CONSOLE中,但是,如果我使用test或prod配置文件,它将同时记录内容在CONSOLE和FILE中。
您在logback xml中看到任何问题吗?
答案 0 :(得分:0)
据我所知,Spring在配置文件选择中不允许逻辑表达式。可以只说!test
,而不能说两者的结合。因此,在您的情况下,如果测试未激活或产品未激活,则条件将触发,这意味着测试已激活或产品已激活。
您可以做的是列举其他您要实际登录控制台的配置文件。例如localhost
。
答案 1 :(得分:0)
这里是一个示例,您可以在其中为各种环境选择附加程序类型,例如对于test和prod配置文件仅选择FILE,对于本地环境仅选择stdout或控制台。
<springProfile name="dev,test,local">
<property name="LOG_PATH" value="C:/<folder_name>/" />
</springProfile>
<!--if production environment is linux -->
<springProfile name="prod">
<property name="LOG_PATH" value="/opt/<folder_name>/" />
</springProfile>
<!-- make sure the pattern tag expression don't break in the middle-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- })
{magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan}
%clr(:){faint} %m%n%wEx</pattern>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}projectLog.%d{dd-MM-yyyy}.log
</fileNamePattern>
<!-- <maxHistory>30</maxHistory> -->
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
</appender>
<springProfile name="local">
<logger name="org.springframework" level="info" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</springProfile>
<springProfile name="test,dev,prod">
<logger name="org.springframework" level="error" additivity="false">
<appender-ref ref="file" />
</logger>
<root level="info">
<appender-ref ref="file" />
</root>
</springProfile>
答案 2 :(得分:0)
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
<springProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active ,for SpringBoot 1.5.4 replace with [name="dev, staging"]-->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
https://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/reference/html/boot-features-logging.html