摘要:是由创始人设计的又一个开源日志组件。此外完整实现使你可以很方便地更换成其它日志系统如或。访问模块与容器集成提供通过来访问日志的功能。依赖配置的核心,包建议使用来管理日志,方便替换底层实现,要用,就在依赖中加入包和包。
依赖配置Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback-classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。
logback的核心,logback-core包
logback建议使用slf4j来管理日志,方便替换底层实现,要用slf4j,就在依赖中加入logback-classic包和slf4j-api包。
加入junit测试。
pom.xml:
配置文件4.0.0 com.fengyuan logback-demo 0.0.1-SNAPSHOT org.slf4j slf4j-api 1.7.12 ch.qos.logback logback-core 1.1.2 ch.qos.logback logback-classic 1.1.3 junit junit 4.12 org.apache.maven.plugins maven-compiler-plugin 1.8
在项目classpath下加入logback.xml:
%d{yyyy-MM-dd HH:mm:ss} [%level] - %m%n logs/error.log %d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n ERROR ACCEPT DENY error.%d{yyyy-MM-dd}.log.zip 30 logs/info.log %d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n INFO ACCEPT DENY info.%i.log 1 3 5MB logs/debug.log %d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n DEBUG ACCEPT DENY debug-%d{yyyy-MM-dd}.%i.log 100MB
包括日志文件的路径,保存的时间,已经滚动的策略都在配置文件中配置,具体看注释。配置文件是从棂枫的这篇博客里学习的,感谢。
测试测试类:
package com.fengyuan.client; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Main { private static Logger log = LoggerFactory.getLogger(Main.class); @Test public void testLogBack() { log.debug("I am debug"); log.info("I am info"); log.error("I am error"); try { throw new IllegalStateException("I am exception"); } catch (Exception e) { log.error(e.getMessage(), e); } } }
执行结果:
控制台输出:
10:35:19,739 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 10:35:19,739 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 10:35:19,739 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/scm-workspace/logback-demo/target/classes/logback.xml] 10:35:19,803 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 30 seconds 10:35:19,803 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[D:scm-workspacelogback-demo argetclasseslogback.xml]] every 30 seconds. 10:35:19,803 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter 10:35:19,810 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 10:35:19,812 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT] 10:35:19,826 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property 10:35:19,848 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender] 10:35:19,850 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [ERROR-OUT] 10:35:19,854 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property 10:35:19,866 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use zip compression 10:35:19,868 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern error.%d{yyyy-MM-dd}.log for the active file 10:35:19,875 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is "yyyy-MM-dd" from file name pattern "error.%d{yyyy-MM-dd}.log.zip". 10:35:19,875 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight. 10:35:19,877 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Fri Aug 26 10:35:19 CST 2016 10:35:19,879 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ERROR-OUT] - Active log file name: logs/error.log 10:35:19,879 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ERROR-OUT] - File property is set to [logs/error.log] 10:35:19,880 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender] 10:35:19,880 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [INFO-OUT] 10:35:19,881 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property 10:35:19,884 |-INFO in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@234bef66 - No compression will be used 10:35:19,887 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[INFO-OUT] - Active log file name: logs/info.log 10:35:19,887 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[INFO-OUT] - File property is set to [logs/info.log] 10:35:19,887 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender] 10:35:19,887 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [DEBUG-OUT] 10:35:19,888 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property 10:35:19,893 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used 10:35:19,893 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern debug-%d{yyyy-MM-dd}.%i.log for the active file 10:35:19,894 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@39fb3ab6 - The date pattern is "yyyy-MM-dd" from file name pattern "debug-%d{yyyy-MM-dd}.%i.log". 10:35:19,894 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@39fb3ab6 - Roll-over at midnight. 10:35:19,894 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@39fb3ab6 - Setting initial period to Fri Aug 26 10:35:19 CST 2016 10:35:19,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[DEBUG-OUT] - Active log file name: logs/debug.log 10:35:19,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[DEBUG-OUT] - File property is set to [logs/debug.log] 10:35:19,895 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.db.DBAppender] 10:35:19,899 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [DB] 10:35:20,088 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@63753b6d - Driver name=MySQL Connector Java 10:35:20,088 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@63753b6d - Driver version=mysql-connector-java-5.1.38 ( Revision: fe541c166cec739c74cc727c5da96c1028b4834a ) 10:35:20,088 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@63753b6d - supportsGetGeneratedKeys=true 10:35:20,090 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG 10:35:20,091 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 10:35:20,091 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [ERROR-OUT] to Logger[ROOT] 10:35:20,091 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [INFO-OUT] to Logger[ROOT] 10:35:20,091 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [DEBUG-OUT] to Logger[ROOT] 10:35:20,091 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 10:35:20,092 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@731a74c - Registering current configuration as safe fallback point 2016-08-26 10:35:20 [DEBUG] - I am debug 2016-08-26 10:35:20 [INFO] - I am info 2016-08-26 10:35:20 [ERROR] - I am error 2016-08-26 10:35:20 [ERROR] - I am exception java.lang.IllegalStateException: I am exception at com.fengyuan.client.Main.testLogBack(Main.java:19) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65] at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65] at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) [junit-4.12.jar:4.12] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12] at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) [junit-4.12.jar:4.12] at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) [junit-4.12.jar:4.12] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) [junit-4.12.jar:4.12] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12] at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
在项目路径下多了一个logs文件夹,里面有3个文件,每个文件记录对应的日志:
可以借助Lombok来简化代码的编写。
有了Lombok,只需要在要写日志的类中加入@Slf4j等注解,不用再写private static Logger log = LoggerFactory.getLogger(Main.class);
使用Lombok后的测试类:
package com.fengyuan.client; import org.junit.Test; import lombok.extern.slf4j.Slf4j; @Slf4j public class Main { @Test public void testLogBack() { log.debug("I am debug"); log.info("I am info"); log.error("I am error"); try { throw new IllegalStateException("I am exception"); } catch (Exception e) { log.error(e.getMessage(), e); } } }日志输出到数据库 建表
首先在数据库中建三张表,用来存储日志记录:
BEGIN; DROP TABLE IF EXISTS logging_event_property; DROP TABLE IF EXISTS logging_event_exception; DROP TABLE IF EXISTS logging_event; COMMIT; BEGIN; CREATE TABLE logging_event( timestmp BIGINT NOT NULL, formatted_message TEXT NOT NULL, logger_name VARCHAR(254) NOT NULL, level_string VARCHAR(254) NOT NULL, thread_name VARCHAR(254), reference_flag SMALLINT, arg0 VARCHAR(254), arg1 VARCHAR(254), arg2 VARCHAR(254), arg3 VARCHAR(254), caller_filename VARCHAR(254) NOT NULL, caller_class VARCHAR(254) NOT NULL, caller_method VARCHAR(254) NOT NULL, caller_line CHAR(4) NOT NULL, event_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ); COMMIT; BEGIN; CREATE TABLE logging_event_property( event_id BIGINT NOT NULL, mapped_key VARCHAR(254) NOT NULL, mapped_value TEXT, PRIMARY KEY(event_id, mapped_key), FOREIGN KEY (event_id) REFERENCES logging_event(event_id) ); COMMIT; BEGIN; CREATE TABLE logging_event_exception ( event_id BIGINT NOT NULL, i SMALLINT NOT NULL, trace_line VARCHAR(254) NOT NULL, PRIMARY KEY(event_id, i), FOREIGN KEY (event_id) REFERENCES logging_event(event_id) ); COMMIT;配置
在logback.xml中加入输出到数据库的appender,配置好数据源:
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/test root 123456
加入引用
加入数据库驱动依赖
这一步很多人容易忘记,配置好相应的配置之后,要记得加入mysql驱动的依赖:
mysql mysql-connector-java 5.1.38
然后,日志就会输出到数据表中了。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65082.html
摘要:本文要来分享给大家程序员最常用的日志框架组件。没有基础的同学也不要着急,这套教程覆盖了目前所有的日志框架,只要你学,就一定用得到,先收藏,以备不时之需。 作为一名Java程序员,我们开发了很多Java应用程序,包括桌面应用、WEB应用以及移动应用。然而日志系统是一个成熟Java应用所必不可少的。在开发和调试阶段,日志可以帮...
摘要:日志名的从开始,自动递增就是指抛出的异常,是显示全部,如果在中写入数字,则表示展示多少行重点来了,上边都是输出源。 一、简介 LogBack是一个日志框架,它是Log4j作者Ceki的又一个日志组件。 LogBack,Slf4j,Log4j之间的关系 slf4j是The Simple Logging Facade for Java的简称,是一个简单日志门面抽象框架,它本身只提供了日志F...
摘要:二环境准备编译器选择这里我们使用进行工作流开发,虽然对于工作流的友好度不是很好,因为会有一些小的,但是,对于的开发还是非常的好的。新建后出现下面的编辑页面到现在,编辑插件就准备好了。 文章源码托管:https://github.com/OUYANGSIHA...欢迎 star !!! 一、前言 在上一节中我们对activiti进行了基本的介绍activiti进行了基本的介绍,同时介绍了...
摘要:由于不是线程安全的,故在方法上增加了同步操作,造成竞争等待。至此,整个多线程调优结束,通过充分优化同步竞争的方式,最终使得分线程记录日志的性能比最原始的多线程写同一文件提高了倍去锁提高到倍,替换提高倍 背景 在一次项目的性能调优中,发现出现竞争瓶颈,导致在资源未使用满的情况下,TPS已经无法提升。祭起JMC(JAVA MISSON CONTROL)飞行记录器大法后,发现线程集中等待...
阅读 4132·2021-11-22 13:52
阅读 2499·2021-11-22 13:52
阅读 3671·2021-11-19 09:59
阅读 1173·2021-11-17 09:33
阅读 2434·2019-08-30 10:53
阅读 1189·2019-08-29 17:28
阅读 1295·2019-08-29 17:03
阅读 3087·2019-08-26 11:31