The Java Logging Mess

转载 2015年11月19日 15:55:23
Every application needs logging. And right now there are a lot of options on what exactly to use for logging in Java. The most famous frameworks are: log4j, logback, commons-logging, slf4j, java.util.logging. And there are a lot more – every now and then someone decides to write his own logger – just go to the Open Type dialog of your IDE and type “Logger” (not to mention that some use the name “Log”). And there’s also the ServletContext.log(..) and stuff like that. It is indeed a mess. But let’s start with some history (note: here’s another post on the topic) .

First there was System.out and System.err. But they were inflexible, so a need for a feature-rich logging has arisen (note that I was in elementary school at that time). Logging that can be customized format, that can go to multiple targets – files, consoles, emails, etc. So log4j has appeared – in 1999.

But other solutions spawned as well, including java.util.logging – an attempt to have standard JDK logging. Note a very successful attempt, as it turned out. java.util.logging appeared with JDK 1.4 in the beginning of 2002. A few months later the realized need for a common logging interface for all existing loggers resulted in apache common-logging.

The idea of commons-logging was viable – libraries should not force a particular logging implementation on applications that use them. So each logging implementation gets adapted to a common API which is used by the library – so your library does not use org.apache.log4j.Logger – it uses org.apache.commons.logging.Log, and it delegates to whatever logging framework exists on the classpath. That way your project can use multiple libraries and use a single logging configuration for all of them.

But commons-logging was not good enough. People say that it has caused more problems than it has solved. So the author of log4j – Ceki Gülcü created a new project – slf4j (Simple Logging Facade for Java) in 2005. It aims to be a better commons-logging.

Log4j has been widely used since 1999, but it was not good enough, so guess who created a new project – logback. It was Ceki Gülcü again. Why a new project? Well, combination of political reasons and old code base that needs replacing from the ground, I guess. Anyway, logback appeared in 2006. How it is better than log4j? Ceki explains here.

So back to present day – there are a lot of logging frameworks and two facades – commons-logging and slf4j. Every library uses a different one, and it’s a mess. Version mismatches, tons of Logger classes on the classpath. Maven succeeds at making this simpler by at least not allowing multiple versions of the same logging implementation, but that’s it. And if you don’t know all the history above and which framework is used for what, it is likely for your project to suffer from that mess.

What’s the solution? The simplest thing is to use slf4j and logback. Why?

  • slf4j has bridges for many existing implementations. This means that you remove the log4j.jar and use the log4j-over-slf4j.jar – it has the same classes in the same package, only the implementation differs – it delegates to the current slf4j implementation in use. That way all libraries that use log4j (or any other bridged implementation) will work with your logback configuration. Unfortunately this doesn’t work quite well with java.util.logging, so you have to hope not to have too many libraries that have decided on a “minimal dependency footprint”.
  • logback is better than log4j (same author – newer implementation, learning from previous mistakes)
  • if a better framework than logback appears you can easily switch to it without changing your classes.

And finally, a word about the logging configuration. It should be external, in the same way (and the same location, preferably) as the other externalized project configurations. You should then load it based on a system “config.location” property.

(In a spring-based web-application there is Log4jWebConfigurer, but there isn’tLogbackWebConfigurer. Luckily, it is simple to write, and there are some existing implementations that are based on the log4j one. In your web.xml thelogbackConfigLocation param should be: file://${config.lotation}/logback.xml)

Why something so simple became so complicated? Because it isn’t simple. There are too many factors that were not taken into account in the beginning, so they needed to be rectified later. It’s a good thing that there hasn’t been a major change in the field since 2006, so we can consider things stable.

Reference: The Logging Mess from our JCG partner Bozhidar Bozhanov at the Bozho’s tech blog.

举报

相关文章推荐

Annotations: Don't Mess with Java(转载)

Annotations are a major kludge on the landscape. They don't fit and we don't need them; but you just...

java日志组件(2):common-logging

Jakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的日志实现工具。它提供给中间件/日志工具开发者一个简单的日志...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

基于java.util.logging实现轻量级日志记录库(增加根据当前类class初始化,修复线程池模型(javaEE)下的堆栈轨迹顺序与当前调用方法不一致问题)

前言: 本章介绍自己写的基于java.util.logging的轻量级日志记录库(baseLog)。 该版本的日志记录库犹如其名,baseLog,是个实现日志记录基本功能的小库,适合小型项目使用,方...

java.util.logging.Logger使用详解

一直用的是log4j 原来java本身也是提供了很强大的log。  这文章说的很详细:http://lavasoft.blog.51cto.com/62575/184492  java.uti...

【日志】Java日志工具(common-logging/log4j/slf4j)在常用框架(Struts2/Spring/Hibernate/Mybatis)中的灵活运用

Java日志工具(common-logging/log4j/slf4j)在常用框架(Struts2/Spring/Hibernate/Mybatis)中的灵活运用 Spring :Commons...

Java Logging API

Log日志:主要用于记录程序运行的情况,以便于程序在部署之后的排错调试等等!也有利于将这些信息进行持久化(如果不将日志信息保存到文件或数据库,则信息便会丢失)Java Logging API需JDK1...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)