一、简介
Loggers组件在此系统中被分为五个级别:DEBUG、INFO、WARN、ERROR和FATAL。这五个级别是有顺序的,DEBUG < INFO < WARN < ERROR < FATAL,分别用来指定这条日志信息的重要程度,明白这一点很重要,Log4j有一个规则:只输出级别不低于设定级别的日志信息,假设Loggers级别设定为INFO,则INFO、WARN、ERROR和FATAL级别的日志信息都会输出,而级别比INFO低的DEBUG则不会输出
二、log4j.properties文件配置
log4j.rootLogger=DEBUG, stdout, logfile
log4j.category.org.springframework=ERROR
#log4j.category.org.apache=INFO
#log4j.category.org.apache=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${base.logs.dir}/WEB-INF/log/myweb.log
log4j.appender.logfile.MaxFileSize=512KB
log4j.appender.logfile.MaxBackupIndex=5
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
三、代码类
import org.apache.log4j.Logger;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
/**
* @Author: zwj
* @Date: 2020/4/21 16:22
* @Version 1.0
*
* 日志记录类
*/
public class LogUtils implements Serializable {
private static Logger log = Logger.getLogger(LogUtils.class);
/**
* 是否开启Debug
*/
public static boolean isDebug = log.isDebugEnabled();
public static boolean isinfo = log.isInfoEnabled();
private static String debug = "debug";
private static String info = "info";
private static String error = "error";
private static String warn = "warn";
/*** 获取输出信息的代码的位置 ***/
private static String location = "";
public LogUtils(Class<? extends Object> clazz) {
}
/**
* 获取行号和传入类的信息
*/
private static StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
/**
* Debug 输出
* @param
* @param
*/
public static void debug(Object object){
if(!isDebug)
return ;
try {
String location = getLocation(object,debug);
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
}
/**
* warn 输出
*/
public static void warn(Object object){
try {
String location = getLocation(object,warn);
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 异常填充值输出
*/
public static void info(Object object){
if(!isinfo)
return ;
try {
String location = getLocation(object,info);
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 异常填充值输出
*/
public static void error(Object object){
try {
String location = getLocation(object,error);
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 异常填充输出
*/
public static void fmtError(Object object) {
}
public static String getLocation(Object object,String type){
location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
+ " [ " + stacks[2].getLineNumber() + " ] ";
/*** 是否是异常 ***/
if (object instanceof Exception) {
Exception e = (Exception) object;
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
getType(location,type,str);
} else {
getType(location,type,object.toString());
}
return location;
}
public static void getType(String location,String type,String string){
switch (type){
case "debug":
log.debug(location+"[ "+string+" ]");
break;
case "info":
log.info(location+"[ "+string+" ]");
break;
case "warn":
log.warn(location+"[ "+string+" ]");
break;
case "error":
log.error(location+"[ "+string+" ]");
break;
}
}
}
网友评论