java日志管理
1.相关概念
日志统一框架(日志门面):apache commons logging、slf4j
日志实现框架(实现层):JDK自带的logging(java.util.logging)、log4j、Java Util Logging、log4j2、logback.
(1)JDK自带的logging(java.util.logging)用法:
1 import java.util.logging.Logger; 2 3 public class IfTest{ 4 private static final Logger logger=Logger.getLogger(IfTest.class.getName()); 5 6 public static void main(String args[]) { 7 logger.info("Hello world!"); 8 } 9 10 }
(2)log4j的使用:
<!-- log4j 依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
1 import org.apache.log4j.Logger; 2 3 /** 4 * Hello world! 5 * 6 */ 7 public class Log4jtest 8 { 9 private static final Logger logger=Logger.getLogger(Log4jtest.class); 10 11 public static void main( String[] args ) 12 { 13 logger.info("Hello world!"); 14 } 15 }
相应的配置文件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
(3)log4j2的使用:
<!-- log4j2 依赖 start --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.2</version> </dependency> <!-- log4j2 依赖 end -->
1 import org.apache.log4j.LogManager; 2 import org.apache.log4j.Logger; 3 4 public class Log4j2test { 5 private static final Logger logger=LogManager.getLogger(Log4j2test.class); 6 public static void main(String[] args) { 7 logger.info("Hello world!"); 8 } 9 }
相应的配置文件log4j2.xml (目前log4j2只支持xml json yuml,不再支持properties文件)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <Configuration status="WARN"> 3 <Appenders> 4 <Console name="Console" target="SYSTEM_OUT"> 5 <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> 6 </Console> 7 </Appenders> 8 <Loggers> 9 <Root level="debug"> 10 <AppenderRef ref="Console"/> 11 </Root> 12 </Loggers> 13 </Configuration>
(4)logback的使用:
1 <!-- logback 依赖 start --> 2 <dependency> 3 <groupId>ch.qos.logback</groupId> 4 <artifactId>logback-core</artifactId> 5 <version>1.1.3</version> 6 </dependency> 7 <dependency> 8 <groupId>ch.qos.logback</groupId> 9 <artifactId>logback-classic</artifactId> 10 <version>1.1.3</version> 11 </dependency> 12 <dependency> 13 <groupId>org.slf4j</groupId> 14 <artifactId>slf4j-api</artifactId> 15 <version>1.7.12</version> 16 </dependency> 17 <!-- logback 依赖 end -->
1 import org.slf4j.Logger; 2 import org.slf4j.LoggerFactory; 3 4 public class LogbackTest { 5 private static final Logger logger=LoggerFactory.getLogger(LogbackTest.class); 6 public static void main(String[] args) { 7 logger.info("Hello world!"); 8 } 9 }
配置文件logback.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 4 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 5 <encoder> 6 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> 7 </encoder> 8 </appender> 9 10 <root level="DEBUG"> 11 <appender-ref ref="STDOUT" /> 12 </root> 13 14 </configuration>
2.spring boot中日志管理
spring boot官方描述引用 https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/htmlsingle/#boot-features-logging
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 System | Customization |
---|---|
Logback |
|
Log4j2 |
|
JDK (Java Util Logging) |
|
链接:https://pan.baidu.com/s/1ynSXl5xhZfpK7sqhcne0Zg
提取码:rip3
参考: