- 浏览: 246350 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
wilsonchen:
...
pdf.js html5展现pdf -
angole:
不错,又学习到了。
mybatis打印sql -
hft24dq:
rxcss66 写道 个人没有搞明白什么原理,不过跟一楼说的一 ...
mybatis打印sql -
fireinjava:
fireinjava 写道org.apache.ibatis. ...
mybatis打印sql -
fireinjava:
org.apache.ibatis.logging.LogFa ...
mybatis打印sql
http://jbaruch.wordpress.com/2011/06/22/unified-logging-using-slf4j/
- 博客分类:
- J2EE
原文地址:http://jbaruch.wordpress.com/2011/06/22/unified-logging-using-slf4j/
Integrating, integrating, integrating. That’s what we do in Java enterprise development. Persisting objects with Hibernate wrapped by JPA using C3Po (or JTA?) (or MongoDB over Morphia?), processed with JBMP, created by JAXB (jackson-json?) from JAX-RS scheduled by Quartz … (a few dozen frameworks later) … all this glued with Spring (or Guice?) deployed on Jetty (or Tomcat, JBoss, Resin?) into cluster by Terracotta (or Hadoop, GigaSpaces, JBoss cache, Infinispam?). Ah, and all this built using Maven Gradle with Artifactory on Jenkins. I sure forgot ½ of the frameworks we constantly use.
Generally we don’t mind much about the internals of the frameworks we use (as long as they are good) – the whole encapsulation stuff is the last undoubted good thing. But except for the API (part of which is the configuration) frameworks have another user-facing end – the logging. When we build a system we want it to behave as one system – single configuration from one end, and single log from another (break it to different files, if you wish, but it should still be a unified logging system).
The reality is that there is no standard de-facto for logging. The standard de-jure – JUL, is not very popular because of its lack of functionality (compared to alternatives) and its suboptimal performance. And then there is Log4J, which almost became standard, but did not. And there is logback, which is a Log4J trashover, and there are facades (JCL and SLF4J), which try to unite all this zoo, and some others, which you have probably never heard of, like syslog4j*, logging framework by the Object Guy, jLo, MonoLog, Lumberjack, Houston, JTraceDump, qflog, LN2, TracingClassLoader, SMTPHandler, Log4Ant, Simple Log, Log Bridge, Craftsman Spy, Pencil, JDLabAgent, Trace Log, JDBC Logger, LimpidLog and Microlog.
Let it be, you’d say – why not have many logging tools, which are good and diverse! Well, the problem, as I’ve already mentioned, is that they leak out of the frameworks. Their diverse configuration leaks from one end, while their diverse output from another. Spring uses Log4J over JCL. So does Hibernate. Jetty uses Logback over SLF4J. Some (like Terracotta modules) use plain Log4J, Jersey uses JUL. This means we end up with 5 separate configurations (Log4J, SLF4J, Logback, JCL and JUL) and 3 different types of log files (Log4j, Logback and JUL). What a system!
To make the long story short – How can we achieve the desired consolidation? Clearly, we need a facade. There are two most commonly used – SLF4J and JCL. JCL is known for its classloader hell, SLF4J is newer, better performing, smarter, simplier to use and generally provides better quality for the same buck (well, no buck – both are open source, of course), so we’ll stick to it. SLF4J is an adapter – thin layer of API to and from different logging implementations. Yap, both ways. It means with SLF4J we can use JUL API on top and log using Log4J in the bottom!
First we need to pick an actual logger. Log4j was considered the best choice up until recently (2006) when Ceki Gülcü decided he needed a fresh start and rewrote from scratch a new Java logging framework, just better than log4j, called Logback. We can give it a try as our underlying logging implementation (we can switch in a moment, as we are using good facade, remember?).
So, here’s what we have to do:
Establish our own good logging:
Add Logback to our classpath
Add SLF4J API to our classpath
Done here. Now our own brand new code will use top-notch logging.
Now for the tricky part. Let’s make the example stack I listed above taking configuration from one source (our config files) and writing to one target (files, listed in our configuration)
All the tools using SLF4J will just work. That includes dozen of Apache projects, inc. Camel and Mina, some SpringSource projects and many others.
Now let’s start rolling with all the rest. This is how you do it (click to enlarge):
Jakarta Commons Logging:
Remove commons-logging.jar from your classpath. Usually, it is transitive dependency from the framework, so you need to instruct your build tool on how to do it. What a lucky coincidence, I just wrote short and instructive blog post about how to do it!
Add jcl-over-slf4j.jar instead. It contains alternative commons-logging API implementation, so the code will run just fine.
Log4J:
Same goes here! Remove log4j.jar from your classpath (Again, it would usually be a transitive dependency from the framework, look here).
Add log4j-over-slf4j.jar instead. It contains alternative log4j API implementation, so the code will run just fine.
JUL:
Well, you can’t remove JUL from classpath (it’s a part of the JRE, dude). For the same reason SLF4J can’t reimplement JUL’s API.
Add jul-to-slf4j.jar. It will translate java.util.logging.LogRecord objects into their SLF4J equivalent.
Install SLF4JBridgeHandler and LevelChangePropagator.
Expect 20% decrease in performance (so use it wisely).
All done. Now both our code and all the 3rd paries configured from single source and write to single target. Hooray!
* syslog4j claims it is cross-platform. Well, I’ll just quote: “Is Syslog4j cross-platform? Yes! Syslog4j UDP/IP and TCP/IP clients should work in any typical Java JRE environment.”
Integrating, integrating, integrating. That’s what we do in Java enterprise development. Persisting objects with Hibernate wrapped by JPA using C3Po (or JTA?) (or MongoDB over Morphia?), processed with JBMP, created by JAXB (jackson-json?) from JAX-RS scheduled by Quartz … (a few dozen frameworks later) … all this glued with Spring (or Guice?) deployed on Jetty (or Tomcat, JBoss, Resin?) into cluster by Terracotta (or Hadoop, GigaSpaces, JBoss cache, Infinispam?). Ah, and all this built using Maven Gradle with Artifactory on Jenkins. I sure forgot ½ of the frameworks we constantly use.
Generally we don’t mind much about the internals of the frameworks we use (as long as they are good) – the whole encapsulation stuff is the last undoubted good thing. But except for the API (part of which is the configuration) frameworks have another user-facing end – the logging. When we build a system we want it to behave as one system – single configuration from one end, and single log from another (break it to different files, if you wish, but it should still be a unified logging system).
The reality is that there is no standard de-facto for logging. The standard de-jure – JUL, is not very popular because of its lack of functionality (compared to alternatives) and its suboptimal performance. And then there is Log4J, which almost became standard, but did not. And there is logback, which is a Log4J trashover, and there are facades (JCL and SLF4J), which try to unite all this zoo, and some others, which you have probably never heard of, like syslog4j*, logging framework by the Object Guy, jLo, MonoLog, Lumberjack, Houston, JTraceDump, qflog, LN2, TracingClassLoader, SMTPHandler, Log4Ant, Simple Log, Log Bridge, Craftsman Spy, Pencil, JDLabAgent, Trace Log, JDBC Logger, LimpidLog and Microlog.
Let it be, you’d say – why not have many logging tools, which are good and diverse! Well, the problem, as I’ve already mentioned, is that they leak out of the frameworks. Their diverse configuration leaks from one end, while their diverse output from another. Spring uses Log4J over JCL. So does Hibernate. Jetty uses Logback over SLF4J. Some (like Terracotta modules) use plain Log4J, Jersey uses JUL. This means we end up with 5 separate configurations (Log4J, SLF4J, Logback, JCL and JUL) and 3 different types of log files (Log4j, Logback and JUL). What a system!
To make the long story short – How can we achieve the desired consolidation? Clearly, we need a facade. There are two most commonly used – SLF4J and JCL. JCL is known for its classloader hell, SLF4J is newer, better performing, smarter, simplier to use and generally provides better quality for the same buck (well, no buck – both are open source, of course), so we’ll stick to it. SLF4J is an adapter – thin layer of API to and from different logging implementations. Yap, both ways. It means with SLF4J we can use JUL API on top and log using Log4J in the bottom!
First we need to pick an actual logger. Log4j was considered the best choice up until recently (2006) when Ceki Gülcü decided he needed a fresh start and rewrote from scratch a new Java logging framework, just better than log4j, called Logback. We can give it a try as our underlying logging implementation (we can switch in a moment, as we are using good facade, remember?).
So, here’s what we have to do:
Establish our own good logging:
Add Logback to our classpath
Add SLF4J API to our classpath
Done here. Now our own brand new code will use top-notch logging.
Now for the tricky part. Let’s make the example stack I listed above taking configuration from one source (our config files) and writing to one target (files, listed in our configuration)
All the tools using SLF4J will just work. That includes dozen of Apache projects, inc. Camel and Mina, some SpringSource projects and many others.
Now let’s start rolling with all the rest. This is how you do it (click to enlarge):
Jakarta Commons Logging:
Remove commons-logging.jar from your classpath. Usually, it is transitive dependency from the framework, so you need to instruct your build tool on how to do it. What a lucky coincidence, I just wrote short and instructive blog post about how to do it!
Add jcl-over-slf4j.jar instead. It contains alternative commons-logging API implementation, so the code will run just fine.
Log4J:
Same goes here! Remove log4j.jar from your classpath (Again, it would usually be a transitive dependency from the framework, look here).
Add log4j-over-slf4j.jar instead. It contains alternative log4j API implementation, so the code will run just fine.
JUL:
Well, you can’t remove JUL from classpath (it’s a part of the JRE, dude). For the same reason SLF4J can’t reimplement JUL’s API.
Add jul-to-slf4j.jar. It will translate java.util.logging.LogRecord objects into their SLF4J equivalent.
Install SLF4JBridgeHandler and LevelChangePropagator.
Expect 20% decrease in performance (so use it wisely).
All done. Now both our code and all the 3rd paries configured from single source and write to single target. Hooray!
* syslog4j claims it is cross-platform. Well, I’ll just quote: “Is Syslog4j cross-platform? Yes! Syslog4j UDP/IP and TCP/IP clients should work in any typical Java JRE environment.”
发表评论
-
spring send gmail
2012-04-24 11:06 1086只要这样配置好久能使用gmail了 <bean id= ... -
log4j 常用配置
2012-03-22 21:02 1046原文地址:http://www.benmccann.com/d ... -
Dependency Injection - An Introductory Tutorial - Part 1
2012-02-20 10:57 1179原文地址:http://code.google.com/p/j ... -
struts2 排除拦截部分路径
2011-11-30 13:27 5742情况:在web.xml中配置一个servlet映射路径为/te ... -
java image scale
2011-07-20 13:47 900http://code.google.com/p/java-i ... -
实现自定义截取图片
2011-07-13 17:30 1064几种插件: http://odyniec.net/projec ... -
jms基础概念和应用场景
2011-07-01 13:55 1575原文地址:http://blog.csdn.net/KimmK ... -
Envers –tracked your Entity Objects
2011-06-29 09:05 1531原文地址:http://get2java.wordpress. ... -
JSON Compression algorithms: HPack VS CJSON
2011-06-28 17:24 3092总结:HPack 优于 CJSON json1 json2 ... -
Why we don’t use Doubles for Financial Calculations
2011-06-27 11:14 1246原文地址:http://bloodredsun.com/?p= ... -
Sturts2 全局异常日志
2011-06-25 10:50 1783原文地址:http://www.brucephillips.n ... -
eclipse的build与clean
2011-06-22 09:01 1546现象:无论怎么改变代码,程序的行为始终不变,一直报错。。。。 ... -
jfreechart 图标各部分名字
2011-06-09 19:57 796图标各部分名字 -
jfreechart 自定义饼图颜色
2011-06-09 18:52 4827关键代码: private static class Pi ... -
jfreechart demo 代码
2011-06-07 18:33 2920jfreechart 官方demo 源代码 -
jfreechart 中文问题
2011-06-07 16:52 909基本如果将jfreechart生成图片的字体多改成中文的字体就 ... -
SVN 安装
2011-05-30 15:45 988collabnet svn 现在出了整和的SVN EDGE。这 ... -
amazon 云计算
2011-05-28 21:45 1041最近看了amazon的云计算 ... -
c3p0 java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
2011-05-28 16:07 1557修改日志级别为info http://hi.baidu.com ... -
mybatis打印sql
2011-05-09 15:03 19894mybatis默认使用log4j,当有self4j这个日志ja ...
相关推荐
银联商务支付对接demo,包含了对接的东西,和银联有关系
{uri-kroki}[Kroki] provides a unified API with support for BlockDiag (BlockDiag, SeqDiag, ActDiag, NwDiag), C4 (with PlantUML), Ditaa, Erd, GraphViz, Nomnoml, Mermaid, PlantUML, SvgBob and UMLet......
release notes 2011.03.13 Unified installer for all IDEs 注:该版本支持 VS2010 VS2008 VS2005 VS2003 VS2002 VC6 官方下载地址:http://www.wholetomato.com/binaries/VA_X_Setup1845.exe 由于官方...
Qt开源版官方安装包
所有的灵感都来源于学术界最近的一些研究成果,例如 [Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks](https://arxiv.org/abs/1604.02878) 和 [FaceNet: A Unified ...
Beyond BIOS: Developing with the Unified Extensible Firmware Interface, Third Edition Published by Vincent Zimmer, the latest edition includes: An overview of UEFI and underlying Platform ...
Java语言开发的统一角色访问控制系统(Unified Role Access Control System),基于Spring Security 3实现的权限控制系统 程序框架版本说明:Spring MVC 3.0.6 + Spring Security 3.1.3 + Hibernate 3.6.10 运行...
☆ pack STL container and class array into unified object with several same interfaces. ☆ can initialize array/stl container with initalization list repeated. e.g. vector<int> a={1,2,3,45,2}; ◆ ...
资源分类:Python库 所属语言:Python 资源全名:unified-message-relay-4.1.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
VMware NSX-T 3.0 OVA文件nsx-unified-appliance-3.0.0.0.0.15946739.ova
你可以在[这篇博客文章]中找到(https://blog.evernote.com/tech/2015/10/26/unified-job-library-android/)或在[这些幻灯片]中(https://speakerdeck.com/vrallev/doo-z-z-z-z-z-e?slide=50)为什么您应该更喜欢这个库...
qt-unified-windows-x86-online.exe
var output1 = emoji.replace_unified(input); // replaces :smile: with platform appropriate content var output2 = emoji.replace_colons(input); // force text output mode emoji....
2020-04-unified-api-design 统一API设计方法讨论的示例 笔记 这里的源代码很旧。 您可以在此处找到unified实用程序的活动源项目: : // **************************************************** // unified - an ...
Unicode处理工具类。 含表情符号表情处理,中日韩字符判断,Unicode... format( " https://api.weixin.qq.com/sns/userinfo? " + " access_token=%s&openid=%s " , wxToken . getAccess_token(), wxToken . getOpeni
资源分类:Python库 所属语言:Python 资源全名:alps_unified_ts-0.0.32-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
如今,由于业务多样化、全球化和不同业务项目的数量不断增加,支持参与项目管理相关任务的人员的需求变得越来越重要。 项目计划、相关成本和进度中所需的准确数据和时间对于项目经理确保项目成功极为重要。...
./configure make make test sudo make install This will install Python as python3. You can pass many options to the configure script; run "./configure --help" to find out more. On OSX and Cygwin,...
QT在线安装器qt-unified-windows-x64-4.6.0-online.exe,留下个备份