您现在的位置是:首页 >其他 >在Spring Cloud架构下使用Spring Boot的Logback日志处理方案网站首页其他
在Spring Cloud架构下使用Spring Boot的Logback日志处理方案
简介在Spring Cloud架构下使用Spring Boot的Logback日志处理方案
在Spring Cloud架构下使用Spring Boot的Logback日志处理方案,可以按照以下步骤进行:
1.引入logback依赖
在pom.xml文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
2.配置logback.xml文件
在src/main/resources目录下创建logback.xml文件,并添加以下配置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_HOME" value="/path/to/logs"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.example" level="DEBUG">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
其中,LOG_HOME为日志文件输出路径,可以根据需要进行修改。
3.logback.xml是Logback框架的配置文件,用于配置日志的输出格式、输出位置、日志级别等信息。
下面是logback.xml配置文件的主要内容:
3.1 配置输出位置
<property name="LOG_HOME" value="/path/to/logs"/>
3.2 配置输出格式
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
定义了日志输出的格式,包括时间、线程、日志级别、类名、日志信息等。
3.3 配置输出方式
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
定义了日志输出的方式,包括控制台输出和文件输出。其中,文件输出使用了滚动策略,即以时间为基准,每天生成一个
4.在应用程序中使用日志
在应用程序中使用日志,可以使用以下方式:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyClass {
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
public void myMethod() {
logger.debug("Debug log message");
logger.info("Info log message");
logger.warn("Warn log message");
logger.error("Error log message");
}
}
或者使用Lombok @Slf4j 注解 参考:https://blog.csdn.net/qq_20299791/article/details/89852458
5.logback.xml配置文件参考
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="3 seconds">
<property resource="application.yml"/>
<property name="LOG_PATH" value="/opt/log/"/>
<!-- <property name="LOG_PATH" value="/Users/changjiabo/opt/log/"/>-->
<springProperty scope="context" name="appName" source="spring.application.name" defaultValue="ho-cc"/>
<springProperty scope="context" name="env" source="spring.profiles.active"/>
<!-- 输出到控制台 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-6p) --- [%yellow(%15.15t)] [%X{requestId}] %cyan(%-40.40logger{39}) : %msg%n</Pattern>
</encoder>
</appender>
<!-- 业务日志2 -->
<appender name="flowlog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_PATH}${appName}/flowlog/flowlog.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}${appName}/flowlog/flowlog-%d{yyyyMMdd}-%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-6p --- [%15.15t] [%X{requestId}] %-40.40logger{39} : %msg%n</Pattern>
</layout>
</appender>
<!-- 访问详细日志 -->
<appender name="accDetail" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_PATH}${appName}/accDetail/accdetail.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}${appName}/accDetail/accdetail-%d{yyyyMMdd}-%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>1</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern> %msg%n </Pattern>
</layout>
</appender>
<!-- 需要上传elk日志 -->
<appender name="logELK" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_PATH}${appName}/metriclog/callCenter.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}${appName}/metriclog/callCenter-%d{yyyyMMdd}-%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>1</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%msg%n</Pattern>
</layout>
</appender>
<!-- 打印 mybatis sql -->
<logger name="com.wintelia" level="debug"/>
<logger name="com.elk.aspect" level="info" additivity="false">
<appender-ref ref="logELK"/>
</logger>
<logger name="com.accDetail.aspect" level="info" additivity="false">
<appender-ref ref="accDetail"/>
</logger>
<!-- 开发环境 -->
<springProfile name="dev,test">
<logger name="com.wintelia" level="debug"/>
<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
<logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>
<logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
<logger name="org.apache.sshd.common.util.SecurityUtils" level="WARN"/>
<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
<logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/>
<logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
</springProfile>
<springProfile name="prod,uat">
<logger name="com.wintelia" level="info"/>
</springProfile>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="flowlog"/>
</root>
</configuration>
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。