日志组件 logback
一、简介
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来访问日志的功能。
二、slf4j和logback的使用
1.如果日志的参数超过3个,需要写成
Object[] params = {newVal, below, above};
logger.debug("Value {} was inserted between {} and {}.", params);
2.因为内部已优化,作者认为slf4j的logger不需要定义为static。
3.可设置缓存后批量写日志文件(但服务器如果重启,可能会丢失未写到磁盘的记录)
4.MDC,用Filter,将当前用户名等业务信息放入MDC中,在日志format定义中即可使用该变量。
5.JMS Appender用于告警, DB Appender用于业务日志等可以使用插件,如生成Log代码的Eclipse插件Log4E。
6.tomcat和glassfish中,设定日志路径为../logs/xxxx.log 都能将日志放入应用服务器本身的logs目录
三、例子
准备相关工具
1. 从网上下载slf4j组件和logback相关的jar包,很多网站都有,logback可以直接到这个网站下载http://logback.qos.ch/,http://www.slf4j.org/可以直接到这里下载http://www.slf4j.org/。总之,不管在哪里下载,你都需要找到以下几个jar包(可能根据需要,有个别不需要,这个没有详细研究,读者可以自己研究下):
1) logback-acces-0.9.18.jar
2) logback-classic-0.9.18.jar
3) logback-core-0.9.18.jar
4) slf4j-api-1.5.6.jar
2、实现记录日志
1)建立一个JAVA工程,比如是LogTest,工程整体目录结构如下,工程根目录除了src目录外,新建一个lib目录,将上边4个jar包放入并导入工程。再新建一个config目录,用于存放日志配 置文件logback.xml,这个文件如何配置,后边再介绍。
2)配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<substitutionProperty name="log.base" value="d:\\logback\\logback" />
<jmxConfigurator />
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator name="myEval">
<expression>message.contains("dao")</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</layout>
</appender>
<appender name="logfile-dao"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator name="myEval_dao">
<expression>message.contains("dao")</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<Encoding>UTF-8</Encoding>
<File>${log.base}_dao.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.base}.%d{yyyy-MM-dd}_dao.log.zip
</FileNamePattern>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</layout>
</appender>
<appender name="logfile-service"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator name="myEval_service">
<expression>message.contains("service.impl")</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<Encoding>UTF-8</Encoding>
<File>${log.base}_service.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.base}.%d{yyyy-MM-dd}_service.log.zip
</FileNamePattern>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</layout>
</appender>
<appender name="jms_dao" class="ch.qos.logback.classic.net.JMSQueueAppender">
<InitialContextFactoryName>
org.apache.activemq.jndi.ActiveMQInitialContextFactory
</InitialContextFactoryName>
<ProviderURL>tcp://192.168.1.120:61616</ProviderURL>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator name="myEval_service">
<expression>message.contains("dao")</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<QueueConnectionFactoryBindingName>ConnectionFactory
</QueueConnectionFactoryBindingName>
<QueueBindingName>cms_dao_log</QueueBindingName>
</appender>
<appender name="jms_service" class="ch.qos.logback.classic.net.JMSQueueAppender">
<InitialContextFactoryName>
org.apache.activemq.jndi.ActiveMQInitialContextFactory
</InitialContextFactoryName>
<ProviderURL>tcp://192.168.1.120:61616</ProviderURL>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator name="myEval_service">
<expression>message.contains("service.impl")</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<QueueConnectionFactoryBindingName>ConnectionFactory
</QueueConnectionFactoryBindingName>
<QueueBindingName>cms_service_log</QueueBindingName>
</appender>
<logger name="com.cms5.cmsservice.jms">
<level value="DEBUG" />
</logger>
<logger name="java.sql.PreparedStatement">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Connection">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Statement">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis.common.jdbc.SimpleDataSource">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis.common.jdbc.ScriptRunner">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate">
<level value="DEBUG" />
</logger>
<logger name="com.danga.MemCached">
<level value="INFO" />
</logger>
<logger name="org.springframework.test">
<level value="DEBUG" />
</logger>
<logger name="org.apache.struts2">
<level value="DEBUG" />
</logger>
<root>
<level value="INFO" />
<!--<appender-ref ref="stdout" />
-->
<appender-ref ref="logfile-dao" />
<appender-ref ref="logfile-service" />
<appender-ref ref="jms_dao" />
<appender-ref ref="jms_service" />
</root>
</configuration>
==================================================
【http://logback.qos.ch/manual/configuration.html】
Inserting log requests into the application code requires a fair amount of planning and effort. Observation shows that approximately four percent of code is dedicated to logging. Consequently, even a moderately sized application will contain thousands of logging statements embedded within its code. Given their number, we need tools to manage these log statements.
Logback can be configured either programmatically or with a configuration script expressed in XML or Groovy format. By the way, existing log4j users can convert their log4j.properties files tologback.xml using our PropertiesTranslator web-application.
Let us begin by discussing the initialization steps that logback follows to try to configure itself:
Logback tries to find a file called logback.groovy in the classpath.
If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath..
If no such file is found, and the executing JVM has the ServiceLoader (JDK 6 and above) the ServiceLoader will be used to resolve an implementation of
com.qos.logback.classic.spi.Configurator
. The first implementation found will be used. See ServiceLoader documentation for more details.If none of the above succeeds, logback configures itself automatically using the
BasicConfigurator
which will cause logging output to be directed to the console.
The fourth and last step is meant to provide a default (but very basic) logging functionality in the absence of a configuration file.
日志组件 logback的更多相关文章
- Java日志组件logback使用:加载非类路径下的配置文件并设置定时更新
Java日志组件logback使用:加载非类路径下的配置文件并设置定时更新 摘自: https://blog.csdn.net/johnson_moon/article/details/7887449 ...
- 日志组件logback的介绍及配置使用方法
一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-acc ...
- 日志处理(二) 日志组件logback的介绍及配置使用方法(转)
本文转自:http://www.cnblogs.com/yuanermen/archive/2012/02/13/2348942.html http://www.cnblogs.com/yuanerm ...
- 转:日志组件logback的介绍及配置使用方法
转自:http://blog.csdn.net/zgmzyr/article/details/8267072 一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.lo ...
- 日志组件logback的介绍及配置使用方法(一)
一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-acc ...
- 日志组件logback的介绍及配置使用方法(二)
四.Logback的默认配置 如果配置文件 logback-test.xml 和 logback.xml 都不存在,那么 logback 默认地会调用BasicConfigurator ,创建一个最小 ...
- java日志组件介绍(common-logging,log4j,slf4j,logback )
转自:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging是apache提供的一个通用的日志 ...
- 转:java日志组件介绍(common-logging,log4j,slf4j,logback )
原网址:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging common-logging是 ...
- 【转】java日志组件介绍(common-logging,log4j,slf4j,logback )
common-logging common-logging是apache提供的一个通用的日志接口.用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的logging, comm ...
随机推荐
- java中try 与catch的使用
(2011-10-08 17:08:43) 转载▼ 标签: 杂谈 分类: Java try{//代码区}catch(Exception e){//异常处理}代码区如果有错误,就会返回所写异常的处理. ...
- Apache和Nginx的对比
Apache与Nginx的优缺点比较 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apac ...
- DateFormat 竟然是非线程安全的?!!!!!
今天撸代码忽然发现一个奇怪的一场抛出,经过一番排查发现有可能DateFormat 的多线程问题造成的,网上一查DateFormat竟然非线程安全.那我原先的代码...(细思极恐)
- 1)Linux学习笔记:crontab命令
crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程 配置文件 ``` SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin: ...
- jmeter+ant+jenkins+mac使用HTML Publisher插件后查看html报告显示不正常
Jenkins安全默认将以下功能关闭: 1.javascript2.html上的内置插件3.内置css或从其它站的css4.从其它站的图处5.AJAX 报告中有javascript,所以显示异常.解决 ...
- oracle存储过程学习---包的概念
转自:http://www.iteye.com/topic/1111793 一.包的概念 类似于一个容器,能打包相应的Pl/SQL变量.常量.函数.过程.复合数据类型等元素到这个容器内.用来限制 ...
- Nodejs中Mongodb使用
Mongodb使用 打开解压后的Mongodb文件夹,新建data.logs文件夹,并在logs文件夹中新建mongodb.log文档. 添加后Mongod文件夹示意图: 用cmd命令行启动Mongo ...
- Nodejs之目录介绍及app.js说明
nodejs目录说明,app.js简单介绍及如何设置app.js启动项目. 新建的项目结构应该是这样 bin:项目的启动文件,也可以放其他脚本. node_modules:用来存放项目的依赖库. pu ...
- perl访问数组中变量
数组一个是存储标量值的无序列表变量. 数组变量以 @ 开头.访问数组元素使用 $ + 变量名称 + [索引值] 格式来读取,实例如下: #!/usr/bin/perl @names = (" ...
- wildfly 如何设置外网访问
wildfly的默认配置是不支持外网访问的, 要想实现外网访问需要修改standalone.xml配置文件. 配置文件所在路径:wildfly/standalone/configuration/sta ...