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.

The Logging Mess

The Logging Mess September 13, 2011 Every application needs logging. And right now there are a lo...
  • xiaojianpitt
  • xiaojianpitt
  • 2011年09月26日 08:29
  • 944

公司的前端格式

/*! * Copyright © 2012-2014 Combanc All rights reserved. * * 通用公共方法 * @author Jollys * @v...
  • elsery
  • elsery
  • 2015年11月13日 16:18
  • 1153

Java中的日志——Java.util.logging、log4j、commons-logging

Java中给项目程序添加log主要有三种方式,一使用JDK中的java.util.logging包,一种是log4j,一种是commons-logging。其中log4j和commons-loggin...
  • luoweifu
  • luoweifu
  • 2015年06月14日 22:07
  • 19878

[mess]java基础1234

// 求余%:商必须是整数; 余数=被余数-商*除数; 第一个数字是正数,结果为整数;第一个数字是负数,结果为负数。】         // for while         // int sum...
  • yqjc2003
  • yqjc2003
  • 2013年09月27日 16:08
  • 346

commons-logging实现日志解耦

一 需要解耦 二 commons-logging简单日志实现 配置commons-loggingproperties 创建Log实例 输出配置 三 commons-logging解耦原理 四 log4...
  • SakuraInLuoJia
  • SakuraInLuoJia
  • 2016年12月09日 10:40
  • 1890

(hdu 1048)The Hardest Problem Ever

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1048字符串水题,关键是要细心。。。WA了好多次...#include #include char ...
  • yuegenupt
  • yuegenupt
  • 2011年07月10日 16:53
  • 290

solr5.2.1+tomcat7.0.63安装

1. 解压solr-5.2.1.tar.gz 到/usr/local/solr.5.2.1,进入安装目录,新建solr目录。 2. 将/usr/local/solr-5.2.1/server/sol...
  • zhcgx4954
  • zhcgx4954
  • 2015年08月20日 18:03
  • 853

Swt MessageBox 对话框

引用:http://blog.163.com/java_moon/blog/static/1333382642009111095740881/ SWT有不同类型的对话框。有些对话框具有特殊的属性。Me...
  • xueyepiaoling
  • xueyepiaoling
  • 2010年10月28日 14:10
  • 2064

java.util.logging.Logger 层次结构测试

通过parent,理解java log层次结构 private void parentTest() { Logger log = Logger.getLogger("test....
  • sinkary
  • sinkary
  • 2015年04月02日 16:27
  • 511

tomcat报错catalina.sh: line 401: /usr/java/jdk1.7.52/bin/java: No such file or directory

将生产服务器的Tomcat目录打包过来后解压后,启动Tomcat后,发现如下问题: # ./shutdown.sh  Using CATALINA_BASE:   /usr/local/t...
  • reblue520
  • reblue520
  • 2016年09月19日 20:03
  • 6844
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The Java Logging Mess
举报原因:
原因补充:

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