1.相关概念
日志统一框架(日志门面):apache commons logging、slf4j
日志实现框架(实现层):JDK自带的logging(java.util.logging)、log4j、Java Util Logging、log4j2、logback.
(1)JDK自带的logging(java.util.logging)用法:
1 importjava.util.logging.Logger;2
3 public classIfTest{4 private static final Logger logger=Logger.getLogger(IfTest.class.getName());5
6 public static voidmain(String args[]) {7 logger.info("Hello world!");8 }9
10 }
View Code
(2)log4j的使用:
log4j
log4j
1.2.17
View Code
1 importorg.apache.log4j.Logger;2
3 /**
4 * Hello world!5 *6 */
7 public classLog4jtest8 {9 private static final Logger logger=Logger.getLogger(Log4jtest.class);10
11 public static voidmain( String[] args )12 {13 logger.info("Hello world!");14 }15 }
View Code
相应的配置文件log4j.properties
log4j.rootLogger =debug, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern= %-d{yyyy-MM-dd HH:mm:ss} %m%n
View Code
(3)log4j2的使用:
org.apache.logging.log4j
log4j-api
2.2
org.apache.logging.log4j
log4j-core
2.2
View Code
1 importorg.apache.log4j.LogManager;2 importorg.apache.log4j.Logger;3
4 public classLog4j2test {5 private static final Logger logger=LogManager.getLogger(Log4j2test.class);6 public static voidmain(String[] args) {7 logger.info("Hello world!");8 }9 }
View Code
相应的配置文件log4j2.xml (目前log4j2只支持xml json yuml,不再支持properties文件)
1 <?xml version="1.0" encoding="UTF-8"?>
2
3
4
5
6
7
8
9
10
11
12
13
View Code
(4)logback的使用:
1
2
3 ch.qos.logback
4 logback-core
5 1.1.3
6
7
8 ch.qos.logback
9 logback-classic
10 1.1.3
11
12
13 org.slf4j
14 slf4j-api
15 1.7.12
16
17
View Code
1 importorg.slf4j.Logger;2 importorg.slf4j.LoggerFactory;3
4 public classLogbackTest {5 private static final Logger logger=LoggerFactory.getLogger(LogbackTest.class);6 public static voidmain(String[] args) {7 logger.info("Hello world!");8 }9 }
View Code
配置文件logback.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3
4
5
6 %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
7
8
9
10
11
12
13
14
View Code
2.spring boot中日志管理
Spring Boot uses Commons Logging for all internal logging, but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging,Log4J2 and Logback. In each case loggers are pre-configured to use console output with optional file output also available.
By default, If you use the ‘Starters’, Logback will be used for logging. Appropriate Logback routing is also included to ensure that dependent libraries that use Java Util Logging, Commons Logging, Log4J or SLF4J will all work correctly.
Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J, Log4J2和Logback。每种Logger都可以通过配置使用控制台或者文件输出日志内容。
如果使用了spring boot的Starter,默认情况下使用Logback作为日志管理。
直接在application.properties文件中配置:
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.file=/log/log/my.log
logging.pattern.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
或者单独配置Logback的配置文件。
Logging SystemCustomization
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
链接:https://pan.baidu.com/s/1ynSXl5xhZfpK7sqhcne0Zg
提取码:rip3
参考: