- 浏览: 252124 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 1152只要这样配置好久能使用gmail了 <bean id= ... -
log4j 常用配置
2012-03-22 21:02 1102原文地址:http://www.benmccann.com/d ... -
Dependency Injection - An Introductory Tutorial - Part 1
2012-02-20 10:57 1210原文地址:http://code.google.com/p/j ... -
struts2 排除拦截部分路径
2011-11-30 13:27 5785情况:在web.xml中配置一个servlet映射路径为/te ... -
java image scale
2011-07-20 13:47 931http://code.google.com/p/java-i ... -
实现自定义截取图片
2011-07-13 17:30 1112几种插件: http://odyniec.net/projec ... -
jms基础概念和应用场景
2011-07-01 13:55 1607原文地址:http://blog.csdn.net/KimmK ... -
Envers –tracked your Entity Objects
2011-06-29 09:05 1586原文地址:http://get2java.wordpress. ... -
JSON Compression algorithms: HPack VS CJSON
2011-06-28 17:24 3164总结:HPack 优于 CJSON json1 json2 ... -
Why we don’t use Doubles for Financial Calculations
2011-06-27 11:14 1297原文地址:http://bloodredsun.com/?p= ... -
Sturts2 全局异常日志
2011-06-25 10:50 1815原文地址:http://www.brucephillips.n ... -
eclipse的build与clean
2011-06-22 09:01 1583现象:无论怎么改变代码,程序的行为始终不变,一直报错。。。。 ... -
jfreechart 图标各部分名字
2011-06-09 19:57 846图标各部分名字 -
jfreechart 自定义饼图颜色
2011-06-09 18:52 4911关键代码: private static class Pi ... -
jfreechart demo 代码
2011-06-07 18:33 2963jfreechart 官方demo 源代码 -
jfreechart 中文问题
2011-06-07 16:52 953基本如果将jfreechart生成图片的字体多改成中文的字体就 ... -
SVN 安装
2011-05-30 15:45 1023collabnet svn 现在出了整和的SVN EDGE。这 ... -
amazon 云计算
2011-05-28 21:45 1088最近看了amazon的云计算 ... -
c3p0 java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
2011-05-28 16:07 1591修改日志级别为info http://hi.baidu.com ... -
mybatis打印sql
2011-05-09 15:03 20017mybatis默认使用log4j,当有self4j这个日志ja ...
相关推荐
最近公司的小程序需要使用到支付功能,我们使用的是银联商务的支付,那么如何在微信小程序中对接银联商务的微信小程序支付呢?这里我记录下我在微信小程序中实现银联商务的微信小程序支付流程 一:接口部分: ...
CUDA(Compute Unified Device Architecture)是由NVIDIA推出的一种并行计算平台和编程模型,它允许开发者利用NVIDIA的图形处理单元(GPU)进行高性能计算。 首先,对于CUDA编程,需要确保你的GPU型号支持CUDA。...
grub4dos是一款基于GRUB(Grand Unified Bootloader)的引导加载器,专门针对DOS环境进行了优化。GRUB是Linux系统中广泛使用的引导加载器,而grub4dos则扩展了其功能,使其能够在Windows环境下运行,并且支持启动...
4. 用户确认支付后,银联服务器处理交易并返回结果给商户系统。 5. 商户系统根据返回的结果进行相应的操作,如订单确认、发货等。 在C#环境中,实现银联支付接口通常涉及以下技术点: 1. **HTTP通信**:使用...
qt-unified-windows-x64-4.6.0-online.exe安装包及国内映射源地址。安装速度快,给赶时间的网友。解压安装,把脚本代码复制到cmd种回车即可。 qt-unified-windows-x64-4.6.0-online.exe安装包及国内映射源地址。安装...
{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......
4. **回调通知处理**:银联在处理完一笔交易后,会主动向商户的服务器发送通知,告知交易结果。开发者需要设置回调函数来处理这些通知,进行业务逻辑的处理,比如更新订单状态、库存管理等。 5. **异常处理与重试...
6. **UML模型**:[http://www.hellocto.com/wz/list.aspx?cid=121](http://www.hellocto.com/wz/list.aspx?cid=121) UML(Unified Modeling Language)是一种标准化的建模语言,用于对系统进行可视化建模。...
qt6在线安装文件:qt-unified-windows-x64-online 向下兼容
Qt开源版官方安装包"qt-unified-windows-x86-4.0.1-1-online.zip"是一个专为Windows 32位系统设计的Qt开发框架的安装程序。Qt是一个跨平台的应用程序开发框架,广泛用于创建桌面、移动以及嵌入式系统的图形用户界面...
CUDA(Compute Unified Device Architecture)是NVIDIA提供的编程工具包,允许开发者利用GPU进行并行计算。CUDA 10.2包含了对GPU编程的广泛支持,包括新的API、性能优化和错误修复,以提高GPU计算效率。 CUDNN...
统一建模语言(Unified Modeling Language,简称UML)是一种标准化的建模语言,广泛应用于软件工程领域。它通过图形化的符号体系来描述系统的结构和行为,帮助开发者更好地理解和构建复杂的系统。随着UML在软件开发...
在WPF(Windows Presentation Foundation)中,统一资源标识符(URI, Unified Resource Identifier)是一种标准机制,用于定位和访问各种类型的资源,包括图像。本文将详细介绍如何使用URI加载图像资源,以及通过...