摘要:简单描述了利用log4j/jdk5 log 和apache 通用log包commmons-logging实现日志输出的方法
commons-logging.properties文件配置信息
# org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
# Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").
#利用log4j为输出介质
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JCategoryLog
#JDK5 Logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
#log4j.properties文件配置信息,log4j.properties最好放到classpath中.
#如: web-inf/classes目录或其他classpath目录.
log4j.rootLogger=ERROR,stdout,LF5,,file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=sdcrm.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
log4j.appender.LF5=org.apache.log4j.lf5.LF5Appender
log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
log.java接口方法
private static Log lg = LogFactory.getLog(this.getClass().getName());
public static void debug(String className, String debugStr) {
Log log = LogFactory.getLog(className);
if (log.isDebugEnabled()) {
log.debug(debugStr);
}
}
public static void info(String className, String infoStr) {
Log log = LogFactory.getLog(className);
if (log.isInfoEnabled()) {
log.info(infoStr);
}
}
public static void warn(String className, String warnStr) { Log log = LogFactory.getLog(className); if (log.isWarnEnabled()) { log.warn(warnStr); } }
public static void error(String className, String infoStr) { Log log = LogFactory.getLog(className); if (log.isErrorEnabled()) { log.error(infoStr); } }
public static void fatal(String className, String infoStr) { Log log = LogFactory.getLog(className); if (log.isFatalEnabled()) { log.fatal(infoStr); } }
调用发法:
LogMgn.error(this.getClass().getName(),"错误信息");