log4j

作者: stutterr | 来源:发表于2017-07-31 20:28 被阅读16次

log4j.rootLogger=表示根目录
log4j.category.*
是对自定义类的设置,可以对类、包和工程单独设置
log4j.additivity.=*
如果是false ,日志输出不会输出到根内,只会输出到log4j.category.*定义的包的定义目录下

基本配置

log4j.rootLogger = frame,info,debug,error
log4j.appender.frame = org.apache.log4j.ConsoleAppender
log4j.appender.frame.layout = org.apache.log4j.PatternLayout
log4j.appender.frame.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
 

log4j.logger.debug = debug
log4j.appender.debug = com.gavin.exam.common.BookLoggerAppender
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
log4j.appender.debug.datePattern = '.'yyyy-MM-dd
log4j.appender.debug.Threshold  =  DEBUG 
#log4j.appender.debug.MaxFileSize = 1000KB
log4j.appender.debug.append = true
log4j.appender.debug.File = c:/logs/debug.log

log4j.logger.info = info
log4j.appender.info = com.gavin.exam.common.BookLoggerAppender
log4j.appender.info.layout = org.apache.log4j.PatternLayout
log4j.appender.info.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
log4j.appender.info.datePattern = '.'yyyy-MM-dd
log4j.appender.info.Threshold  =  INFO 
#log4j.appender.info.MaxFileSize = 1000KB
log4j.appender.info.append = true
log4j.appender.info.File = c:/logs/info.log


log4j.logger.error = error
log4j.appender.error = com.gavin.exam.common.BookLoggerAppender
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
log4j.appender.error.datePattern = '.'yyyy-MM-dd
log4j.appender.error.Threshold  =  ERROR 
#log4j.appender.error.MaxFileSize = 1000KB
log4j.appender.error.append = true
log4j.appender.error.File = c:/logs/error.log

基本用法

 private final Logger logger =
 Logger.getLogger(UserService.class);
 logger.info(userName+"inof 登陆");

缺点。所有info,error,debeg都会写到一个文件下

进阶配置(将error,info,debeg分开文件)

log4j.rootLogger = frame,info,debug,error
log4j.appender.frame = org.apache.log4j.ConsoleAppender
log4j.appender.frame.layout = org.apache.log4j.PatternLayout
log4j.appender.frame.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
 

log4j.logger.debug = debug
log4j.appender.debug = com.gavin.exam.common.ExamLoggerAppender
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
log4j.appender.debug.datePattern = '.'yyyy-MM-dd
log4j.appender.debug.Threshold  =  DEBUG 
#log4j.appender.debug.MaxFileSize = 1000KB
log4j.appender.debug.append = true
log4j.appender.debug.File = c:/logs/debug.log

log4j.logger.info = info
log4j.appender.info = com.gavin.exam.common.ExamLoggerAppender
log4j.appender.info.layout = org.apache.log4j.PatternLayout
log4j.appender.info.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
log4j.appender.info.datePattern = '.'yyyy-MM-dd
log4j.appender.info.Threshold  =  INFO 
#log4j.appender.info.MaxFileSize = 1000KB
log4j.appender.info.append = true
log4j.appender.info.File = c:/logs/info.log


log4j.logger.error = error
log4j.appender.error = com.gavin.exam.common.ExamLoggerAppender
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
log4j.appender.error.datePattern = '.'yyyy-MM-dd
log4j.appender.error.Threshold  =  ERROR 
#log4j.appender.error.MaxFileSize = 1000KB
log4j.appender.error.append = true
log4j.appender.error.File = c:/logs/error.log

重写的类
···
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Priority;

public class ExamLoggerAppender extends DailyRollingFileAppender {

@Override
public boolean isAsSevereAsThreshold(Priority priority) {

//是指同一级别才返回
return this.getThreshold().equals(priority);
}
}
···

什么时候才要输出log?

  • controller层捕获到参数不对可以logger.info("paraException",paraException)
    最好同时输出userName
  • controller层捕获到serviceException可以logger.warn("serviceException",serviceException) 可能是机器登陆
  • service层可以log出开始时间与结束时间
    将代码包在(可以使用aop即切面优化)
long startTime = System.currentTimeMillis();
/**service代码**/
long endTime = System.currentTimeMillis();

logger.info("methodname time"+(endTime- startTime  ))

aop进行log优化

详细见spring aop优化

相关文章

网友评论

      本文标题:log4j

      本文链接:https://www.haomeiwen.com/subject/xrrrlxtx.html