Spring boot 采用logback-spring.xml做日志管理

Spring boot 官网给出的参考:

When possible, we recommend that you use the -spring variants for your logging configuration (for example, logback-spring.xml rather than logback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.

Profile-specific Configuration

The <springProfile> tag lets you optionally include or exclude sections of configuration based on the active Spring profiles. Profile sections are supported anywhere within the <configuration> element. Use the name attribute to specify which profile accepts the configuration. The <springProfile> tag can contain a simple profile name (for example staging) or a profile expression. A profile expression allows for more complicated profile logic to be expressed, for exampleproduction & (eu-central | eu-west). Check the reference guide for more details. The following listing shows three sample profiles:

 

官网地址:https://docs.spring.io/spring-boot/docs/2.1.2.RELEASE/reference/htmlsingle/

1、Spring boot 框架中配置logback-spring.xml

 首先将logback-spring.xml放在项目目录的resource下,然后在项目的配置文件application.yml加入如下配置:

#日志配置
logging:
  config: classpath:logback-spring.xml

直接给出logback-spring.xml配置文件供参考:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

	<!-- 格式化输出:%date表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度 %msg:日志消息,%n是换行符-->
	<property name="LOG_PATTERN"
			  value="%red(%date{HH:mm:ss.SSS})  %green([%thread] %-5level %logger{36} - %msg%n)" />

	<!-- 开发、测试环境 -->
	<springProfile name="dev,test">
		<!-- 定义日志存储的路径,不要配置相对路径 F:\正反斜杠有区别 -->
		<!-- F:\正反斜杠有区别 -->
		<property name="FILE_PATH" value="F:\logs/stdout.log.%d{yyyy-MM-dd}.%i.log" />
	</springProfile>

	<!-- 生產環境 -->
	<springProfile name="pro">
		<!-- 定义日志存储的路径,不要配置相对路径 -->
		<property name="FILE_PATH" value="/user/lib/cccf/logs/cccf.%d{yyyy-MM-dd}.%i.log" />
	</springProfile>

	<!-- 控制台输出日志 -->
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<!-- 按照上面配置的LOG_PATTERN来打印日志 -->
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
	</appender>

	<!--每天生成一个日志文件,保存15天的日志文件。rollingFile是用来切分文件的 -->
	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 设置按尺寸和时间(同时满足)分割 -->
		<rollingPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<!-- rollover daily -->
			<fileNamePattern>cdlgb_logs\%d{yyyy-MM-dd}/stdout.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
			<!-- each file should be at most 10MB, keep 15 days worth of history,
				but at most 3GB -->
			<maxFileSize>10MB</maxFileSize>
			<maxHistory>15</maxHistory>
			<totalSizeCap>1GB</totalSizeCap>
		</rollingPolicy>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}-%msg%n</pattern>
			<charset>UTF-8</charset> <!-- 此处设置字符集 -->
		</encoder>
	</appender>

	<!-- project default level -->
	<logger name="com.hiynn.cccf" level="INFO" />

	<!-- 日志输出级别:DEBUG,INFO,WARN,ERROR四种-->
	<root level="INFO">
		<appender-ref ref="console" />
		<appender-ref ref="FILE" />
	</root>
</configuration>

我们的配置文件实现以下功能点:

1、日志输出格式自定义。

2、保留最近N天的日志。

3、按日期每天创建文件夹管理日志。

4、每个文件下日志文件大小超过10M就会创建新的日志文件,方便从庞大日志文件中排查错误。

5、实现多环境日志管理设置。

6、对日志级别进行管理。

2、最终结果展示:

最近半个月的日志:

每天日志详情:

 转载请注明原创,请尊重原创。

 

您可以通过在 logback-spring.xml 文件中添加以下配置来为日志添加颜色: ``` <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> <charset>UTF-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> </root> <springProfile name="dev"> <logger name="com.example" level="DEBUG" /> </springProfile> <springProfile name="prod"> <logger name="com.example" level="WARN" /> </springProfile> <springProfile name="test"> <logger name="com.example" level="DEBUG" /> </springProfile> <springProfile name="!test"> <logger name="org.springframework.web" level="WARN" /> </springProfile> <!-- 添加以下配置以为日志添加颜色 --> <conversionRule conversionWord="%clr(%level)" levelMin="DEBUG" levelMax="INFO" next="[%thread] %logger{36}.%M - %msg%n"/> <conversionRule conversionWord="%clr(%level)" levelMin="WARN" levelMax="ERROR" next="[%thread] %logger{36}.%M - %msg%n"/> <conversionRule conversionWord="%clr(%exception)" levelMin="ERROR" levelMax="ERROR" next="%n"/> </configuration> ``` 这将为 DEBUG 和 INFO 级别的日志添加颜色。 如果您使用的是 Spring Boot,则您可以在 application.properties 或 application.yml 文件中使用以下属性来配置彩色日志: ``` logging.console.level=info spring.profiles.active=dev # 添加以下属性以为日志添加颜色 logging.pattern.level=%clr(%5p) logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} [%C:%L] --- [%t] %-40.40logger{39} : %m%n%wex logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} [%C:%L] --- [%t] %-40.40logger{39} : %clr(${LOG_EXCEPTION_CONVERSION_WORD:-%5p})%m%n%wex ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值