1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| package com.ming.log;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.Node; import org.apache.logging.log4j.core.config.plugins.*; import org.apache.logging.log4j.core.layout.AbstractStringLayout; import org.apache.logging.log4j.core.layout.PatternLayout; import org.apache.logging.log4j.core.layout.PatternSelector; import org.apache.logging.log4j.core.pattern.RegexReplacement;
import java.io.File; import java.nio.charset.Charset;
/** * boss json格式日志 * <p> * 配合elk的配置 使用 * 原作者文章地址: https://blog.csdn.net/lnkToKing/article/details/79563460 * * @author ming * @date 2018-06-22 10:59:56 */ @Plugin(name = "MingJsonPatternLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) public class MingJsonPatternLayout extends AbstractStringLayout { /** * 项目路径 */ private static String PROJECT_PATH;
private PatternLayout patternLayout;
private String projectName; private String logType;
static { PROJECT_PATH = new File("").getAbsolutePath(); }
private BossJsonPatternLayout(Configuration config, RegexReplacement replace, String eventPattern, PatternSelector patternSelector, Charset charset, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi, String headerPattern, String footerPattern, String projectName, String logType) { super(config, charset, PatternLayout.createSerializer(config, replace, headerPattern, null, patternSelector, alwaysWriteExceptions, noConsoleNoAnsi), PatternLayout.createSerializer(config, replace, footerPattern, null, patternSelector, alwaysWriteExceptions, noConsoleNoAnsi));
this.projectName = projectName; this.logType = logType; this.patternLayout = PatternLayout.newBuilder() .withPattern(eventPattern) .withPatternSelector(patternSelector) .withConfiguration(config) .withRegexReplacement(replace) .withCharset(charset) .withAlwaysWriteExceptions(alwaysWriteExceptions) .withNoConsoleNoAnsi(noConsoleNoAnsi) .withHeader(headerPattern) .withFooter(footerPattern) .build(); }
@Override public String toSerializable(LogEvent event) { //在这里处理日志内容 String message = patternLayout.toSerializable(event); String jsonStr = new JsonLoggerInfo(projectName, message, event.getLevel().name(), logType, event.getTimeMillis()).toString(); return jsonStr + "\n"; }
@PluginFactory public static BossJsonPatternLayout createLayout( @PluginAttribute(value = "pattern", defaultString = PatternLayout.DEFAULT_CONVERSION_PATTERN) final String pattern, @PluginElement("PatternSelector") final PatternSelector patternSelector, @PluginConfiguration final Configuration config, @PluginElement("Replace") final RegexReplacement replace, // LOG4J2-783 use platform default by default, so do not specify defaultString for charset @PluginAttribute(value = "charset") final Charset charset, @PluginAttribute(value = "alwaysWriteExceptions", defaultBoolean = true) final boolean alwaysWriteExceptions, @PluginAttribute(value = "noConsoleNoAnsi", defaultBoolean = false) final boolean noConsoleNoAnsi, @PluginAttribute("header") final String headerPattern, @PluginAttribute("footer") final String footerPattern, @PluginAttribute("projectName") final String projectName, @PluginAttribute("logType") final String logType) {
return new BossJsonPatternLayout(config, replace, pattern, patternSelector, charset, alwaysWriteExceptions, noConsoleNoAnsi, headerPattern, footerPattern, projectName, logType); }
/** * 输出的日志内容 */ public static class JsonLoggerInfo { /** * 项目名 */ private String projectName; /** * 项目目录路径 */ private String projectPath; /** * 日志信息 */ private String message; /** * 日志级别 */ private String level; /** * 日志分类 */ private String logType; /** * 日志时间 */ private String time;
public JsonLoggerInfo(String projectName, String message, String level, String logType, long timeMillis) { this.projectName = projectName; this.projectPath = PROJECT_PATH; this.message = message; this.level = level; this.logType = logType; this.time = DateFormatUtils.format(timeMillis, "yyyy-MM-dd HH:mm:ss.SSS"); }
public String getProjectName() { return projectName; }
public String getProjectPath() { return projectPath; }
public String getMessage() { return message; }
public String getLevel() { return level; }
public String getLogType() { return logType; }
public String getTime() { return time; }
@Override public String toString() { try { return new ObjectMapper().writeValueAsString(this); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } } }
|