业务中用到根据不同设备来打印日志,直接上代码
<!-- 不同的业务逻辑日志打印到指定文件夹-->
<logger name="AB101" additivity="false" level="INFO">
<appender-ref ref="AB101Appender"/>
</logger>
<logger name="B200" additivity="false" level="INFO">
<appender-ref ref="B200Appender"/>
</logger>
<!--不同业务逻辑的日志打印到不同文件-->
<appender name="AB101Appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.path}/AB101.log</File>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/AB101.log.%d</fileNamePattern>
<maxHistory>12</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="B200Appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.path}/B200.log</File>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/B200.log.%d</fileNamePattern>
<maxHistory>12</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
以上配置加入到logback-spring.xml中,logger 的子标签appender-ref指向的是appender标签的name,都可以配置自己的路径、名称等。在用的时候需要通过输入名称来定义logger
private final static Logger AB101Logger = LoggerFactory.getLogger("AB101");
private final static Logger B200 = LoggerFactory.getLogger("B200");
AB101Logger.info("hahah");
B200.info("hahah");
效果如下
至此就可以根据自己的业务来分开打印,但是不会输出到控制台,输出在文件里
来源:CSDN
作者:小米辣加香菜
链接:https://blog.csdn.net/weixin_44372810/article/details/103566440