log4j异常信息单独保存 log4j包名缩写

来源:互联网 发布:mysql转换日期格式 编辑:程序博客网 时间:2024/06/05 22:55

项目中的日志非常多,如果在服务器上想找error信息麻烦的很。

如果只存储error信息又不知道到底是什么原因导致的报错。

所以我就自己定义了一个DailyRollingFileAppender

package com.moker.utils.log.shorten;import org.apache.log4j.DailyRollingFileAppender;import org.apache.log4j.spi.LocationInfo;import org.apache.log4j.spi.LoggingEvent;import com.moker.utils.string.StringUtils;public class ShortenDailyRollingFileAppender extends DailyRollingFileAppender {/** * 排除page的名称 */private String excludeClassPrefix;/** * 排除page的名称转数组 */private String[] excludeClassPrefixArray;@Overridepublic void append(LoggingEvent event) {LocationInfo locationInfo = event.getLocationInformation();locationInfo.fullInfo = reconstructionFullInfo(event.getLocationInformation());super.append(event);}/** * 重构fullInfo classname改为简写 *  * @param locationInfo * @return * @author sourny * @data 2017年8月7日 */private String reconstructionFullInfo(LocationInfo locationInfo) {String quotes = locationInfo.getClassName();StringBuilder buf = new StringBuilder();if (quotes != null) {boolean isFilter = false;for (String string : excludeClassPrefixArray) {if (quotes.startsWith(string)) {isFilter = true;break;}}if (isFilter)return locationInfo.fullInfo;String[] split = quotes.split("\\.");int length = split.length - 1;for (int i = 0; i < length; i++) {buf.append(split[i].substring(0, 1) + ".");}buf.append(split[length]);buf.append(".");buf.append(locationInfo.getMethodName());buf.append("(line:");buf.append(locationInfo.getLineNumber());buf.append(")");quotes = buf.toString();} else {quotes = LocationInfo.NA;}return quotes;}private void initAbbreviationClassPrefixArray() {excludeClassPrefixArray = new String[] {};if (StringUtils.isNotBlank(excludeClassPrefix))excludeClassPrefixArray = excludeClassPrefix.split(",");}public String getAbbreviationClassPrefix() {return excludeClassPrefix;}public void setAbbreviationClassPrefix(String abbreviationClassPrefix) {this.excludeClassPrefix = abbreviationClassPrefix;initAbbreviationClassPrefixArray();}}package com.moker.utils.log.shorten;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Level;import org.apache.log4j.spi.LocationInfo;import org.apache.log4j.spi.LoggingEvent;public class ErrorDailyRollingFileAppender extends ShortenDailyRollingFileAppender {/** 储存前面多少条日志信息 */private Integer frontSize = 6;/** 储存日志信息集合 */private List logEventList = new ArrayList();public final static String SEPARATOR = "------------------------------";@Overridepublic void append(LoggingEvent event) {pushLoggingEvent(event);LocationInfo locationInfo = new LocationInfo("", SEPARATOR, "", "0");Level level = event.getLevel();LoggingEvent separator = new LoggingEvent(event.fqnOfCategoryClass, event.getLogger(), event.timeStamp, level,"", event.getThreadName(), event.getThrowableInformation(), event.getNDC(), locationInfo,event.getProperties());if (level.toInt() == Level.ERROR_INT) {super.append(separator);for (LoggingEvent loggingEvent : logEventList) {super.append(loggingEvent);}}}private void pushLoggingEvent(LoggingEvent event) {int diff = logEventList.size() - frontSize + 1;if (diff > 0) {for (int i = 0; i < diff; i++)logEventList.remove(i);}logEventList.add(event);}public Integer getFrontSize() {return frontSize;}public void setFrontSize(Integer frontSize) {this.frontSize = frontSize;}}### 输出到日志文件 ###log4j.appender.shorten = com.moker.utils.log.shorten.ShortenDailyRollingFileAppender###设置DatePattern,当天日志保存到log.log文件,前一天的日志文件名将被修改为#log.log + _yyyy-MM-dd.loglog4j.appender.shorten.File =../mokerlogs/api/shorten.loglog4j.appender.shorten.DatePattern = '_'yyyy-MM-dd'.log'log4j.appender.shorten.Append = truelog4j.appender.shorten.abbreviationClassPrefix = com.moker## 输出DEBUG级别以上的日志log4j.appender.shorten.Threshold = DEBUGlog4j.appender.shorten.layout =com.moker.utils.log.ExPatternLayoutlog4j.appender.shorten.layout.ConversionPattern = [%d{yyyy-MM-dd HH\:mm\:ss\.SSS}][%-5p] --> [%t] : %l %m %x %n### 输出到日志文件 ###log4j.appender.errorShorten = com.moker.utils.log.shorten.ErrorDailyRollingFileAppender###设置DatePattern,当天日志保存到log.log文件,前一天的日志文件名将被修改为#log.log + _yyyy-MM-dd.loglog4j.appender.errorShorten.File =../mokerlogs/api/error.loglog4j.appender.errorShorten.DatePattern = '_'yyyy-MM-dd'.log'log4j.appender.errorShorten.Append = truelog4j.appender.errorShorten.abbreviationClassPrefix = com.mokerlog4j.appender.errorShorten.frontSize = 6## 输出DEBUG级别以上的日志log4j.appender.errorShorten.Threshold = DEBUGlog4j.appender.errorShorten.layout =com.moker.utils.log.ExPatternLayoutlog4j.appender.errorShorten.layout.ConversionPattern = [%d{yyyy-MM-dd HH\:mm\:ss\.SSS}][%-5p] --> [%t] : %l %m %x %n

因为是在日志中查看jar包里的不需要显示的太完整
所以我把jar包里的包名全部改为缩写

ERROR日志显示效果

初步构思,待优化