`
13146489
  • 浏览: 252995 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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.”
分享到:
评论

相关推荐

    使用银联商务实现微信小程序支付

    最近公司的小程序需要使用到支付功能,我们使用的是银联商务的支付,那么如何在微信小程序中对接银联商务的微信小程序支付呢?这里我记录下我在微信小程序中实现银联商务的微信小程序支付流程 一:接口部分: ...

    CUDA开发环境的建立.pdf

    CUDA(Compute Unified Device Architecture)是由NVIDIA推出的一种并行计算平台和编程模型,它允许开发者利用NVIDIA的图形处理单元(GPU)进行高性能计算。 首先,对于CUDA编程,需要确保你的GPU型号支持CUDA。...

    grub4dos-0.4.4-2008-08-08-src.zip

    grub4dos是一款基于GRUB(Grand Unified Bootloader)的引导加载器,专门针对DOS环境进行了优化。GRUB是Linux系统中广泛使用的引导加载器,而grub4dos则扩展了其功能,使其能够在Windows环境下运行,并且支持启动...

    银联支付接口Pay_C#

    4. 用户确认支付后,银联服务器处理交易并返回结果给商户系统。 5. 商户系统根据返回的结果进行相应的操作,如订单确认、发货等。 在C#环境中,实现银联支付接口通常涉及以下技术点: 1. **HTTP通信**:使用...

    银联商务支付对接demo_支付_银商

    4. **回调通知处理**:银联在处理完一笔交易后,会主动向商户的服务器发送通知,告知交易结果。开发者需要设置回调函数来处理这些通知,进行业务逻辑的处理,比如更新订单状态、库存管理等。 5. **异常处理与重试...

    qt-unified-windows-x64-4.6.0-online.exe

    qt-unified-windows-x64-4.6.0-online.exe安装包及国内映射源地址。安装速度快,给赶时间的网友。解压安装,把脚本代码复制到cmd种回车即可。 qt-unified-windows-x64-4.6.0-online.exe安装包及国内映射源地址。安装...

    uml工具比较文档一览表

    统一建模语言(Unified Modeling Language,简称UML)是一种面向对象的标准化建模语言,用于软件工程领域中的规格说明、可视化处理、构建和文档编制一个面向对象的分析和设计模型。随着UML的应用越来越广泛,各种...

    Android代码-kroki

    {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......

    开元柜架地址大全-各种开元柜架

    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 向下兼容

    qt6在线安装文件:qt-unified-windows-x64-online 向下兼容

    qt-unified-windows-x86-4.0.1-1-online.zip

    Qt开源版官方安装包"qt-unified-windows-x86-4.0.1-1-online.zip"是一个专为Windows 32位系统设计的Qt开发框架的安装程序。Qt是一个跨平台的应用程序开发框架,广泛用于创建桌面、移动以及嵌入式系统的图形用户界面...

    TensorRT-7.0.0.11.Ubuntu-18.04.x86_64-gnu.cuda-10.2.cudnn7.6.zip

    CUDA(Compute Unified Device Architecture)是NVIDIA提供的编程工具包,允许开发者利用GPU进行并行计算。CUDA 10.2包含了对GPU编程的广泛支持,包括新的API、性能优化和错误修复,以提高GPU计算效率。 CUDNN...

    WPF之Uri加载图片

    在WPF(Windows Presentation Foundation)中,统一资源标识符(URI, Unified Resource Identifier)是一种标准机制,用于定位和访问各种类型的资源,包括图像。本文将详细介绍如何使用URI加载图像资源,以及通过...

    uml tool比较

    统一建模语言(Unified Modeling Language,简称UML)是一种标准化的建模语言,广泛应用于软件工程领域。它通过图形化的符号体系来描述系统的结构和行为,帮助开发者更好地理解和构建复杂的系统。随着UML在软件开发...

    UML工具介绍大全截止2010

    UML(Unified Modeling Language,统一建模语言)是一种标准化的图形化语言,用于软件系统的可视化建模。它帮助开发者更好地理解系统结构和行为,并且能够促进团队成员之间的沟通。本文根据“UML工具介绍大全截止...

Global site tag (gtag.js) - Google Analytics