Log 日志 - Log4j slf4j Logback JCL(Jakarta Commons Logging) etc. 博客分类: Tools 日志loglog4jslf4jlogback
程序员文章站
2024-03-17 23:53:40
...
Founder of log4j, SLF4J and logback - Ceki on *:
http://*.com/users/100970/ceki
Log4j or Logback?
http://*.com/questions/4311086/any-reason-for-a-new-project-to-use-log4j-instead-of-logback
引用
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.
http://*.com/questions/178215/log4j-vs-logback/925098#925098引用
Ceki said:
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.
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.
Log4j使用总结:
http://kdboy.iteye.com/blog/208851
commons-logging 和 log4j的结合使用:
http://ltc603.iteye.com/blog/151341
关于log4j 的 Level:
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四种。
http://*.com/questions/7745885/log4j-logging-hierarchy-order
引用
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;
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;
关于 log4j.additivity:
http://veerasundar.com/blog/2009/08/log4j-tutorial-additivity-what-and-why/
引用
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中添加下行即可:
log4j.additivity.com.ambow.upgrade=false
如:
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中添加下行即可:
log4j.additivity.com.ambow.upgrade=false
通过设置appender的Threshold,可以实现appender对不同级别log的过滤:
http://logging.apache.org/log4j/1.2/faq.html#a2.9
//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
Log4j 之 输出格式 PatternLayout 参考:
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
Log4j使用技巧——让子类使用父类中定义的Logger:
引用
在项目中,有时候会遇到这么一种情况,我们需要记录一些类的使用情况,在类的声明中,我们会如此声明一个logger:
private static final Logger logger=Logger.getLogger(MyClass.class);
然后再用这个logger来打印我们关心的信息。这种方法一直都很不错在我们的类比较少的情况下。不过在类大量增加时,我们发现这种方法并不是一个好的方法,它使我们的工程看上去臃肿不堪,于是,我们必须采用一种简洁的方法来替换它,确保其能够让我们的程序看起来舒服一些...:
http://cuiyingfeng.blog.51cto.com/43841/6625
private static final Logger logger=Logger.getLogger(MyClass.class);
然后再用这个logger来打印我们关心的信息。这种方法一直都很不错在我们的类比较少的情况下。不过在类大量增加时,我们发现这种方法并不是一个好的方法,它使我们的工程看上去臃肿不堪,于是,我们必须采用一种简洁的方法来替换它,确保其能够让我们的程序看起来舒服一些...:
http://cuiyingfeng.blog.51cto.com/43841/6625