使用java自带的日志管理_java日志管理

1.相关概念

日志统一框架(日志门面):apache commons logging、slf4j

日志实现框架(实现层):JDK自带的logging(java.util.logging)、log4j、Java Util Logging、log4j2、logback.

(1)JDK自带的logging(java.util.logging)用法:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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的使用:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

log4j

log4j

1.2.17

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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的使用:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

org.apache.logging.log4j

log4j-api

2.2

org.apache.logging.log4j

log4j-core

2.2

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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文件)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

2

3

4

5

6

7

8

9

10

11

12

13

View Code

(4)logback的使用:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值