常用的日志框架

参考: https://www.bilibili.com/video/BV1iJ411H74S?from=search&seid=17788791804221837810

  • Loggers:被称为记录器,应用程序通过获取Logger对象,调用其API来来发布日志信息。
  • Appenders:(Handlers),每个Logger都会关联一组Handlers,Logger会将日志交给关联Handlers处理,由Handlers负责将日志做记录。Handlers在此是一个抽象,其具体的实现决定了日志记录的位置可以是控制台、文件、网络上的其他日志服务或操作系统日志等。
  • Layouts:也被称为Formatters,它负责对日志事件中的数据进行转换和格式化。
  • Level:每条日志消息都有一个关联的日志级别。我可以将Level和Loggers,Appenders做关联以便于我们过滤消息。
  • Filters:过滤器

JUL(Java util Logging)

使用示例

  1. public static void main(String[] args) {
  2. Logger logger = Logger.getLogger("top.xinzhang0618.springboot.demo.TestMain");
  3. logger.log(Level.INFO, "测试JUL! 参数: {0}, 参数: {1}", new Object[]{111, 222});
  4. }
  5. -------------运行结果
  6. "C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=62947:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;D:\DemoProject\springboot-demo\target\classes;C:\XZrepository\org\springframework\boot\spring-boot-starter-web\2.3.0.RELEASE\spring-boot-starter-web-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter\2.3.0.RELEASE\spring-boot-starter-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot\2.3.0.RELEASE\spring-boot-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-autoconfigure\2.3.0.RELEASE\spring-boot-autoconfigure-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-logging\2.3.0.RELEASE\spring-boot-starter-logging-2.3.0.RELEASE.jar;C:\XZrepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\XZrepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\XZrepository\org\apache\logging\log4j\log4j-to-slf4j\2.13.2\log4j-to-slf4j-2.13.2.jar;C:\XZrepository\org\apache\logging\log4j\log4j-api\2.13.2\log4j-api-2.13.2.jar;C:\XZrepository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;C:\XZrepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\XZrepository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-json\2.3.0.RELEASE\spring-boot-starter-json-2.3.0.RELEASE.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-databind\2.11.0\jackson-databind-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-annotations\2.11.0\jackson-annotations-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-core\2.11.0\jackson-core-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.0\jackson-datatype-jdk8-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.0\jackson-datatype-jsr310-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.0\jackson-module-parameter-names-2.11.0.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-tomcat\2.3.0.RELEASE\spring-boot-starter-tomcat-2.3.0.RELEASE.jar;C:\XZrepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.35\tomcat-embed-core-9.0.35.jar;C:\XZrepository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;C:\XZrepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.35\tomcat-embed-websocket-9.0.35.jar;C:\XZrepository\org\springframework\spring-web\5.2.6.RELEASE\spring-web-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-beans\5.2.6.RELEASE\spring-beans-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-webmvc\5.2.6.RELEASE\spring-webmvc-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-context\5.2.6.RELEASE\spring-context-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-expression\5.2.6.RELEASE\spring-expression-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-aop\2.3.0.RELEASE\spring-boot-starter-aop-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\spring-aop\5.2.6.RELEASE\spring-aop-5.2.6.RELEASE.jar;C:\XZrepository\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;C:\XZrepository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\XZrepository\org\springframework\spring-core\5.2.6.RELEASE\spring-core-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-jcl\5.2.6.RELEASE\spring-jcl-5.2.6.RELEASE.jar;C:\XZrepository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar;C:\XZrepository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.1.2\mybatis-spring-boot-starter-2.1.2.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-jdbc\2.3.0.RELEASE\spring-boot-starter-jdbc-2.3.0.RELEASE.jar;C:\XZrepository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;C:\XZrepository\org\springframework\spring-jdbc\5.2.6.RELEASE\spring-jdbc-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-tx\5.2.6.RELEASE\spring-tx-5.2.6.RELEASE.jar;C:\XZrepository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.1.2\mybatis-spring-boot-autoconfigure-2.1.2.jar;C:\XZrepository\org\mybatis\mybatis\3.5.4\mybatis-3.5.4.jar;C:\XZrepository\org\mybatis\mybatis-spring\2.0.4\mybatis-spring-2.0.4.jar" top.xinzhang0618.springboot.demo.TestMain
  7. 五月 22, 2020 9:36:33 下午 top.xinzhang0618.springboot.demo.TestMain main
  8. 信息: 测试JUL! 参数: 111, 参数: 222

