Java Logging Techniques Summary(Introduction in Brief)

本文详细介绍了Java中常见的日志技术,包括java.util.logging、Log4j、logback、JakartaCommonsLogging和SLF4J,并阐述了使用日志而非System.out.println的原因,如控制能力、追踪性以及不同严重级别的输出。此外,比较了这些技术的特点,并讨论了日志框架的优势以及具体引入方式,最后总结了项目中常用的日志组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. As the time evolves, many logging techniques emerged.

    1) java.util.logging (JUL) is provided by SUN along with JDK.

    2) org.apache.log4j (Log4j) is provided by ASF.

         -> Log4j-1.X and Log4j-2.X

         -> But most projects are using Log4j-1.X

    3) logback

    4) org.apache.commons.logging (JakartaCommonsLogging) is provided by ASF

    5) org.slf4j (SLF4J) is provided by SimpleLoggingFacade

 

2. Why do we use log and not System.out.println()?

    1) SYSOUT is not controlable.

        --> What if we want to output these information into file or other position?

        --> What if we don't want all of these information to be printed? 

        --> What if we want the information to be partially printed according to theirs severe level? 

    2) SYSOUT is not tracable

        --> If you are writing a java application server then only way to know what your server is doing is by seeing log file of your server. suppose you don't write anything in your java log file then no body knows what your sever is doing, it becomes increasingly important if your application is connected to upstream and downstream like in many stock trading systems or electronic trading system and get input from upstream , transforms and normalize it and send down to downstream. In case of any issue without java logs you won't be able to figure out what went wrong. That’s why logging in java is most important while writing any java server application. Logging in Java is not by choice it’s must to understand

    3) So logging techniques have emerged to solve these problems.

        --> They provide different severe level when output.

        --> They provide different handlers/appenders for the position of information.

        --> They are tracable.

 

3. Brief Comparasion of these techniques 

    1) Both Log4j, JUL and Logback are concrete implementations for logging. 

        --> But Log4j is more effecient and more functional than JUL .

    2) Both commons-logging and slf4j are interfaces for logging.

        --> The core target of these interfaces is to provide a simple and common standard for logging.


 

    3) Diagrams in details


 

4. Brief Introduction to JUL and Log4j

    1) JUL

        1) Provided along with JDK.

        2) Core function is provided with LogManager

        3) Below is the detailed work flow of JUL

     2) Log4j

        1) Provided by ASF and has a long history.

        2) The simple work flow of Log4j is as below


 

5. Brief Introduction to JCL and SLF4J

    0) The benefits of using logging-frameworks:

         --> Frameworks themselves didn't provide concrete logging function. They provided interfaces instead.

         --> The real logging function is based on Log4j or JUL.

         --> The deployer can decide which logging system to use, Log4j or JUL or others.

         --> The deployer can easily change the logging system without change the code as the interface stays the same.

    1) JCL


    2) SLF4J


 

6. Summary:

    1) By convention, many projects don't use JUL.

    2) They are using Log4j and JCL as combination.

    3) Or we can use Log4j and SLF4J as combination.

    3) The new trend is using Logback and SLF4J as combination.

 

Reference Links:

    1) http://mike.vanvendeloo.net/2011/05/06/logging-which-framework-to-choose-log4j-commons-logging-logback-slf4j

    2) http://www.tutorialspoint.com/log4j/log4j_architecture.htm

    3) http://aofengblog.blog.163.com/blog/static/631702120113249403464/

    4) http://webx.taobao.org/docs/logging.html

    5) http://javarevisited.blogspot.hk/2011/05/top-10-tips-on-logging-in-java.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值