package cn.yc.com.Test.logs;
import org.apache.log4j.Logger;
/**
*
* <p>Description: log4j的使用方法,及级别定义</p>
* @author 余辉
* @date 2015年8月17日上午9:49:20
* @version 1.0
*/
public class logs {
private static Logger logger = Logger.getLogger(logs.class.getName());
public static void main(String[] args) {
logger.info("logs类main方法:"+logs.class.getName());
int sum =0;
logger.debug("debug信息");
logger.info("info信息");
logger.warn("warn信息");
logger.error("error信息");
try {
sum = 1/0;//这是错误代码
} catch (Exception e) {
logger.error("error信息-->" + e.getMessage());
}
}
}
log4j.properties的配置信息
log4j.rootLogger=ALL,stdoutlog,debuglog,infolog,warnlog,errorlog,fatallog
log4j.appender.stdoutlog=org.apache.log4j.ConsoleAppender
log4j.appender.stdoutlog.layout=org.apache.log4j.PatternLayout
log4j.appender.stdoutlog.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
#file debug
#log4j.appender.debuglog=com.mfniu.log.LogAppender
#log4j.appender.debuglog.layout=org.apache.log4j.PatternLayout
#log4j.appender.debuglog.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
#log4j.appender.debuglog.datePattern='.'yyyy-MM-dd
#log4j.appender.debuglog.Threshold = DEBUG
#log4j.appender.debuglog.append=true
#log4j.appender.debuglog.File=logs/debug.log
#file info
log4j.appender.infolog=cn.yc.com.log.LogAppender
log4j.appender.infolog.layout=org.apache.log4j.PatternLayout
log4j.appender.infolog.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.infolog.datePattern='.'yyyy-MM-dd
log4j.appender.infolog.Threshold = INFO
log4j.appender.infolog.append=true
log4j.appender.infolog.File=/logs/info.log
#file warn
log4j.appender.warnlog=cn.yc.com.log.LogAppender
log4j.appender.warnlog.layout=org.apache.log4j.PatternLayout
log4j.appender.warnlog.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.warnlog.datePattern='.'yyyy-MM-dd
log4j.appender.warnlog.Threshold = WARN
log4j.appender.warnlog.append=true
log4j.appender.warnlog.File=/logs/warn.log
#file error
log4j.appender.errorlog=cn.yc.com.log.LogAppender
log4j.appender.errorlog.layout=org.apache.log4j.PatternLayout
log4j.appender.errorlog.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.errorlog.datePattern='.'yyyy-MM-dd
log4j.appender.errorlog.Threshold = ERROR
log4j.appender.errorlog.append=true
log4j.appender.errorlog.File=/logs/error.log
#file fatal
log4j.appender.fatallog=cn.yc.com.log.LogAppender
log4j.appender.fatallog.layout=org.apache.log4j.PatternLayout
log4j.appender.fatallog.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.fatallog.datePattern='.'yyyy-MM-dd
log4j.appender.fatallog.Threshold = FATAL
log4j.appender.fatallog.append=true
log4j.appender.fatallog.File=/logs/fatal.log