[slf+log4j] Basic + different levels of logs are sent to different files

Usually logs are printed based on slf4j+log4j or slf4j+logback. As a facade, slf4j only provides an interface, and the actual printing still uses the functions provided by log4j or logback. This article uses log4j as an example.

1. The foundation of log4j : http://www.cnblogs.com/Fskjb/archive/2011/01/29/1947592.html
gist : There is always a rootLogger in Log4J, logger can be inherited, and logger name can be customized, this The name is related to the name of Logger.getLogger("name").

Logger.getLogger() or LogFactory.getLog()
Logger comes from log4j's own package. If you use Logger.getLogger, you need a log4j jar package, in this way you can only rely on log4j:

LogFactory comes from the common-logging package. If you use LogFactory.getLog, you can replace log4j with any logger that implements the generic logging interface, and your program will not be affected. The common-logging package of apache is a general log interface. Through this middle layer, you can arbitrarily specify which logging system to use. Increase system flexibility. If log4j does not exist, commons-logging will choose another logging implementation class. This ensures that the log4j log file is not necessarily used in the program.
http://www.jb51.net/article/41291.htm

2. The advantages of slf4j and the jar package reference can be referred to: http://www.importnew.com/7450.html
Key points :
(1) slf4j is an independent and specific printing log framework, which can be combined with log4j, or can be easily switched to logback.
(2) Compared with log4j, slf4j can use placeholders, such as log4j writing:
logger.debug("Processing trade with id: " + id + " symbol: " + symbol);

Using slf4j you can:
logger.debug("Processing trade with id: {} and symbol : {} ", id, symbol);

Note: It cannot be in the form of {1},{2},{3}.
This will be replaced at runtime by one of the actual strings provided. This not only reduces the number of string concatenations in your code, but also saves new String objects.
(3) The judgment of isDebugEnabled() and isInfoEnabled() is omitted, and slf4j automatically provides judgment.

3. When slf4j prints exceptions,
it is necessary to print logs with variable parameters and to print exception information. With slf4j, you cannot directly put exceptions at the end of the parameters, that is, you cannot:
logger.error("error info {}#{}",new Object[]{a, b}, e);// e 是exception类型

Only:
logger.error("error info {}#{}",new Object[]{a, b, e});// e 是exception类型

Put e at the end of the mutable object array. When two parameters are required, just use e as the third element. The first way of writing will treat e as a normal Object, and call the e.toString() method to print, but the stack information of the exception cannot be printed.

Note: Regarding this point, the data says that versions above slf4j 1.7.* can follow the first writing method, and 1.7.* and below must follow the second writing method. But I can only test 1.7.* according to the second way of writing.

4. There have been many articles describing how to output logs of different levels to different files, and you can refer to them completely. For example:
http://blog.csdn.net/wangchsh2008/article/details/8812857
http://www.cprogramdevelop.com/4496123/

There is a point to explain here, if you customize the logger, for example: log4j.logger. ABC=info, ferror, finfo, fdebug
and define 3 files for ferror, finfo, fdebug respectively.
##  error  appender
log4j.appender.ferror.Threshold = error
log4j.appender.ferror = A.B.C.common.util.MyDailyRollingFileAppender
log4j.appender.ferror.File = ${log.root}/error.log
log4j.appender.ferror.Append = true
log4j.appender.ferror.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.ferror.layout = org.apache.log4j.PatternLayout
log4j.appender.ferror.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p] %X{requestID} [%c-%L]- %m%n

##  info  appender
log4j.appender.finfo.Threshold = info
log4j.appender.finfo = A.B.C.common.util.MyDailyRollingFileAppender
log4j.appender.finfo.File = ${log.root}/info.log
log4j.appender.finfo.Append = true
log4j.appender.finfo.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.finfo.layout = org.apache.log4j.PatternLayout
log4j.appender.finfo.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p] %X{requestID} [%c-%L]- %m%n

##  debug  appender
log4j.appender.fdebug.Threshold = debug
log4j.appender.fdebug = A.B.C.common.util.MyDailyRollingFileAppender
log4j.appender.fdebug.File = ${log.root}/debug.log
log4j.appender.fdebug.Append = true
log4j.appender.fdebug.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.fdebug.layout = org.apache.log4j.PatternLayout
log4j.appender.fdebug.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p] %X{requestID} [%c-%L]- %m%n

###root log
log4j.rootLogger = ${log.level},finfo

## my log
log4j.logger.A.B.C=info,ferror,finfo,fdebug

Which level of log will be printed at the end depends on the level defined by log4j.logger.ABC. As configured above, the debug log will not be printed.
Reason:
log4j will automatically call the isDebugEnabled() method of log4jLoggerAdapter:
public boolean isDebugEnabled() {
    return this.logger.isDebugEnabled();
}
public boolean isDebugEnabled() {
    return this.repository.isDisabled(10000)?false:Level.DEBUG.isGreaterOrEqual(this.getEffectiveLevel());
}

The getEffectiveLevel() here will return the level info defined by log4j.logger.ABC, and if the debug is less than this level, it will be ignored and not played.

If the debug level is configured here, the verification can be passed. When printing, the public boolean isAsSevereAsThreshold(Priority priority) method implemented by the inherited DailyRollingFileAppender class will be called. The parameter priority of this method is the method called when the log is actually printed. For example, calling logger.debug("this is the log"); then priority= Level.DEBUG. Then compare this level with the respectively configured Threshold values ​​of ferror, finfo, and fdebug. The comparison rules are customized in isAsSevereAsThreshold().

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326894340&siteId=291194637