jul中定义的日志级别
SEVERE(最高值),WARNING, INFO (默认级别), CONFIG, FINE(用来DEBUG输出), FINER, FINEST(最低值)
还有两个特殊的级别:OFF,可用来关闭日志记录。ALL,启用所有消息的日志记录
默认日志输出级别是INFO, 即INFO级别以上的会输出, 其余会被过滤

自定义日志配置

  1. public static void main(String[] args) throws IOException {
  2. Logger logger = Logger.getLogger("top.xinzhang0618.springboot.demo.TestMain");
  3. logger.setUseParentHandlers(false);
  4. // 控制台
  5. ConsoleHandler consoleHandler = new ConsoleHandler();
  6. SimpleFormatter simpleFormatter = new SimpleFormatter();
  7. consoleHandler.setFormatter(simpleFormatter);
  8. logger.addHandler(consoleHandler);
  9. logger.setLevel(Level.INFO);
  10. consoleHandler.setLevel(Level.INFO);
  11. // 日志文件
  12. FileHandler fileHandler = new FileHandler("D:\\soft\\log.txt");
  13. fileHandler.setFormatter(simpleFormatter);
  14. logger.addHandler(fileHandler);
  15. logger.info("测试自定义日志配置");
  16. }

Logger的父子关系
JUL中Logger之间存在父子关系, 父子关系通过路径来关联;
如下 :logger2是logger1的父, LoggerManager$RootLogger, name是””为logger2的父
logger1会默认使用logger2的handler, 使用logger.setUseParentHandlers(false);关闭

  1. // 日志记录器对象父子关系
  2. Logger logger1 = Logger.getLogger("com.itheima.log");
  3. Logger logger2 = Logger.getLogger("com.itheima");

通过日志配置文件自定义日志配置
默认配置文件路径$JAVAHOME\jre\lib\logging.properties

  1. public static void main(String[] args) throws IOException {
  2. InputStream ins = TestMain.class.getClassLoader().getResourceAsStream("logging.properties");
  3. LogManager logManager = LogManager.getLogManager();
  4. logManager.readConfiguration(ins);
  5. Logger logger = Logger.getLogger("top.xinzhang0618.springboot.demo.TestMain");
  6. logger.info("测试Info日志记录");
  7. logger.fine("测试通过配置文件自定义日志fine级别");
  8. }
  1. # RootLogger使用的处理器(获取时设置)
  2. handlers= java.util.logging.ConsoleHandler
  3. # RootLogger日志等级
  4. .level= INFO
  5. ## 自定义Logger
  6. top.xinzhang0618.springboot.demo.handlers= java.util.logging.ConsoleHandler, java.util.logging.FileHandler
  7. # 自定义Logger日志等级
  8. top.xinzhang0618.springboot.demo.level= FINE
  9. # 忽略父日志设置
  10. top.xinzhang0618.springboot.demo.useParentHandlers=false
  11. ## 控制台处理器
  12. # 输出日志级别
  13. java.util.logging.ConsoleHandler.level = FINE
  14. # 输出日志格式
  15. java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
  16. ## 文件处理器
  17. # 输出日志级别
  18. java.util.logging.FileHandler.level=FINE
  19. # 输出日志格式
  20. java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
  21. # 输出日志文件路径
  22. java.util.logging.FileHandler.pattern = d://soft/java%u.log
  23. # 输出日志文件限制大小(50000字节)
  24. java.util.logging.FileHandler.limit = 50000
  25. # 输出日志文件限制个数
  26. java.util.logging.FileHandler.count = 10
  27. # 输出日志文件 是否是追加
  28. java.util.logging.FileHandler.append=true

