diff --git a/FASTSTART.md b/FASTSTART.md index a9504b1b106a202dd1bff9994cf7ad72ab4abdcf..00b60a9615e208abee843a7df2d622ff0c4d38a7 100644 --- a/FASTSTART.md +++ b/FASTSTART.md @@ -182,11 +182,13 @@ log4j.appender.L=com.plumelog.log4j.appender.KafkaAppender #appName系统的名称(自己定义就好) log4j.appender.L.appName=plumelog + log4j.appender.L.env=${spring.profiles.active} log4j.appender.L.kafkaHosts=172.16.247.143:9092,172.16.247.60:9092,172.16.247.64:9092 #redis做为中间件 log4j.appender.L=com.plumelog.log4j.appender.RedisAppender log4j.appender.L.appName=plumelog + log4j.appender.L.env=${spring.profiles.active} log4j.appender.L.redisHost=172.16.249.72:6379 #redis没有密码这一项为空或者不需要 #log4j.appender.L.redisAuth=123456 diff --git a/plumelog-core/src/main/java/com/plumelog/core/dto/BaseLogMessage.java b/plumelog-core/src/main/java/com/plumelog/core/dto/BaseLogMessage.java index 343bcc8e500feb021e8595b5554dbdce720a72ed..0be758e8aa21de39f1fda0ad345d2e63b0c1f71d 100644 --- a/plumelog-core/src/main/java/com/plumelog/core/dto/BaseLogMessage.java +++ b/plumelog-core/src/main/java/com/plumelog/core/dto/BaseLogMessage.java @@ -23,7 +23,12 @@ public class BaseLogMessage { * 应用名 */ private String appName; - + + /** + * 应用环境 + */ + private String env; + /** * 方法名 */ @@ -44,7 +49,15 @@ public class BaseLogMessage { public void setAppName(String appName) { this.appName = appName; } - + + public void setEnv(String env) { + this.env = env; + } + + public String getEnv() { + return env; + } + public String getMethod() { return method; } @@ -60,4 +73,8 @@ public class BaseLogMessage { public void setServerName(String serverName) { this.serverName = serverName; } + + public String getAppNameWithEnv() { + return this.appName + "-_-" + this.env; + } } diff --git a/plumelog-core/src/main/java/com/plumelog/core/dto/WarningRule.java b/plumelog-core/src/main/java/com/plumelog/core/dto/WarningRule.java index ada523a2843c289ed7319d6b059ea91a06057b19..6d829dd1300f65239676281b4ef43f9cffef38bc 100644 --- a/plumelog-core/src/main/java/com/plumelog/core/dto/WarningRule.java +++ b/plumelog-core/src/main/java/com/plumelog/core/dto/WarningRule.java @@ -11,6 +11,7 @@ package com.plumelog.core.dto; public class WarningRule { private String appName; + private String env; private String appCategory; private String className; private String receiver; @@ -27,7 +28,15 @@ public class WarningRule { public void setAppName(String appName) { this.appName = appName; } - + + public String getEnv() { + return env; + } + + public void setEnv(String env) { + this.env = env; + } + public String getAppCategory() { return appCategory; } diff --git a/plumelog-core/src/main/java/com/plumelog/core/dto/WarningRuleDto.java b/plumelog-core/src/main/java/com/plumelog/core/dto/WarningRuleDto.java index 4cf1aaaa1587716487b32e1597343ac69d738fb1..413a57b4c5262a6b9c5d5d7746c5ff1c3cda8a66 100644 --- a/plumelog-core/src/main/java/com/plumelog/core/dto/WarningRuleDto.java +++ b/plumelog-core/src/main/java/com/plumelog/core/dto/WarningRuleDto.java @@ -1,7 +1,5 @@ package com.plumelog.core.dto; -import com.plumelog.core.dto.WarningRule; - /** * className:WarningRule * description:错误告警规则 diff --git a/plumelog-core/src/main/java/com/plumelog/core/util/TraceLogMessageFactory.java b/plumelog-core/src/main/java/com/plumelog/core/util/TraceLogMessageFactory.java index 6e6b503c95d55c9d8c2dc253e77c3ac3d6ac02a9..ce4940c467cc3d77d4cf7493bd13abd92ec11c8a 100644 --- a/plumelog-core/src/main/java/com/plumelog/core/util/TraceLogMessageFactory.java +++ b/plumelog-core/src/main/java/com/plumelog/core/util/TraceLogMessageFactory.java @@ -14,10 +14,11 @@ import com.plumelog.core.TraceMessage; * @version 1.0.0 */ public class TraceLogMessageFactory { - - public static TraceLogMessage getTraceLogMessage(TraceMessage traceMessage, String appName, long time) { + + public static TraceLogMessage getTraceLogMessage(TraceMessage traceMessage, String appName, String env, long time) { TraceLogMessage traceLogMessage = new TraceLogMessage(); traceLogMessage.setAppName(appName); + traceLogMessage.setEnv(env); traceLogMessage.setTraceId(traceMessage.getTraceId()); traceLogMessage.setMethod(traceMessage.getMessageType()); traceLogMessage.setTime(time); @@ -26,24 +27,25 @@ public class TraceLogMessageFactory { traceLogMessage.setServerName(IpGetter.CURRENT_IP); return traceLogMessage; } - - public static RunLogMessage getLogMessage(String appName, String message, long time) { + + public static RunLogMessage getLogMessage(String appName, String env, String message, long time) { RunLogMessage logMessage = new RunLogMessage(); logMessage.setServerName(IpGetter.CURRENT_IP); logMessage.setAppName(appName); + logMessage.setEnv(env); logMessage.setContent(message); logMessage.setDtTime(time); logMessage.setTraceId(TraceId.logTraceID.get()); return logMessage; } - + public static String packageMessage(String message, Object[] args) { StringBuilder builder = new StringBuilder(128); builder.append(message); - for (int i = 0; i < args.length; i++) { - builder.append("\n").append(args[i]); + for (Object arg : args) { + builder.append("\n").append(arg); } return builder.toString(); } - + } diff --git a/plumelog-log4j/src/main/java/com/plumelog/log4j/appender/KafkaAppender.java b/plumelog-log4j/src/main/java/com/plumelog/log4j/appender/KafkaAppender.java index b313a2888e234b895c0d589c2f991e750dc55a90..699c5b1b9e1319d2c3d5bac12b16f80991ff4048 100644 --- a/plumelog-log4j/src/main/java/com/plumelog/log4j/appender/KafkaAppender.java +++ b/plumelog-log4j/src/main/java/com/plumelog/log4j/appender/KafkaAppender.java @@ -23,11 +23,12 @@ import java.util.concurrent.ThreadPoolExecutor; public class KafkaAppender extends AppenderSkeleton { private KafkaProducerClient kafkaClient; private String appName; + private String env = "default"; private String kafkaHosts; private String runModel; - private int maxCount=100; - private int logQueueSize=10000; - private int threadPoolSize=1; + private int maxCount = 100; + private int logQueueSize = 10000; + private int threadPoolSize = 1; private boolean compressor = false; public void setAppName(String appName) { @@ -57,28 +58,32 @@ public class KafkaAppender extends AppenderSkeleton { public void setCompressor(boolean compressor) { this.compressor = compressor; } - - private static ThreadPoolExecutor threadPoolExecutor - = ThreadPoolUtil.getPool(); + + public void setEnv(String env) { + this.env = env; + } + + private static ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtil.getPool(); + @Override protected void append(LoggingEvent loggingEvent) { - if(this.runModel!=null){ - LogMessageConstant.RUN_MODEL=Integer.parseInt(this.runModel); + if (this.runModel != null) { + LogMessageConstant.RUN_MODEL = Integer.parseInt(this.runModel); } if (this.kafkaClient == null) { this.kafkaClient = KafkaProducerClient.getInstance(this.kafkaHosts, this.compressor ? "lz4" : "none"); MessageAppenderFactory.initQueue(this.logQueueSize); - for(int a=0;a{ - MessageAppenderFactory.startRunLog(this.kafkaClient,maxCount); + threadPoolExecutor.execute(() -> { + MessageAppenderFactory.startRunLog(this.kafkaClient, maxCount); }); - threadPoolExecutor.execute(()->{ - MessageAppenderFactory.startTraceLog(this.kafkaClient,maxCount); + threadPoolExecutor.execute(() -> { + MessageAppenderFactory.startTraceLog(this.kafkaClient, maxCount); }); } } - final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(this.appName, loggingEvent); + final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(this.appName, this.env, loggingEvent); if (logMessage instanceof RunLogMessage) { final String message = LogMessageUtil.getLogMessage(logMessage, loggingEvent); MessageAppenderFactory.pushRundataQueue(message); diff --git a/plumelog-log4j/src/main/java/com/plumelog/log4j/appender/RedisAppender.java b/plumelog-log4j/src/main/java/com/plumelog/log4j/appender/RedisAppender.java index 7b81a64162a2b7048c2abfae37ce3e9f7d918b38..618de5060488a0cba12ad05a1ecfb650246b0e84 100644 --- a/plumelog-log4j/src/main/java/com/plumelog/log4j/appender/RedisAppender.java +++ b/plumelog-log4j/src/main/java/com/plumelog/log4j/appender/RedisAppender.java @@ -14,7 +14,6 @@ import com.plumelog.core.redis.RedisClient; import org.apache.log4j.AppenderSkeleton; import org.apache.log4j.spi.LoggingEvent; -import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; /** @@ -27,6 +26,7 @@ import java.util.concurrent.ThreadPoolExecutor; public class RedisAppender extends AppenderSkeleton { private AbstractClient redisClient; private String appName; + private String env = "default"; private String redisHost; private String redisPort; private String redisAuth; @@ -94,34 +94,43 @@ public class RedisAppender extends AppenderSkeleton { public void setMasterName(String masterName) { this.masterName = masterName; } - - private static ThreadPoolExecutor threadPoolExecutor - = ThreadPoolUtil.getPool(); - + + public void setEnv(String env) { + this.env = env; + } + + private static ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtil.getPool(); + @Override protected void append(LoggingEvent loggingEvent) { if (this.runModel != null) { LogMessageConstant.RUN_MODEL = Integer.parseInt(this.runModel); } if (this.redisClient == null) { - if (this.model.equals("cluster")) { + if ("cluster".equals(this.model)) { this.redisClient = RedisClusterClient.getInstance(this.redisHost, this.redisAuth); - } else if (this.model.equals("sentinel")) { - this.redisClient = RedisSentinelClient.getInstance(this.redisHost, this.masterName, this.redisAuth, this.redisDb); + } else if ("sentinel".equals(this.model)) { + this.redisClient = RedisSentinelClient + .getInstance(this.redisHost, this.masterName, this.redisAuth, this.redisDb); } else { int port = 6379; String ip = "127.0.0.1"; - if(this.redisPort==null){ - String[] hs = redisHost.split(":"); - if (hs.length == 2) { - ip = hs[0]; - port = Integer.valueOf(hs[1]); + if (this.redisPort == null) { + // 如果redisHost不包含:号则端口号默认使用6379 + if (redisHost.contains(":")) { + String[] hs = redisHost.split(":"); + if (hs.length == 2) { + ip = hs[0]; + port = Integer.parseInt(hs[1]); + } + } else { + ip = this.redisHost; } - }else { - ip=this.redisHost; - port=Integer.parseInt(this.redisPort); + } else { + ip = this.redisHost; + port = Integer.parseInt(this.redisPort); } - this.redisClient = RedisClient.getInstance(ip,port,this.redisAuth, this.redisDb); + this.redisClient = RedisClient.getInstance(ip, port, this.redisAuth, this.redisDb); } MessageAppenderFactory.initQueue(this.logQueueSize); @@ -137,7 +146,7 @@ public class RedisAppender extends AppenderSkeleton { }); } } - final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(this.appName, loggingEvent); + final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(this.appName, this.env, loggingEvent); if (logMessage instanceof RunLogMessage) { final String message = LogMessageUtil.getLogMessage(logMessage, loggingEvent); MessageAppenderFactory.pushRundataQueue(message); diff --git a/plumelog-log4j/src/main/java/com/plumelog/log4j/util/LogMessageUtil.java b/plumelog-log4j/src/main/java/com/plumelog/log4j/util/LogMessageUtil.java index 879c6632f9c5c3f8ca2ae47e85442cfaab017a86..6901167345b5e60a3a555db966e33e60cfcc9222 100644 --- a/plumelog-log4j/src/main/java/com/plumelog/log4j/util/LogMessageUtil.java +++ b/plumelog-log4j/src/main/java/com/plumelog/log4j/util/LogMessageUtil.java @@ -15,7 +15,6 @@ import org.apache.log4j.spi.LoggingEvent; import org.slf4j.helpers.FormattingTuple; import org.slf4j.helpers.MessageFormatter; -import java.sql.Timestamp; import java.util.Date; import java.util.Map; @@ -27,23 +26,23 @@ import java.util.Map; * @version 1.0.0 */ public class LogMessageUtil { - - public static BaseLogMessage getLogMessage(String appName, LoggingEvent loggingEvent) { + + public static BaseLogMessage getLogMessage(String appName, String env, LoggingEvent loggingEvent) { TraceMessage traceMessage = LogMessageThreadLocal.logMessageThreadLocal.get(); String formattedMessage = getMessage(loggingEvent); if (formattedMessage.startsWith(LogMessageConstant.TRACE_PRE)) { return TraceLogMessageFactory.getTraceLogMessage( - traceMessage, appName, loggingEvent.getTimeStamp()); + traceMessage, appName, env, loggingEvent.getTimeStamp()); } RunLogMessage logMessage = - TraceLogMessageFactory.getLogMessage(appName, formattedMessage, loggingEvent.getTimeStamp()); + TraceLogMessageFactory.getLogMessage(appName, env, formattedMessage, loggingEvent.getTimeStamp()); logMessage.setClassName(loggingEvent.getLoggerName()); - LocationInfo locationInfo=loggingEvent.getLocationInformation(); - String method=locationInfo.getMethodName(); - String line=locationInfo.getLineNumber(); - logMessage.setMethod(method+"("+locationInfo.getFileName()+":"+line+")"); - logMessage.setDateTime(DateUtil.parseDateToStr(new Date(loggingEvent.getTimeStamp()),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI)); + LocationInfo locationInfo = loggingEvent.getLocationInformation(); + String method = locationInfo.getMethodName(); + String line = locationInfo.getLineNumber(); + logMessage.setMethod(method + "(" + locationInfo.getFileName() + ":" + line + ")"); + logMessage.setDateTime(DateUtil.parseDateToStr(new Date(loggingEvent.getTimeStamp()), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI)); logMessage.setLogLevel(loggingEvent.getLevel().toString()); return logMessage; @@ -55,10 +54,11 @@ public class LogMessageUtil { * @param logEvent * @return */ - public static String getLogMessage(BaseLogMessage baseLogMessage,final LoggingEvent logEvent){ - Map mdc= logEvent.getProperties(); - Map map= GfJsonUtil.parseObject(GfJsonUtil.toJSONString(baseLogMessage),Map.class); - if(mdc!=null) { + @SuppressWarnings("unchecked") + public static String getLogMessage(BaseLogMessage baseLogMessage, final LoggingEvent logEvent) { + Map mdc = logEvent.getProperties(); + Map map = GfJsonUtil.parseObject(GfJsonUtil.toJSONString(baseLogMessage), Map.class); + if (mdc != null) { map.putAll(mdc); } return GfJsonUtil.toJSONString(map); @@ -66,11 +66,12 @@ public class LogMessageUtil { private static String getMessage(LoggingEvent logEvent) { if (logEvent.getLevel().toInt() == Priority.ERROR_INT) { String msg = ""; - if (logEvent.getThrowableInformation() != null){ + if (logEvent.getThrowableInformation() != null) { msg = LogExceptionStackTrace.erroStackTrace( logEvent.getThrowableInformation().getThrowable()).toString(); } - if (logEvent.getRenderedMessage()!=null&&logEvent.getRenderedMessage().indexOf(LogMessageConstant.DELIM_STR) > -1) { + if (logEvent.getRenderedMessage() != null && logEvent.getRenderedMessage() + .contains(LogMessageConstant.DELIM_STR)) { FormattingTuple format = MessageFormatter.format(logEvent.getRenderedMessage(), msg); return format.getMessage(); } diff --git a/plumelog-log4j2/src/main/java/com/plumelog/log4j2/appender/KafkaAppender.java b/plumelog-log4j2/src/main/java/com/plumelog/log4j2/appender/KafkaAppender.java index 181a4a9aba28de417b9a24ad16aca9023116c229..709c624694d6bb2e79a67cb36e6ea3bce6aa1dfd 100644 --- a/plumelog-log4j2/src/main/java/com/plumelog/log4j2/appender/KafkaAppender.java +++ b/plumelog-log4j2/src/main/java/com/plumelog/log4j2/appender/KafkaAppender.java @@ -18,7 +18,6 @@ import org.apache.logging.log4j.core.config.plugins.PluginElement; import org.apache.logging.log4j.core.config.plugins.PluginFactory; import java.io.Serializable; -import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; /** @@ -32,6 +31,7 @@ import java.util.concurrent.ThreadPoolExecutor; public class KafkaAppender extends AbstractAppender { private static KafkaProducerClient kafkaClient; private String appName; + private String env; private String kafkaHosts; private String runModel; private String expand; @@ -39,10 +39,11 @@ public class KafkaAppender extends AbstractAppender { private int logQueueSize = 10000; private int threadPoolSize = 1; - protected KafkaAppender(String name, String appName, String kafkaHosts, String runModel, Filter filter, Layout layout, + protected KafkaAppender(String name, String appName, String env, String kafkaHosts, String runModel, Filter filter, Layout layout, final boolean ignoreExceptions, String expand, int maxCount, int logQueueSize, int threadPoolSize) { super(name, filter, layout, ignoreExceptions); this.appName = appName; + this.env = env; this.kafkaHosts = kafkaHosts; this.runModel = runModel; this.expand = expand; @@ -57,7 +58,7 @@ public class KafkaAppender extends AbstractAppender { } protected void send(LogEvent logEvent) { - final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, logEvent); + final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, env, logEvent); if (logMessage instanceof RunLogMessage) { final String message = LogMessageUtil.getLogMessage(logMessage, logEvent); MessageAppenderFactory.pushRundataQueue(message); @@ -73,6 +74,7 @@ public class KafkaAppender extends AbstractAppender { public static KafkaAppender createAppender( @PluginAttribute("name") String name, @PluginAttribute("appName") String appName, + @PluginAttribute("env") String env, @PluginAttribute("kafkaHosts") String kafkaHosts, @PluginAttribute("topic") String topic, @PluginAttribute("expand") String expand, @@ -83,6 +85,9 @@ public class KafkaAppender extends AbstractAppender { @PluginAttribute("compressor") boolean compressor, @PluginElement("Layout") Layout layout, @PluginElement("Filter") final Filter filter) { + if (env == null) { + env = "default"; + } if (runModel != null) { LogMessageConstant.RUN_MODEL = Integer.parseInt(runModel); } @@ -112,6 +117,6 @@ public class KafkaAppender extends AbstractAppender { MessageAppenderFactory.startTraceLog(kafkaClient, count); }); } - return new KafkaAppender(name, appName, kafkaHosts, runModel, filter, layout, true, expand, maxCount, logQueueSize, threadPoolSize); + return new KafkaAppender(name, appName, env, kafkaHosts, runModel, filter, layout, true, expand, maxCount, logQueueSize, threadPoolSize); } } diff --git a/plumelog-log4j2/src/main/java/com/plumelog/log4j2/appender/RedisAppender.java b/plumelog-log4j2/src/main/java/com/plumelog/log4j2/appender/RedisAppender.java index 8125d44acb9ac185b0585f9e3dfe2e68e9ee5f24..7cb738cacc90d8fb5b6bfcbeb0b16a70bdaefcaa 100644 --- a/plumelog-log4j2/src/main/java/com/plumelog/log4j2/appender/RedisAppender.java +++ b/plumelog-log4j2/src/main/java/com/plumelog/log4j2/appender/RedisAppender.java @@ -21,7 +21,6 @@ import org.apache.logging.log4j.core.config.plugins.PluginElement; import org.apache.logging.log4j.core.config.plugins.PluginFactory; import java.io.Serializable; -import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; /** @@ -35,6 +34,7 @@ import java.util.concurrent.ThreadPoolExecutor; public class RedisAppender extends AbstractAppender { private static AbstractClient redisClient; private String appName; + private String env; private String redisHost; private String redisPort; private String redisAuth; @@ -48,10 +48,11 @@ public class RedisAppender extends AbstractAppender { private String model = "standalone"; private String masterName; - protected RedisAppender(String name, String appName, String redisHost, String redisPort, String redisAuth, String runModel, Filter filter, Layout layout, + protected RedisAppender(String name, String appName, String env, String redisHost, String redisPort, String redisAuth, String runModel, Filter filter, Layout layout, final boolean ignoreExceptions, String expand, int maxCount, int logQueueSize, int redisDb, int threadPoolSize, boolean compressor, String model, String masterName) { super(name, filter, layout, ignoreExceptions); this.appName = appName; + this.env = env; this.redisHost = redisHost; this.redisPort = redisPort; this.redisAuth = redisAuth; @@ -72,7 +73,7 @@ public class RedisAppender extends AbstractAppender { } protected void send(LogEvent logEvent) { - final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, logEvent); + final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, env, logEvent); if (logMessage instanceof RunLogMessage) { final String message = LogMessageUtil.getLogMessage(logMessage, logEvent); MessageAppenderFactory.pushRundataQueue(message); @@ -88,6 +89,7 @@ public class RedisAppender extends AbstractAppender { public static RedisAppender createAppender( @PluginAttribute("name") String name, @PluginAttribute("appName") String appName, + @PluginAttribute("env") String env, @PluginAttribute("redisHost") String redisHost, @PluginAttribute("redisPort") String redisPort, @PluginAttribute("redisAuth") String redisAuth, @@ -102,6 +104,9 @@ public class RedisAppender extends AbstractAppender { @PluginAttribute("masterName") String masterName, @PluginElement("Layout") Layout layout, @PluginElement("Filter") final Filter filter) { + if (env == null) { + env = "default"; + } if (runModel != null) { LogMessageConstant.RUN_MODEL = Integer.parseInt(runModel); } @@ -124,7 +129,7 @@ public class RedisAppender extends AbstractAppender { String[] hs = redisHost.split(":"); if (hs.length == 2) { ip = hs[0]; - port = Integer.valueOf(hs[1]); + port = Integer.parseInt(hs[1]); } } else { ip = redisHost; @@ -156,6 +161,6 @@ public class RedisAppender extends AbstractAppender { compressor ? LogMessageConstant.LOG_KEY_TRACE_COMPRESS : LogMessageConstant.LOG_KEY_TRACE, compressor); }); } - return new RedisAppender(name, appName, redisHost, redisPort, redisAuth, runModel, filter, layout, true, expand, maxCount, logQueueSize, redisDb, threadPoolSize, compressor, model, masterName); + return new RedisAppender(name, appName, env, redisHost, redisPort, redisAuth, runModel, filter, layout, true, expand, maxCount, logQueueSize, redisDb, threadPoolSize, compressor, model, masterName); } } diff --git a/plumelog-log4j2/src/main/java/com/plumelog/log4j2/util/LogMessageUtil.java b/plumelog-log4j2/src/main/java/com/plumelog/log4j2/util/LogMessageUtil.java index c3cf8c4cba5cf09bf6a82b5bbbd3d46135c317b1..b3150c17db8481a103ec805db65e60a1aa0ef60a 100644 --- a/plumelog-log4j2/src/main/java/com/plumelog/log4j2/util/LogMessageUtil.java +++ b/plumelog-log4j2/src/main/java/com/plumelog/log4j2/util/LogMessageUtil.java @@ -31,10 +31,10 @@ public class LogMessageUtil { private static String isExpandRunLog(LogEvent logEvent) { String traceId = null; - if (!logEvent.getContextData().isEmpty()) { - traceId = logEvent.getContextData().toMap().get(LogMessageConstant.TRACE_ID); - TraceId.logTraceID.set(traceId); - } + if (!logEvent.getContextData().isEmpty()) { + traceId = logEvent.getContextData().toMap().get(LogMessageConstant.TRACE_ID); + TraceId.logTraceID.set(traceId); + } return traceId; } @@ -45,6 +45,7 @@ public class LogMessageUtil { * @param logEvent * @return */ + @SuppressWarnings("unchecked") public static String getLogMessage(BaseLogMessage baseLogMessage, final LogEvent logEvent) { Map mdc = logEvent.getContextData().toMap(); Map map = GfJsonUtil.parseObject(GfJsonUtil.toJSONString(baseLogMessage), Map.class); @@ -55,16 +56,16 @@ public class LogMessageUtil { return GfJsonUtil.toJSONString(map); } - public static BaseLogMessage getLogMessage(String appName, LogEvent logEvent) { + public static BaseLogMessage getLogMessage(String appName, String env, LogEvent logEvent) { isExpandRunLog(logEvent); TraceMessage traceMessage = LogMessageThreadLocal.logMessageThreadLocal.get(); String formattedMessage = getMessage(logEvent); if (formattedMessage.startsWith(LogMessageConstant.TRACE_PRE)) { return TraceLogMessageFactory.getTraceLogMessage( - traceMessage, appName, logEvent.getTimeMillis()); + traceMessage, appName, env, logEvent.getTimeMillis()); } RunLogMessage logMessage = - TraceLogMessageFactory.getLogMessage(appName, formattedMessage, logEvent.getTimeMillis()); + TraceLogMessageFactory.getLogMessage(appName, env, formattedMessage, logEvent.getTimeMillis()); logMessage.setClassName(logEvent.getLoggerName()); StackTraceElement stackTraceElement = logEvent.getSource(); diff --git a/plumelog-logback/src/main/java/com/plumelog/logback/appender/KafkaAppender.java b/plumelog-logback/src/main/java/com/plumelog/logback/appender/KafkaAppender.java index bd75050cce57ce32e8c5ea6fc0642ce420f2198b..0223d2103a13228f7906debc131d48012beee117 100644 --- a/plumelog-logback/src/main/java/com/plumelog/logback/appender/KafkaAppender.java +++ b/plumelog-logback/src/main/java/com/plumelog/logback/appender/KafkaAppender.java @@ -24,6 +24,7 @@ import java.util.concurrent.ThreadPoolExecutor; public class KafkaAppender extends AppenderBase { private KafkaProducerClient kafkaClient; private String appName; + private String env = "default"; private String kafkaHosts; private String runModel; private String expand; @@ -43,7 +44,11 @@ public class KafkaAppender extends AppenderBase { public void setAppName(String appName) { this.appName = appName; } - + + public void setEnv(String env) { + this.env = env; + } + public void setKafkaHosts(String kafkaHosts) { this.kafkaHosts = kafkaHosts; } @@ -73,7 +78,7 @@ public class KafkaAppender extends AppenderBase { send(event); } protected void send(ILoggingEvent event) { - final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, event); + final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, env, event); if (logMessage instanceof RunLogMessage) { final String message = LogMessageUtil.getLogMessage(logMessage, event); MessageAppenderFactory.pushRundataQueue(message); diff --git a/plumelog-logback/src/main/java/com/plumelog/logback/appender/RedisAppender.java b/plumelog-logback/src/main/java/com/plumelog/logback/appender/RedisAppender.java index cff13adb2c28a3add7fb31ece9e953aeb36672d7..9c5ad69de312170ee49726730916534ea4ae76b5 100644 --- a/plumelog-logback/src/main/java/com/plumelog/logback/appender/RedisAppender.java +++ b/plumelog-logback/src/main/java/com/plumelog/logback/appender/RedisAppender.java @@ -14,7 +14,6 @@ import com.plumelog.core.util.GfJsonUtil; import com.plumelog.core.util.ThreadPoolUtil; import com.plumelog.logback.util.LogMessageUtil; -import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; /** @@ -28,6 +27,7 @@ import java.util.concurrent.ThreadPoolExecutor; public class RedisAppender extends AppenderBase { private AbstractClient redisClient; private String appName; + private String env = "default"; private String redisHost; private String redisPort; private String redisAuth; @@ -52,7 +52,11 @@ public class RedisAppender extends AppenderBase { public void setAppName(String appName) { this.appName = appName; } - + + public void setEnv(String env) { + this.env = env; + } + public void setRedisHost(String redisHost) { this.redisHost = redisHost; } @@ -111,7 +115,7 @@ public class RedisAppender extends AppenderBase { } protected void send(ILoggingEvent event) { - final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, event); + final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, env, event); if (logMessage instanceof RunLogMessage) { final String message = LogMessageUtil.getLogMessage(logMessage, event); MessageAppenderFactory.pushRundataQueue(message); @@ -133,18 +137,23 @@ public class RedisAppender extends AppenderBase { } if (this.redisClient == null) { - if (this.model.equals("cluster")) { + if ("cluster".equals(this.model)) { this.redisClient = RedisClusterClient.getInstance(this.redisHost, this.redisAuth); - } else if (this.model.equals("sentinel")) { + } else if ("sentinel".equals(this.model)) { this.redisClient = RedisSentinelClient.getInstance(this.redisHost, this.masterName, this.redisAuth, this.redisDb); } else { int port = 6379; String ip = "127.0.0.1"; if (this.redisPort == null) { - String[] hs = redisHost.split(":"); - if (hs.length == 2) { - ip = hs[0]; - port = Integer.valueOf(hs[1]); + // 如果redisHost不包含:号则端口号默认使用6379 + if (redisHost.contains(":")) { + String[] hs = redisHost.split(":"); + if (hs.length == 2) { + ip = hs[0]; + port = Integer.parseInt(hs[1]); + } + } else { + ip = this.redisHost; } } else { ip = this.redisHost; diff --git a/plumelog-logback/src/main/java/com/plumelog/logback/util/LogMessageUtil.java b/plumelog-logback/src/main/java/com/plumelog/logback/util/LogMessageUtil.java index 5b3099a233f587301fcac9c531793531e77671fa..f0a7fbfbd4a49abed4b6df0251eeded2e351a27a 100644 --- a/plumelog-logback/src/main/java/com/plumelog/logback/util/LogMessageUtil.java +++ b/plumelog-logback/src/main/java/com/plumelog/logback/util/LogMessageUtil.java @@ -27,10 +27,10 @@ public class LogMessageUtil { private static String isExpandRunLog(ILoggingEvent logEvent) { String traceId = null; - if (!logEvent.getMDCPropertyMap().isEmpty()) { - traceId = logEvent.getMDCPropertyMap().get(LogMessageConstant.TRACE_ID); - TraceId.logTraceID.set(traceId); - } + if (!logEvent.getMDCPropertyMap().isEmpty()) { + traceId = logEvent.getMDCPropertyMap().get(LogMessageConstant.TRACE_ID); + TraceId.logTraceID.set(traceId); + } return traceId; } @@ -41,6 +41,7 @@ public class LogMessageUtil { * @param iLoggingEvent * @return */ + @SuppressWarnings("unchecked") public static String getLogMessage(BaseLogMessage baseLogMessage, final ILoggingEvent iLoggingEvent) { Map mdc = iLoggingEvent.getMDCPropertyMap(); Map map = GfJsonUtil.parseObject(GfJsonUtil.toJSONString(baseLogMessage), Map.class); @@ -50,16 +51,16 @@ public class LogMessageUtil { return GfJsonUtil.toJSONString(map); } - public static BaseLogMessage getLogMessage(final String appName, final ILoggingEvent iLoggingEvent) { + public static BaseLogMessage getLogMessage(final String appName, final String env, final ILoggingEvent iLoggingEvent) { isExpandRunLog(iLoggingEvent); TraceMessage traceMessage = LogMessageThreadLocal.logMessageThreadLocal.get(); String formattedMessage = getMessage(iLoggingEvent); if (formattedMessage.startsWith(LogMessageConstant.TRACE_PRE)) { return TraceLogMessageFactory.getTraceLogMessage( - traceMessage, appName, iLoggingEvent.getTimeStamp()); + traceMessage, appName, env, iLoggingEvent.getTimeStamp()); } RunLogMessage logMessage = - TraceLogMessageFactory.getLogMessage(appName, formattedMessage, iLoggingEvent.getTimeStamp()); + TraceLogMessageFactory.getLogMessage(appName, env, formattedMessage, iLoggingEvent.getTimeStamp()); logMessage.setClassName(iLoggingEvent.getLoggerName()); StackTraceElement stackTraceElement = iLoggingEvent.getCallerData()[0]; @@ -94,7 +95,7 @@ public class LogMessageUtil { } private static String packageMessage(String message, Object[] args) { - if (message != null && message.indexOf(LogMessageConstant.DELIM_STR) > -1) { + if (message != null && message.contains(LogMessageConstant.DELIM_STR)) { return MessageFormatter.arrayFormat(message, args).getMessage(); } return TraceLogMessageFactory.packageMessage(message, args); diff --git a/plumelog-server/src/main/java/com/plumelog/server/cache/AppNameCache.java b/plumelog-server/src/main/java/com/plumelog/server/cache/AppNameCache.java index 8f4916a6aecc99b2e7456528ea4c6138833cbcc4..01d26f2e95bc5a971c2bbce37bfd31191c77fd95 100644 --- a/plumelog-server/src/main/java/com/plumelog/server/cache/AppNameCache.java +++ b/plumelog-server/src/main/java/com/plumelog/server/cache/AppNameCache.java @@ -1,9 +1,12 @@ package com.plumelog.server.cache; -import java.util.HashSet; +import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; public class AppNameCache { + public static final String APP_NAME_SET = "plumelog:appname:set"; - public static Set appName=new HashSet<>(); + + public static Map> appName = new ConcurrentHashMap<>(); } diff --git a/plumelog-server/src/main/java/com/plumelog/server/client/ElasticLowerClient.java b/plumelog-server/src/main/java/com/plumelog/server/client/ElasticLowerClient.java index bc02a02632224a81c80f0e37333f58e780391009..0f6fb96636c0d51dd9e8ce65059cd05b484db929 100644 --- a/plumelog-server/src/main/java/com/plumelog/server/client/ElasticLowerClient.java +++ b/plumelog-server/src/main/java/com/plumelog/server/client/ElasticLowerClient.java @@ -141,12 +141,13 @@ public class ElasticLowerClient { } public boolean creatIndice(String indice,String type) { - List existIndexList = new ArrayList(); try { Request request = new Request( "PUT", "/" + indice + ""); String properties = "\"properties\":{\"appName\":{\"type\":\"keyword\"}," + + "\"env\":{\"type\":\"keyword\"}," + + "\"appNameWithEnv\":{\"type\":\"keyword\"}," + "\"logLevel\":{\"type\":\"keyword\"}," + "\"serverName\":{\"type\":\"keyword\"}," + "\"traceId\":{\"type\":\"keyword\"}," + @@ -171,12 +172,13 @@ public class ElasticLowerClient { return false; } public boolean creatIndiceTrace(String indice, String type) { - List existIndexList = new ArrayList(); try { Request request = new Request( "PUT", "/" + indice + ""); String properties = "\"properties\":{\"appName\":{\"type\":\"keyword\"}," + + "\"env\":{\"type\":\"keyword\"}" + + "\"appNameWithEnv\":{\"type\":\"keyword\"}" + "\"traceId\":{\"type\":\"keyword\"}" + "}"; String ent = "{\"settings\":{\"number_of_shards\":"+ InitConfig.ES_INDEX_SHARDS+",\"number_of_replicas\":"+InitConfig.ES_INDEX_REPLICAS+",\"refresh_interval\":\""+InitConfig.ES_REFRESH_INTERVAL+"\"}"; diff --git a/plumelog-server/src/main/java/com/plumelog/server/collect/BaseLogCollect.java b/plumelog-server/src/main/java/com/plumelog/server/collect/BaseLogCollect.java index 6226eae3b2df23ec8bcbafb0185881d31bb58c8f..0e6fd76d5d3a93b07eb6922047675214f83f2b48 100644 --- a/plumelog-server/src/main/java/com/plumelog/server/collect/BaseLogCollect.java +++ b/plumelog-server/src/main/java/com/plumelog/server/collect/BaseLogCollect.java @@ -14,6 +14,7 @@ import org.springframework.context.ApplicationEventPublisher; import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor; @@ -75,7 +76,7 @@ public class BaseLogCollect { for(int i=0;i new HashSet<>()).add(runLogMessage.getEnv()); if (runLogMessage.getLogLevel().toUpperCase().equals("ERROR")) { errorLogs.add(runLogMessage); } diff --git a/plumelog-server/src/main/java/com/plumelog/server/controller/MainController.java b/plumelog-server/src/main/java/com/plumelog/server/controller/MainController.java index ed586045d830acc9b5e2f5fc8d83010206827ca2..c9157cfb982052f4a8ef87c121efa838b3b11f02 100644 --- a/plumelog-server/src/main/java/com/plumelog/server/controller/MainController.java +++ b/plumelog-server/src/main/java/com/plumelog/server/controller/MainController.java @@ -269,6 +269,7 @@ public class MainController { WarningRuleDto warningRuleDto=new WarningRuleDto(); warningRuleDto.setId(mapKey); warningRuleDto.setAppName(warningRule.getAppName()); + warningRuleDto.setEnv(warningRule.getEnv()); warningRuleDto.setAppCategory(warningRule.getAppCategory()); warningRuleDto.setClassName(warningRule.getClassName()); warningRuleDto.setReceiver(warningRule.getReceiver()); diff --git a/plumelog-server/src/main/java/com/plumelog/server/monitor/PlumeLogMonitorListener.java b/plumelog-server/src/main/java/com/plumelog/server/monitor/PlumeLogMonitorListener.java index 73291d363614fbdd5e0f49680bf0037c8b15b265..a12dd9f9000b4cbde273fb0d20af09ae3f77671b 100644 --- a/plumelog-server/src/main/java/com/plumelog/server/monitor/PlumeLogMonitorListener.java +++ b/plumelog-server/src/main/java/com/plumelog/server/monitor/PlumeLogMonitorListener.java @@ -75,7 +75,7 @@ public class PlumeLogMonitorListener implements ApplicationListener logMessages) { logMessages.forEach(runLogMessage -> { - List monitorRuleConfig = plumeLogMonitorRuleConfig.getMonitorRuleConfig(runLogMessage.getAppName()); + List monitorRuleConfig = plumeLogMonitorRuleConfig.getMonitorRuleConfig(runLogMessage.getAppName(), runLogMessage.getEnv()); if (monitorRuleConfig != null) { //运行规则 enforcementRules(monitorRuleConfig, runLogMessage); @@ -95,6 +95,7 @@ public class PlumeLogMonitorListener implements ApplicationListener getMonitorRuleConfig(String appName) { + public List getMonitorRuleConfig(String appName, String env) { if (configMap.isEmpty()) { initMonitorRuleConfig(); } - return configMap.get(appName); + return configMap.get(getKey(appName, env)); } public synchronized void initMonitorRuleConfig() { @@ -62,14 +60,15 @@ public class PlumeLogMonitorRuleConfig { if (warningRule.getStatus() == 0) { return; } - if (backConfigMap.containsKey(warningRule.getAppName())) { - List warningRules = backConfigMap.get(warningRule.getAppName()); + String key = getKey(warningRule.getAppName(), warningRule.getEnv()); + if (backConfigMap.containsKey(key)) { + List warningRules = backConfigMap.get(key); warningRules.add(warningRule); - backConfigMap.put(warningRule.getAppName(), warningRules); + backConfigMap.put(key, warningRules); } else { List lists = new ArrayList<>(); lists.add(warningRule); - backConfigMap.put(warningRule.getAppName(), lists); + backConfigMap.put(key, lists); } } @@ -84,4 +83,8 @@ public class PlumeLogMonitorRuleConfig { } } + + private static String getKey(String appName, String env) { + return appName + ":" + env; + } } diff --git a/plumelog-server/src/main/java/com/plumelog/server/monitor/PlumeLogMonitorTextMessage.java b/plumelog-server/src/main/java/com/plumelog/server/monitor/PlumeLogMonitorTextMessage.java index 6c3fee13fab7149b156898f9d89c3214c0622be6..b915f31fcee092fb5bc39b33e820e68dbcb0dc8b 100644 --- a/plumelog-server/src/main/java/com/plumelog/server/monitor/PlumeLogMonitorTextMessage.java +++ b/plumelog-server/src/main/java/com/plumelog/server/monitor/PlumeLogMonitorTextMessage.java @@ -52,6 +52,8 @@ public class PlumeLogMonitorTextMessage { private Long time; private String appName; + + private String env; private String className; @@ -59,8 +61,9 @@ public class PlumeLogMonitorTextMessage { private String errorContent; - public Builder(String appName) { + public Builder(String appName, String env) { this.appName = appName; + this.env = env; } public Builder className(String className) { @@ -98,7 +101,9 @@ public class PlumeLogMonitorTextMessage { public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("#### 报警通知:").append(" \n> ").append("应用名 【").append(appName).append("】\n\n>"); + builder.append("#### 报警通知:").append(" \n> ") + .append("应用名 【").append(appName).append("】\n\n>") + .append("应用环境【").append(env).append("】\n\n> "); if (!StringUtils.isEmpty(className)) { builder.append("类路径【").append(className).append("】 \n\n> "); } diff --git a/plumelog-ui-front/src/assets/less/filters.less b/plumelog-ui-front/src/assets/less/filters.less index 7ea5a8dc1bcc749646d05a3200fa07eefd82192c..d557554aa8ebbe0a9f1f40e253e1372f6b7e706f 100644 --- a/plumelog-ui-front/src/assets/less/filters.less +++ b/plumelog-ui-front/src/assets/less/filters.less @@ -7,16 +7,16 @@ text-align:left; padding-bottom:10px; &.key{ - padding-left:20px; + padding-left:15px; text-align:right; font-weight:700; - width:150px; - padding-right:30px; + width:110px; + padding-right:20px; } .txt{ &.txtAppName { - width:220px; + width:200px; } } .ivu-checkbox-default diff --git a/plumelog-ui-front/src/components/table-expand.vue b/plumelog-ui-front/src/components/table-expand.vue index a83daa686b5751b3b8713299533af0479a6dd867..e5cf8680d3aac7cf1de66fcaa6bb46fd489c801a 100644 --- a/plumelog-ui-front/src/components/table-expand.vue +++ b/plumelog-ui-front/src/components/table-expand.vue @@ -46,10 +46,10 @@ export default { }, methods: { hightLightCode(code, isHighlight) { - code = code.replace(/\\n\\t/g, "\n").replace(/\\n\\tat/g, "\n").replace(/\\n/g, '\n'); + code = code.replace(/\\n/g, '\n').replace(/\\t/g, '\t'); let rows = []; if (code.indexOf('java.') > -1) { - let content = '
' + Prism.highlight(code.replace(/\n/g, '
'), Prism.languages.stackjava, 'stackjava').replace(/</g, '<').replace(/>/g, '>') + "
" + let content = '
' + Prism.highlight(code.replace(/\n/g, '
').replace(/\t/g, '  '), Prism.languages.stackjava, 'stackjava').replace(/</g, '<').replace(/>/g, '>') + "
" rows.push({isH: true, content}) return rows; }else if (isHtml.test(code)) { @@ -78,7 +78,7 @@ export default { rows.push({isH: false, content}) return rows; } else { - let content = '
' + code.replace(/\n/g, '
').replace(/\tat/g, '') + "
"; + let content = '
' + code.replace(/\n/g, '
').replace(/\t/g, '  ') + "
"; rows.push({isH: true, content}) return rows; } diff --git a/plumelog-ui-front/src/views/Home.vue b/plumelog-ui-front/src/views/Home.vue index 13867f6928d36a9d9cb9abfa3e731979fa3fb1fc..2279a247e235cc030c6ee9ba7a9a8055b36b9379 100644 --- a/plumelog-ui-front/src/views/Home.vue +++ b/plumelog-ui-front/src/views/Home.vue @@ -12,67 +12,152 @@ - - - - - - - - - - - - - - - -
应用名称 - - - 排除 -
日志等级 - -
服务器名称 - -
- - - - - - - - - - - - - -
类名 - -
追踪码 - -
日期和时间 - -
+
+ + + + + + + + + + + +
应用名称 + + + 排除 + 应用环境 + + + 日志等级 + +
+ + + + + + + + + + + + +
追踪码 + +
类名 + +
+ + + + + + + + + + + +
服务器名称 + +
日期和时间 + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
扩展字段 + + + +
+ + + {{ tag.label }}:{{ tag.tag }} + +
内容 + + 切换为条件模式 +
条件 + + + + 切换为内容模式 +
+ + + {{ tag.tag }} + +
+ + +
+
@@ -82,72 +167,6 @@
- -
- - - - - - - - - - - - - - - - - - - - - - - - - -
扩展字段 - - - -
- - - {{ tag.label }}:{{ tag.tag }} - -
内容 - - 切换为条件模式 -
条件 - - - - 切换为内容模式 -
- - - {{ tag.tag }} - -
- - -
@@ -180,6 +199,10 @@ {{ row.appName }} +