第02章 Spring-Boot 日志文件logback配置

第02章 Spring-Boot 日志文件配置

前言

  • 为什么要使用 Spring-Boot?

    可参考官方文档。

  • 什么是 Spring-Boot?

    可参考官方文档说明。

  • 官方地址

    https://spring.io/projects/spring-boot

目标

  • 完成 Jenkins 在 Docker 中的安装与配置。
  • 安装在 Docker 中的 Jenkins 能正常对外提供服务。
  • 在外部开发环境中能正常访问和使用 Jenkins 持续集成服务。

环境

  • **VMware:**VMware Workstation 14 Pro

  • **Linux:**CentOS7.4

  • **Docker:**18.06.0-ce, build 0ffa825

  • **Jenkins:**Jenkins2.121.1

  • **JDK:**jdk1.8.0_172

  • **Spring-Boot:**2.0.4

  • **Eclipse:**Eclipse Neon.3 (4.6.3)

  • spring-tool-suite:

    Version: 3.8.4.RELEASE
    Build Id: 201703310825
    Platform: Eclipse Neon.3 (4.6.3)

  • **Maven:**3.5.0

支持日志框架

Java.Util.Logging
Log4J2
Logback  // 默认是使用logback

配置方式

默认配置文件配置和引用外部配置文件配置

默认配置文件配置

(不建议使用:不够灵活,对log4j2等不够友好)

# 日志文件名,比如:xxx.log,或者是 /var/log/xxx.log
logging.file=xxx.log 
# 日志级别配置,比如: logging.level.org.springframework=DEBUG
logging.level.*=info
logging.level.org.springframework=DEBUG

引用外部配置文件

logback 配置

spring boot默认会加载classpath:logback-spring.xml或者classpath:logback-spring.groovy

使用自定义配置文件,配置方式为:

logging.config=classpath:logback-xxx.xml

注:不要使用logback这个来命名,否则spring boot将不能完全实例化

基于spring boot的配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<include resource="org/springframework/boot/logging/logback/base.xml"/>
	<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
自定义配置
logging:
  config: classpath:logback-ringyin.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

	<!-- 文件输出格式 -->
	<property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" />
	<!-- test文件路径 -->
	<property name="TEST_FILE_PATH" value="c:/opt/roncoo/logs" />
	<!-- pro文件路径 -->
	<property name="PRO_FILE_PATH" value="/opt/roncoo/logs" />

	<!-- 开发环境 -->
	<springProfile name="dev">
		<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
			<encoder>
				<pattern>${PATTERN}</pattern>
			</encoder>
		</appender>
		
		<logger name="com.roncoo.education" level="debug"/>

		<root level="info">
			<appender-ref ref="CONSOLE" />
		</root>
	</springProfile>

	<!-- 测试环境 -->
	<springProfile name="test">
		<!-- 每天产生一个文件 -->
		<appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
			<!-- 文件路径 -->
			<file>${TEST_FILE_PATH}</file>
			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
				<!-- 文件名称 -->
				<fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
				<!-- 文件最大保存历史数量 -->
				<MaxHistory>100</MaxHistory>
			</rollingPolicy>
			
			<layout class="ch.qos.logback.classic.PatternLayout">
				<pattern>${PATTERN}</pattern>
			</layout>
		</appender>
		
		<root level="info">
			<appender-ref ref="TEST-FILE" />
		</root>
	</springProfile>

	<!-- 生产环境 -->
	<springProfile name="prod">
		<appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
			<file>${PRO_FILE_PATH}</file>
			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
				<fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
				<MaxHistory>100</MaxHistory>
			</rollingPolicy>
			<layout class="ch.qos.logback.classic.PatternLayout">
				<pattern>${PATTERN}</pattern>
			</layout>
		</appender>
		
		<root level="warn">
			<appender-ref ref="PROD_FILE" />
		</root>
	</springProfile>
</configuration>

log4j 配置

去除logback的依赖包,添加log4j2的依赖包
<exclusions>
   <exclusion>
	   <groupId>org.springframework.boot</groupId>
	   <artifactId>spring-boot-starter-logging</artifactId>
   </exclusion>
</exclusions>
<!-- 使用log4j2 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
在classpath添加log4j2.xml或者log4j2-spring.xml

(spring boot 默认加载)

自定义配置文件

logging.config=classpath:log4j2-dev.xml
# 可选配置文件
log4j2-dev.xml
log4j2-test.xml
log4j2-prod.xml

比较

性能比较

Log4J2 和 Logback 都优于 log4j(不推荐使用)

配置方式

spring boot 默认加载)**

自定义配置文件

logging.config=classpath:log4j2-dev.xml
# 可选配置文件
log4j2-dev.xml
log4j2-test.xml
log4j2-prod.xml

比较

性能比较

Log4J2 和 Logback 都优于 log4j(不推荐使用)

配置方式

Logback最简洁,spring boot默认,推荐使用

猜你喜欢

转载自blog.csdn.net/pointdew/article/details/108541345