日志原理解析
image.png

Log4j

官方网站: http://logging.apache.org/log4j/1.2/

使用示例

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-logging</artifactId>
  4. </dependency>
  1. public static void main(String[] args) {
  2. Logger logger = LoggerFactory.getLogger(TestMain.class);
  3. // 日志级别
  4. // 错误信息,但不会影响系统运行
  5. logger.error("error");
  6. // 警告信息,可能会发生问题
  7. logger.warn("warn");
  8. // 程序运行信息,数据库的连接、网络、IO操作等
  9. logger.info("info");
  10. // 调试信息,一般在开发阶段使用,记录程序的变量、参数等
  11. logger.debug("debug");
  12. // 追踪信息,记录程序的所有流程信息
  13. logger.trace("trace");
  14. }
  15. ----------------控制台输出
  16. "C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=52687:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;D:\DemoProject\springboot-demo\target\classes;C:\XZrepository\org\springframework\boot\spring-boot-starter-web\2.3.0.RELEASE\spring-boot-starter-web-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter\2.3.0.RELEASE\spring-boot-starter-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot\2.3.0.RELEASE\spring-boot-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-autoconfigure\2.3.0.RELEASE\spring-boot-autoconfigure-2.3.0.RELEASE.jar;C:\XZrepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\XZrepository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-json\2.3.0.RELEASE\spring-boot-starter-json-2.3.0.RELEASE.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-databind\2.11.0\jackson-databind-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-annotations\2.11.0\jackson-annotations-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-core\2.11.0\jackson-core-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.0\jackson-datatype-jdk8-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.0\jackson-datatype-jsr310-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.0\jackson-module-parameter-names-2.11.0.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-tomcat\2.3.0.RELEASE\spring-boot-starter-tomcat-2.3.0.RELEASE.jar;C:\XZrepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.35\tomcat-embed-core-9.0.35.jar;C:\XZrepository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;C:\XZrepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.35\tomcat-embed-websocket-9.0.35.jar;C:\XZrepository\org\springframework\spring-web\5.2.6.RELEASE\spring-web-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-beans\5.2.6.RELEASE\spring-beans-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-webmvc\5.2.6.RELEASE\spring-webmvc-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-context\5.2.6.RELEASE\spring-context-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-expression\5.2.6.RELEASE\spring-expression-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-aop\2.3.0.RELEASE\spring-boot-starter-aop-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\spring-aop\5.2.6.RELEASE\spring-aop-5.2.6.RELEASE.jar;C:\XZrepository\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-logging\2.3.0.RELEASE\spring-boot-starter-logging-2.3.0.RELEASE.jar;C:\XZrepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\XZrepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\XZrepository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\XZrepository\org\apache\logging\log4j\log4j-to-slf4j\2.13.2\log4j-to-slf4j-2.13.2.jar;C:\XZrepository\org\apache\logging\log4j\log4j-api\2.13.2\log4j-api-2.13.2.jar;C:\XZrepository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;C:\XZrepository\org\springframework\spring-core\5.2.6.RELEASE\spring-core-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-jcl\5.2.6.RELEASE\spring-jcl-5.2.6.RELEASE.jar;C:\XZrepository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar;C:\XZrepository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.1.2\mybatis-spring-boot-starter-2.1.2.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-jdbc\2.3.0.RELEASE\spring-boot-starter-jdbc-2.3.0.RELEASE.jar;C:\XZrepository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;C:\XZrepository\org\springframework\spring-jdbc\5.2.6.RELEASE\spring-jdbc-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-tx\5.2.6.RELEASE\spring-tx-5.2.6.RELEASE.jar;C:\XZrepository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.1.2\mybatis-spring-boot-autoconfigure-2.1.2.jar;C:\XZrepository\org\mybatis\mybatis\3.5.4\mybatis-3.5.4.jar;C:\XZrepository\org\mybatis\mybatis-spring\2.0.4\mybatis-spring-2.0.4.jar" top.xinzhang0618.springboot.demo.TestMain
  17. 14:13:28.308 [main] ERROR top.xinzhang0618.springboot.demo.TestMain - error
  18. 14:13:28.313 [main] WARN top.xinzhang0618.springboot.demo.TestMain - warn
  19. 14:13:28.314 [main] INFO top.xinzhang0618.springboot.demo.TestMain - info
  20. 14:13:28.314 [main] DEBUG top.xinzhang0618.springboot.demo.TestMain - debug

