SpringBoot使用日志

1、日志框架

日志门面 日志实现
JCL、SLF4J、jboss-logging Log4j、JUL、Log4j2、Logback

日志门面:SLF4J

日志实现:Logback

SpringBoot:底层是Spring框架,Spring框架默认是用JCL;

SpringBoot选用SLF4J和Logback.

2、SLF4J使用

以后开发的时候,日志记录。日志记录方法的调用,不应直接调用日志的实现类,而是调用日志抽象层的方法。

应该给系统里倒入slf4j的jar和logback的实现jar。

1   import org.slf4j.Logger;
2   import org.slf4j.LoggerFactory;
3 4   public class HelloWorld {
5     public static void main(String[] args) {
6       Logger logger = LoggerFactory.getLogger(HelloWorld.class);
7       logger.info("Hello World");
8     }
9   }

每一个日志的实现框架都有自己的配置文件。使用slf4j以后,配置文件还是做成日志实现框自己本身的配置文件

3、遗留问题

框架A:

(slf4j+logback):Spring(commons-logging)、Hibernate(jboss-logging)、Mybatis、xxx

统一:统一使用slf4j+logback

如何让系统中的日志统一到slf4j:

1、将系统中的其它日志框架先排除出去

2、用中间包来替换所有的日志框架

3、我们倒入slf4j的其它实现

 

日志级别:

trace > debug > info > warn > error

 

The following example shows potential(默认的、潜在的) logging settings in application.properties:

1   logging.level.root=WARN
2   logging.level.org.springframework.web=DEBUG
3   logging.level.org.hibernate=ERROR

 

日志在yml中的相关配置:

 logging.level.com.itcast=info
 logging.path=日志保存的位置
 logging.file=D:/logging.log 日志的文件包含路径
 logging.pattern.console=console: %d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n 控制台输出日志的格式
 logging.pattern.file: %d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n 在指定文件中日志输出格式

 

logging.file logging.path Example Description
(none) (none)   Console only logging.
Specific file (none) my.log Writes to the specified log file. Names can be an exact location or relative to the current directory.
(none) Specific directory /var/log Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.

 

Depending on your logging system, the following files are loaded:

使用springboot加载不同方式的日志实现方式,需要使用的配置文件名称如下,存放位置在项目根目录下举例logback.xml

Logging System Customization
Logback logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
Log4j2 log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging) logging.properties

logback.xml

 1 xml version="1.0" encoding="UTF-8"?>
 2   
 7   <configuration scan="false" scanPeriod="60 seconds" debug="false">
 8       
 9       <property name="LOG_HOME" value="/app/log" />
10       
11       <property name="appName" value="atguigu-springboot">property>
12       
13       <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
14           
23           <layout class="ch.qos.logback.classic.PatternLayout">
24               <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
25           layout>
26       appender>
27 28         
29       <appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
30           
31           <file>${LOG_HOME}/${appName}.logfile>
32           
36           <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
37               
41               <fileNamePattern>${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.logfileNamePattern>
42               
47               <MaxHistory>365MaxHistory>
48               
51               <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
52                   <maxFileSize>100MBmaxFileSize>
53               timeBasedFileNamingAndTriggeringPolicy>
54           rollingPolicy>
55                
56           <layout class="ch.qos.logback.classic.PatternLayout">
57               <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%npattern>
58           layout>
59       appender>
60 61       
69       
70       <logger name="com.atguigu" level="debug" />
71       
72       <logger name="org.springframework" level="debug" additivity="false">logger>
73 74 75 76       
80       <root level="info">
81           <appender-ref ref="stdout" />
82           <appender-ref ref="appLogAppender" />
83       root>
84   configuration> 

 

You need to either use logback-spring.xml or define a logging.config property.

使用logging-spring.xml这种方式,可以根据不同配置环境配置日志的输出方式。

 1  <springProfile name="staging">
 2       
 3   springProfile>
 4  5   <springProfile name="dev, staging">
 6       
 7   springProfile>
 8  9   <springProfile name="!production">
10       
11   springProfile>

案例:

1 <layout class="ch.qos.logback.classic.PatternLayout">
2      <springProfile name="dev">
3           <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
4       springProfile>
5   layout>

 --------------------------------------------------------------------------------------------------------------------------------------------------------

相关文档网页支持:

1 1、https://docs.spring.io/spring-boot/docs/1.5.20.RELEASE/reference/htmlsingle/#howto-logging
3 2、https://www.slf4j.org/manual.html
5 3、https://www.slf4j.org/legacy.html

转载于:https://www.cnblogs.com/JohnEricCheng/p/10790382.html

你可能感兴趣的:(SpringBoot使用日志)