我想添加新的日志文件mynewlog.log,我习惯于在不同的环境中使用不同的日志级别而不影响现有的日志。dev环境和prod环境的级别在下面的描述中提到:
对于dev env
电平错误激活
级别信息处于活动状态
级别调试处于活动状态(dev的默认级别)
我添加了一个新的appender mynewlog,并将默认级别设置为debug,然后创建了3个名为mynewlog的记录器,每个记录器都有不同的级别。
dev env的logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="2 seconds">
<property resource="logback.properties" />
<appender name="apiLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/mylog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/mylog.log.%d{yyyy.MM.dd}</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
</appender>
<appender name="myNewLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/myNewLog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/myNewLog%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="apiLog" level="INFO" additivity="false">
<appender-ref ref="apiLog" />
</logger>
<logger name="myNewLog" level="DEBUG" additivity="true">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="INFO" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="ERROR" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<root level="ERROR">
<appender-ref ref="console" />
</root>
</configuration>
对于产品环境
级别错误处于活动状态(产品的默认级别)
等级信息将在需要时激活
级别调试已停用
我添加了一个新的appender mynewlog,并将默认级别设置为error,然后创建了3个名为mynewlog的记录器,每个记录器都有不同的级别。
prod env的logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="2 seconds">
<property resource="logback.properties" />
<appender name="apiLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/mylog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/mylog.log.%d{yyyy.MM.dd}</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
</appender>
<appender name="myNewLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APPL_FULL_PATH}/logs/myNewLog.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APPL_FULL_PATH}/logs/myNewLog%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<Pattern>%d{yyyy.MM.dd HH:mm:ss} %p %m%n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="apiLog" level="INFO" additivity="false">
<appender-ref ref="apiLog" />
</logger>
<logger name="myNewLog" level="ERROR" additivity="true">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="DEBUG" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<logger name="myNewLog" level="INFO" additivity="false">
<appender-ref ref="myNewLog" />
</logger>
<root level="ERROR">
<appender-ref ref="console" />
</root>
</configuration>
我的问题是:
如何管理每个环境的日志级别?
我可以在一个logback文件中管理两个环境的级别吗?
1条答案
按热度按时间zwghvu4y1#
在application.yml中,为每个环境使用不同的概要文件。并定义级别var my.log.level
在logback.xml中,使用“”导入此变量。如果logback.xml无效,请将其重命名为logback-spring.xml以重试。
使用--spring.profiles.active运行。例如,使用dev运行