Log4j组件

  • Logger

日志记录器, 负责收集处理日志记录; 实例的命名是类的全限定名, Logger的名字大小写敏感, 有继承机制
根logger为RootLogger, name为”ROOT”

  • Appenders

日志的输出地
image.png

  1. #指定日志的输出级别与输出端
  2. log4j.rootLogger=INFO,Console
  3. # 控制台输出配置
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender
  5. log4j.appender.Console.layout=org.apache.log4j.PatternLayout
  6. log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
  7. # 文件输出配置
  8. log4j.appender.A = org.apache.log4j.DailyRollingFileAppender
  9. #指定日志的输出路径
  10. log4j.appender.A.File = D:/log.txt
  11. log4j.appender.A.Append = true
  12. #使用自定义日志格式化器
  13. log4j.appender.A.layout = org.apache.log4j.PatternLayout
  14. #指定日志的输出格式
  15. log4j.appender.A.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r] -
  16. [%p] %m%n
  17. #指定日志的文件编码
  18. log4j.appender.A.encoding=UTF-8
  19. #mysql
  20. log4j.appender.logDB=org.apache.log4j.jdbc.JDBCAppender
  21. log4j.appender.logDB.layout=org.apache.log4j.PatternLayout
  22. log4j.appender.logDB.Driver=com.mysql.jdbc.Driver
  23. log4j.appender.logDB.URL=jdbc:mysql://localhost:3306/test
  24. log4j.appender.logDB.User=root
  25. log4j.appender.logDB.Password=root
  26. log4j.appender.logDB.Sql=INSERT INTO
  27. log(project_name,create_date,level,category,file_name,thread_name,line,all_categ
  28. ory,message) values('itcast','%d{yyyy-MM-dd
  29. HH:mm:ss}','%p','%c','%F','%t','%L','%l','%m')
  1. CREATE TABLE `log` (
  2. `log_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `project_name` varchar(255) DEFAULT NULL COMMENT '目项名',
  4. `create_date` varchar(255) DEFAULT NULL COMMENT '创建时间',
  5. `level` varchar(255) DEFAULT NULL COMMENT '优先级',
  6. `category` varchar(255) DEFAULT NULL COMMENT '所在类的全名',
  7. `file_name` varchar(255) DEFAULT NULL COMMENT '输出日志消息产生时所在的文件名称 ',
  8. `thread_name` varchar(255) DEFAULT NULL COMMENT '日志事件的线程名',
  9. `line` varchar(255) DEFAULT NULL COMMENT '号行',
  10. `all_category` varchar(255) DEFAULT NULL COMMENT '日志事件的发生位置',
  11. `message` varchar(4000) DEFAULT NULL COMMENT '输出代码中指定的消息',
  12. PRIMARY KEY (`log_id`)
  13. );
  • Layouts

布局器, 用于控制日志输出内容的格式
image.png

  1. log4j 采用类似 C 语言的 printf 函数的打印格式格式化日志信息,具体的占位符及其含义如下:
  2. %m 输出代码中指定的日志信息
  3. %p 输出优先级,及 DEBUGINFO
  4. %n 换行符(Windows平台的换行符为 "\n"Unix 平台为 "\n"
  5. %r 输出自应用启动到输出该 log 信息耗费的毫秒数
  6. %c 输出打印语句所属的类的全名
  7. %t 输出产生该日志的线程全名
  8. %d 输出服务器当前时间,默认为 ISO8601,也可以指定格式,如:%d{yyyyMMdd
  9. HH:mm:ss}
  10. %l 输出日志时间发生的位置,包括类名、线程、及在代码中的行数。如:
  11. Test.main(Test.java:10)
  12. %F 输出日志消息产生时所在的文件名称
  13. %L 输出代码中的行号
  14. %% 输出一个 "%" 字符
  15. * 可以在 % 与字符之间加上修饰符来控制最小宽度、最大宽度和文本的对其方式。如:
  16. %5c 输出category名称,最小宽度是5category<5,默认的情况下右对齐
  17. %-5c 输出category名称,最小宽度是5category<5"-"号指定左对齐,会有空格
  18. %.5c 输出category名称,最大宽度是5category>5,就会将左边多出的字符截掉,<5
  19. 会有空格
  20. %20.30c category名称<20补空格,并且右对齐,>30字符,就从左边交远销出的字符截掉

**

Log4j2

官网: https://logging.apache.org/log4j/2.x/

Apache Log4j 2是对Log4j的升级版,参考了logback的一些优秀的设计,并且修复了一些问题,因此带来了一些重大的提升,主要有:
- 异常处理,在logback中,Appender中的异常不会被应用感知到,但是在log4j2中,提供了一些异常处理机制。
- 性能提升, log4j2相较于log4j 和logback都具有很明显的性能提升,后面会有官方测试的数据。
- 自动重载配置,参考了logback的设计,当然会提供自动刷新参数配置,最实用的就是我们在生产上可以动态的修改日志的级别而不需要重启应用。
- 无垃圾机制,log4j2在大部分情况下,都可以使用其设计的一套无垃圾机制,避免频繁的日志收集导致的jvm gc。

**

JCL(Jakarta Commons Logging)

它是为 “所有的Java日志实现”提供一个统一的接口,它自身也提供一个日志的实现,但是功能非常常弱
(SimpleLog)。所以一般不会单独使用它。他允许开发人员使用不同的具体日志实现工具: Log4j, Jdk自带的日志(JUL)

使用示例

  1. <dependency>
  2. <groupId>commons-logging</groupId>
  3. <artifactId>commons-logging</artifactId>
  4. <version>1.2</version>
  5. </dependency>
  1. public static void main(String[] args) {
  2. // 创建日志对象
  3. Log log = LogFactory.getLog(TestMain.class);
  4. // 日志记录输出
  5. log.fatal("fatal");
  6. log.error("error");
  7. log.warn("warn");
  8. log.info("info");
  9. log.debug("debug");
  10. }
  11. --------------
  12. "C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=55607:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;D:\DemoProject\springboot-demo\target\classes;C:\XZrepository\org\springframework\boot\spring-boot-starter-web\2.3.0.RELEASE\spring-boot-starter-web-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter\2.3.0.RELEASE\spring-boot-starter-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot\2.3.0.RELEASE\spring-boot-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-autoconfigure\2.3.0.RELEASE\spring-boot-autoconfigure-2.3.0.RELEASE.jar;C:\XZrepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\XZrepository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-json\2.3.0.RELEASE\spring-boot-starter-json-2.3.0.RELEASE.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-databind\2.11.0\jackson-databind-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-annotations\2.11.0\jackson-annotations-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\core\jackson-core\2.11.0\jackson-core-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.0\jackson-datatype-jdk8-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.0\jackson-datatype-jsr310-2.11.0.jar;C:\XZrepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.0\jackson-module-parameter-names-2.11.0.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-tomcat\2.3.0.RELEASE\spring-boot-starter-tomcat-2.3.0.RELEASE.jar;C:\XZrepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.35\tomcat-embed-core-9.0.35.jar;C:\XZrepository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;C:\XZrepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.35\tomcat-embed-websocket-9.0.35.jar;C:\XZrepository\org\springframework\spring-web\5.2.6.RELEASE\spring-web-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-beans\5.2.6.RELEASE\spring-beans-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-webmvc\5.2.6.RELEASE\spring-webmvc-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-context\5.2.6.RELEASE\spring-context-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-expression\5.2.6.RELEASE\spring-expression-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-aop\2.3.0.RELEASE\spring-boot-starter-aop-2.3.0.RELEASE.jar;C:\XZrepository\org\springframework\spring-aop\5.2.6.RELEASE\spring-aop-5.2.6.RELEASE.jar;C:\XZrepository\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-logging\2.3.0.RELEASE\spring-boot-starter-logging-2.3.0.RELEASE.jar;C:\XZrepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\XZrepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\XZrepository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\XZrepository\org\apache\logging\log4j\log4j-to-slf4j\2.13.2\log4j-to-slf4j-2.13.2.jar;C:\XZrepository\org\apache\logging\log4j\log4j-api\2.13.2\log4j-api-2.13.2.jar;C:\XZrepository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;C:\XZrepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\XZrepository\org\springframework\spring-core\5.2.6.RELEASE\spring-core-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-jcl\5.2.6.RELEASE\spring-jcl-5.2.6.RELEASE.jar;C:\XZrepository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar;C:\XZrepository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.1.2\mybatis-spring-boot-starter-2.1.2.jar;C:\XZrepository\org\springframework\boot\spring-boot-starter-jdbc\2.3.0.RELEASE\spring-boot-starter-jdbc-2.3.0.RELEASE.jar;C:\XZrepository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;C:\XZrepository\org\springframework\spring-jdbc\5.2.6.RELEASE\spring-jdbc-5.2.6.RELEASE.jar;C:\XZrepository\org\springframework\spring-tx\5.2.6.RELEASE\spring-tx-5.2.6.RELEASE.jar;C:\XZrepository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.1.2\mybatis-spring-boot-autoconfigure-2.1.2.jar;C:\XZrepository\org\mybatis\mybatis\3.5.4\mybatis-3.5.4.jar;C:\XZrepository\org\mybatis\mybatis-spring\2.0.4\mybatis-spring-2.0.4.jar" top.xinzhang0618.springboot.demo.TestMain
  13. 14:51:01.635 [main] ERROR top.xinzhang0618.springboot.demo.TestMain - fatal
  14. 14:51:01.640 [main] ERROR top.xinzhang0618.springboot.demo.TestMain - error
  15. 14:51:01.640 [main] WARN top.xinzhang0618.springboot.demo.TestMain - warn
  16. 14:51:01.640 [main] INFO top.xinzhang0618.springboot.demo.TestMain - info
  17. 14:51:01.640 [main] DEBUG top.xinzhang0618.springboot.demo.TestMain - debug

**

SLF4J(Simple Logging Facade For Java)

官方网站: https://www.slf4j.org/

SLF4J主要是为了给Java日志访问提供一套标准、规范的API框架,其主要意义在于提供接口,具体的实现可以交由其他日志框架
image.png

**

Logback


官方网站:https://logback.qos.ch/index.html
Logback是由log4j创始人设计的另一个开源日志组件,性能比log4j要好

**
Logback主要分为三个模块:

  • logback-core:其它两个模块的基础模块
  • logback-classic:它是log4j的一个改良版本,同时它完整实现了slf4j API
  • logback-access:访问模块与Servlet容器集成提供通过Http来访问日志的功能

logback会依次读取以下类型配置文件:
logback.groovy
logback-test.xml
logback.xml
如果均不存在会采用默认配置

Logback组件: Logger, Appender, Layout

SpringBoot中的日志使用

  1. <dependency>
  2. <artifactId>spring-boot-starter-logging</artifactId>
  3. <groupId>org.springframework.boot</groupId>
  4. </dependency>

image.png
修改默认日志配置
image.png

补充: 将日志切换为log4j2

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <!--排除logback-->
  6. <exclusion>
  7. <artifactId>spring-boot-starter-logging</artifactId>
  8. <groupId>org.springframework.boot</groupId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <!-- 添加log4j2 -->
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-log4j2</artifactId>
  16. </dependency>