Log 日志 - Log4j slf4j Logback JCL(Jakarta Commons Logging) etc.

Founder of log4j, SLF4J and logback - Ceki on stackoverflow:
[b][url]http://stackoverflow.com/users/100970/ceki[/url][/b]


[b]Log4j or Logback?[/b]
[url]http://stackoverflow.com/questions/4311086/any-reason-for-a-new-project-to-use-log4j-instead-of-logback[/url][quote]log4j isn't under active development anymore, and since logback is being developed ground up by the same author as log4j, Ceki Gülcü, to correct some mistakes made in log4j's development, you can be pretty sure that using logback isn't a bad idea.[/quote][url]http://stackoverflow.com/questions/178215/log4j-vs-logback/925098#925098[/url][quote][color=red][b]Ceki said:[/b][/color]
Logback natively implements the SLF4J API. This means that if you are using logback, you are actually using the SLF4J API. You could theoretically use the internals of the logback API directly for logging, but that is highly discouraged. All logback documentation and examples on loggers are written in terms of the SLF4J API.
So by using logback, you'd be actually using SLF4J and if for any reason you wanted to switch back to log4j, you could do so within minutes by simply dropping slf4j-log4j12.jar onto your class path.
When migrating from logback to log4j, logback specific parts, specifically those contained in logback.xml configuration file would still need to be migrated to its log4j equivalent, i.e. log4j.properties. When migrating in the other direction, log4j configuration, i.e. log4j.properties, would need to be converted to its logback equivalent. There is an on-line tool for that. The amount of work involved in migrating configuration files is much less than the work required to migrate logger calls disseminated throughout all your software's source code and its dependencies.[/quote]


Log4j使用总结:
[b][url]http://kdboy.iteye.com/blog/208851[/url][/b]


commons-logging 和 log4j的结合使用:
[url]http://ltc603.iteye.com/blog/151341[/url]

[color=red][b]关于log4j 的 Level:[/b][/color]
log4j日志输出的级别分8种,从最严到最宽,依次为:
OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL
OFF表示 turn off logging,ALL表示 turn on all logging.
程序会打印高于或等于所设置级别的日志,设置的日志级别越高,打印出来的日志就越少。如果设置级别为INFO,则级别高于等于INFO(如:INFO、WARN、ERROR)的日志信息将可以被输出,小于该级别的如DEBUG将不会被输出。
其中最常见的是ERROR、WARN、INFO、DEBUG四种。
[url]http://stackoverflow.com/questions/7745885/log4j-logging-hierarchy-order[/url][quote]public final static int OFF_INT = Integer.MAX_VALUE;
public final static int FATAL_INT = 50000;
public final static int ERROR_INT = 40000;
public final static int WARN_INT = 30000;
public final static int INFO_INT = 20000;
public final static int DEBUG_INT = 10000;
public static final int TRACE_INT = 5000;
public final static int ALL_INT = Integer.MIN_VALUE; [/quote]


关于 log4j.additivity:
[url]http://veerasundar.com/blog/2009/08/log4j-tutorial-additivity-what-and-why/[/url][quote]log4j.additivity是 子Logger 是否继承 父Logger 的 输出源(appender) 的标志位。具体说,默认情况下 子Logger 会继承 父Logger 的appender,也就是说 子Logger 会在 父Logger 的appender里输出。若是additivity设为false,则 子Logger 只会在自己的appender里输出,而不会在 父Logger 的appender里输出。

如:
log4j.rootLogger=INFO, stdout,logfile
log4j.logger.com.ambow=DEBUG, ambowLog
log4j.logger.com.ambow.business=DEBUG, bussinessLog
log4j.logger.com.ambow.upgrade=INFO, dataSyncLog

则com.ambow.upgrade包及其子包下的Logger不光在Appender dataSyncLog里输出,也会在rootLogger的Appender stuout和logfile、com.ambow的Appender ambowLog中输出;
若想让com.ambow.upgrade包及其子包下的Logger只在Appender dataSyncLog中输出,则在log4j.properties中添加下行即可:
[b][color=red]log4j.additivity.com.ambow.upgrade=false[/color][/b] [/quote]


通过设置appender的Threshold,可以实现appender对不同级别log的过滤:
[url]http://logging.apache.org/log4j/1.2/faq.html#a2.9[/url]

//com.xxx.email.service.sender包在appender stdout(即rootLogger的appender)中的输出级别为DEBUG,但在appender JDBC中的输出级别为ERROR
log4j.rootLogger=INFO, stdout
log4j.logger.com.xxx.email.service.sender=DEBUG, JDBC

log4j.appender.JDBC=org.apache.log4j.jdbc.JDBCAppender
...
log4j.appender.JDBC.Threshold=ERROR



[b]Log4j 之 输出格式 PatternLayout 参考:[/b]
[url]http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html[/url]


Log4j使用技巧——让子类使用父类中定义的Logger:[quote]在项目中,有时候会遇到这么一种情况,我们需要记录一些类的使用情况,在类的声明中,我们会如此声明一个logger:
private static final Logger logger=Logger.getLogger(MyClass.class);
然后再用这个logger来打印我们关心的信息。这种方法一直都很不错在我们的类比较少的情况下。不过在类大量增加时,我们发现这种方法并不是一个好的方法,它使我们的工程看上去臃肿不堪,于是,我们必须采用一种简洁的方法来替换它,确保其能够让我们的程序看起来舒服一些...:
[url]http://cuiyingfeng.blog.51cto.com/43841/6625[/url]
[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值