ogback是由log4j创始人设计的又一个开源日志组件。是log4j的一个 改良版本。此外完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。
这部分网上已经被人写烂了,我就不写了,随便丢一个教程吧
https://blog.csdn.net/white_ice/article/details/85065219
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE_NAME_PATTERN" value="/usr/local/hppalogs/web/hppa-web.%d{yyyy-MM-dd}.%i.log"/>
<!-- 日志格式 -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%c){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<property name="FILE_LOG_PATTERN"
value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %c : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!--输出到文件-->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_NAME_PATTERN}</fileNamePattern>
<!-- 日志保留天数 -->
<maxHistory>366</maxHistory>
<!-- 日志文件上限大小,达到指定大小后删除旧的日志文件 -->
<totalSizeCap>2GB</totalSizeCap>
<!-- 每个日志文件的最大值 -->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
<logger name="com.baomidou.mybatisplus" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
Logger log = LoggerFactory.getLogger("log1");
Logger requestLog = LoggerFactory.getLogger("log2");
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/dbw-hppa-server.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/dbw-hppa-server.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
</rollingPolicy>
<append>true</append><!-- 日志追加 -->
<encoder>
<pattern>[%thread]%d{HH:mm:ss.SSS} %-5level %logger{100}[%line] - %msg %mdc %n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--单独打印接收报文日志-->
<appender name="requestLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/dbw-hppa-server-request.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/dbw-hppa-server-request.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
</rollingPolicy>
<append>true</append><!-- 日志追加 -->
<encoder>
<pattern>[%thread]%d{HH:mm:ss.SSS} %-5level %logger{100}[%line] - %msg %mdc %n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<logger name="log1" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="requestLogAppender"/>
</logger>
<logger name="log2" level="ERROR" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。