美文网首页
log4j向log4j2的升级

log4j向log4j2的升级

作者: 超神气的二月飞 | 来源:发表于2017-10-13 14:23 被阅读0次

    升级之后程序里的日志记录代码不需要更改,实验证明log4j2比log4j效率更高

    1、引入官方log4j2 相关资源,maven依赖如下

    2、resource目录下增加log4j2.xml文件

    3、自定义的layout类,可以根据自己需要改写

    package com.bbk.watch.store.common.log4j;

    importjava.io.PrintWriter;

    importjava.io.StringWriter;

    importjava.io.Writer;

    import java.nio.charset.Charset;

    import java.util.Date;

    import org.apache.commons.lang.exception.ExceptionUtils;

    import org.apache.logging.log4j.core.LogEvent;

    import org.apache.logging.log4j.core.config.plugins.Plugin;

    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;

    import org.apache.logging.log4j.core.config.plugins.PluginFactory;

    import org.apache.logging.log4j.core.layout.AbstractStringLayout;

    import com.alibaba.fastjson.JSON;

    import com.alibaba.fastjson.JSONObject;

    import com.bbk.watch.store.common.ElkLog;

    import com.bbk.watch.store.common.ResponseUtils;

    import com.bbk.watch.store.common.ResultBeanFactory;

    @Plugin(name = "SampleLayout", category = "Core", elementType = "layout", printObject = true)

    public class SampleLayout extends AbstractStringLayout {

    /**

    *

    */

    private static final long serialVersionUID = 1L;

    protected SampleLayout(boolean locationInfo, boolean properties, boolean complete,

    Charset charset) {

    super(charset);

    }

    @PluginFactory

    public static SampleLayout createLayout(@PluginAttribute("locationInfo") boolean locationInfo,

    @PluginAttribute("properties") boolean properties,

    @PluginAttribute("complete") boolean complete,

    @PluginAttribute(value = "charset", defaultString = "UTF-8") Charset charset) {

    return new SampleLayout(locationInfo, properties, complete, charset);

    }

    @Override

    public String toSerializable(LogEvent loggingEvent) {

    if (loggingEvent.getMessage().getParameters() == null || loggingEvent.getMessage().getParameters().length == 0) {

    return String.format("[%s] [%s] %s", "elk",

    ResponseUtils.formatDate(new Date()), "不能解析的日志类型" + "\n");

    }

    StringBuilder exceptionBuiler = new StringBuilder();

    Object object = loggingEvent.getMessage().getParameters()[0];

    // if

    // (DaLog.class.isAssignableFrom(loggingEvent.getMessage().getClass()))

    // {

    // return StringUtils.EMPTY;

    // }

    JSONObject logstashEvent = new JSONObject();

    if (loggingEvent.getThrown() != null) {

    final Throwable throwableInformation = loggingEvent.getThrown();

    try {

    Writer w = new StringWriter();

    throwableInformation.printStackTrace(new PrintWriter(w));

    exceptionBuiler.append(w.toString());

    } catch (Exception e) {

    }

    } else if ("ERROR".equals(loggingEvent.getLevel().toString())) {

    if (object instanceof Exception) {

    Exception exception = (Exception) object;

    String stackTrace = ExceptionUtils.getFullStackTrace(exception);

    exceptionBuiler.append(stackTrace);

    }

    }

    if (loggingEvent.isIncludeLocation()) {

    StackTraceElement stackTraceElement = loggingEvent.getSource();

    logstashEvent.put("exceptionName",

    String.format("%s %s %s", stackTraceElement.getClassName(), stackTraceElement.getMethodName(), stackTraceElement.getLineNumber()));

    }

    logstashEvent.put("exceptionLevel", loggingEvent.getLevel().toString());

    if (String.class.isAssignableFrom(object.getClass())) {

    exceptionBuiler.append(object);

    } else if (ElkLog.class.isAssignableFrom(object.getClass())) {

    ElkLog elkLog = (ElkLog) object;

    exceptionBuiler.append(elkLog.getException());

    logstashEvent.put("code", elkLog.getCode());

    logstashEvent.put("desc", elkLog.getDesc());

    } else if (Exception.class.isAssignableFrom(object.getClass())) {

    Exception exception = (Exception) object;

    exceptionBuiler.append(ExceptionUtils.getFullStackTrace(exception));

    logstashEvent.put("code", ResultBeanFactory.FAIL_CODE);

    } else {

    }

    logstashEvent.put("exception", exceptionBuiler.toString());

    return String.format("[%s] [%s] %s", "elk",

    ResponseUtils.formatDate(new Date()), JSON.toJSONString(logstashEvent) + "\n");

    }

    }

    4、去掉web.xml  log4j1.x相关配置,这里tomcat启动日志不会输出到文件,如果需要可以参考官网http://logging.apache.org/log4j/2.x/manual/webapp.html,没对这一部分进行研究。

    相关文章

      网友评论

          本文标题:log4j向log4j2的升级

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