Эх сурвалжийг харах

添加分布式日志记录

tuonina 6 жил өмнө
parent
commit
2bb1a1a7ed

+ 4 - 0
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/RabbitConfiguration.java

@@ -27,5 +27,9 @@ public class RabbitConfiguration {
         return new Queue(QueueName.Framework);
     }
 
+    @Bean
+    public Queue logQueue(){
+        return  new Queue(QueueName.LOG);
+    }
 
 }

+ 1 - 0
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/constant/QueueName.java

@@ -8,4 +8,5 @@ package cn.gygxzc.tina.cloud.bus.constant;
 public interface QueueName {
     public final String Messager = "Messager";
     String Framework = "Framework";
+    String LOG = "LOG";
 }

+ 3 - 0
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/framweork/receiver/FrameworkReceiver.java

@@ -1,6 +1,7 @@
 package cn.gygxzc.tina.cloud.bus.framweork.receiver;
 
 import cn.gygxzc.tina.cloud.bus.constant.QueueName;
+import cn.gygxzc.tina.cloud.bus.message.LogMessage;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.amqp.rabbit.annotation.RabbitHandler;
@@ -26,4 +27,6 @@ public class FrameworkReceiver {
     public void process(FrameworkReceiver receiver) {
         logger.info("receive :{}", receiver);
     }
+
+
 }

+ 24 - 0
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/framweork/receiver/LoggerReceiver.java

@@ -0,0 +1,24 @@
+package cn.gygxzc.tina.cloud.bus.framweork.receiver;
+
+import cn.gygxzc.tina.cloud.bus.constant.QueueName;
+import cn.gygxzc.tina.cloud.bus.message.LogMessage;
+import cn.gygxzc.tina.cloud.bus.utils.GsonUtils;
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.stereotype.Component;
+
+/**
+ * Created by niantuo on 2018/11/5.
+ * 日志收集器.
+ *
+ */
+//@Component
+//@RabbitListener(queues = QueueName.LOG)
+public class LoggerReceiver {
+
+//    @RabbitHandler
+//    public void mqLog(String message) {
+//        LogMessage logMessage = GsonUtils.fromJson(LogMessage.class,message);
+//    }
+
+}

+ 128 - 0
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/message/LogMessage.java

@@ -0,0 +1,128 @@
+package cn.gygxzc.tina.cloud.bus.message;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import cn.gygxzc.tina.cloud.bus.utils.GsonUtils;
+
+/**
+ * Created by niantuo on 2018/11/5.
+ * 分布式日志记录
+ */
+
+public class LogMessage {
+
+    private String sysName;
+    private String threadName;
+    private String level;
+    private String message;
+    private String arguments;
+    private String formattedMessage;
+    private String loggerName;
+    private String callerData;
+    private String marker;
+    private String propertyMap;
+    private Long timeStamp;
+
+    public LogMessage(ILoggingEvent event,String name) {
+        this.sysName = name;
+        this.threadName = event.getThreadName();
+        level = event.getLevel().levelStr;
+        message = event.getMessage();
+        arguments = GsonUtils.toJson(event.getArgumentArray());
+        formattedMessage = event.getFormattedMessage();
+        loggerName = event.getLoggerName();
+        callerData = GsonUtils.toJson(event.getCallerData());
+        marker = GsonUtils.toJson(event.getMarker());
+        propertyMap = GsonUtils.toJson(event.getMDCPropertyMap());
+        timeStamp = event.getTimeStamp();
+    }
+
+    public void setSysName(String sysName) {
+        this.sysName = sysName;
+    }
+
+    public String getSysName() {
+        return sysName;
+    }
+
+    public String getThreadName() {
+        return threadName;
+    }
+
+    public void setThreadName(String threadName) {
+        this.threadName = threadName;
+    }
+
+    public String getLevel() {
+        return level;
+    }
+
+    public void setLevel(String level) {
+        this.level = level;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    public String getArguments() {
+        return arguments;
+    }
+
+    public void setArguments(String arguments) {
+        this.arguments = arguments;
+    }
+
+    public String getFormattedMessage() {
+        return formattedMessage;
+    }
+
+    public void setFormattedMessage(String formattedMessage) {
+        this.formattedMessage = formattedMessage;
+    }
+
+    public String getLoggerName() {
+        return loggerName;
+    }
+
+    public void setLoggerName(String loggerName) {
+        this.loggerName = loggerName;
+    }
+
+    public String getCallerData() {
+        return callerData;
+    }
+
+    public void setCallerData(String callerData) {
+        this.callerData = callerData;
+    }
+
+    public String getMarker() {
+        return marker;
+    }
+
+    public void setMarker(String marker) {
+        this.marker = marker;
+    }
+
+    public String getPropertyMap() {
+        return propertyMap;
+    }
+
+    public void setPropertyMap(String propertyMap) {
+        this.propertyMap = propertyMap;
+    }
+
+    public Long getTimeStamp() {
+        return timeStamp;
+    }
+
+    public void setTimeStamp(Long timeStamp) {
+        this.timeStamp = timeStamp;
+    }
+
+
+}

+ 3 - 0
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/messager/sender/IMessagerSender.java

@@ -1,5 +1,6 @@
 package cn.gygxzc.tina.cloud.bus.messager.sender;
 
+import cn.gygxzc.tina.cloud.bus.message.LogMessage;
 import cn.gygxzc.tina.cloud.bus.messager.bean.MessageBean;
 
 /**
@@ -10,4 +11,6 @@ import cn.gygxzc.tina.cloud.bus.messager.bean.MessageBean;
 public interface IMessagerSender {
 
     public void send(MessageBean message);
+
+    void send(LogMessage message);
 }

+ 7 - 0
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/messager/sender/impl/MessagerSender.java

@@ -1,8 +1,10 @@
 package cn.gygxzc.tina.cloud.bus.messager.sender.impl;
 
 import cn.gygxzc.tina.cloud.bus.constant.QueueName;
+import cn.gygxzc.tina.cloud.bus.message.LogMessage;
 import cn.gygxzc.tina.cloud.bus.messager.bean.MessageBean;
 import cn.gygxzc.tina.cloud.bus.messager.sender.IMessagerSender;
+import cn.gygxzc.tina.cloud.bus.utils.GsonUtils;
 import org.springframework.amqp.core.AmqpTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -30,4 +32,9 @@ public class MessagerSender implements IMessagerSender {
     public void send(MessageBean message) {
         amqpTemplate.convertAndSend(QueueName.Messager, message);
     }
+
+    @Override
+    public void send(LogMessage message) {
+        amqpTemplate.convertAndSend(QueueName.LOG, GsonUtils.toJson(message));
+    }
 }

+ 26 - 0
cloud-bus/src/main/java/cn/gygxzc/tina/cloud/bus/utils/GsonUtils.java

@@ -0,0 +1,26 @@
+package cn.gygxzc.tina.cloud.bus.utils;
+
+import com.google.gson.Gson;
+
+/**
+ * Created by niantuo on 2018/11/5.
+ */
+
+public class GsonUtils {
+    private final static Gson mGson = new Gson();
+
+    public static String toJson(Object obj) {
+        return mGson.toJson(obj);
+    }
+
+    /**
+     * 实际上如果是数组,这样子是解析不出来的吧。
+     * @param clz 要返回的类型
+     * @param json 原始json数据
+     * @param <T> 泛型
+     * @return
+     */
+    public static <T> T fromJson(Class<T> clz, String json) {
+        return mGson.fromJson(json, clz);
+    }
+}

+ 1 - 0
zen-core/build.gradle

@@ -20,4 +20,5 @@ dependencies {
             'io.jsonwebtoken:jjwt-jackson:0.10.5'
     compile("cn.gygxzc.cloud:jwt-session:$jwtVersion")
     compile("cn.gygxzc.cloud:tina-auth:$authVersion")
+    compile project(':cloud-bus')
 }

+ 35 - 0
zen-core/src/main/java/cn/gygxzc/envir/core/utils/ApplicationUtils.java

@@ -0,0 +1,35 @@
+package cn.gygxzc.envir.core.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+/**
+ * Created by niantuo on 2018/11/5.
+ * 获取bean类
+ */
+@Component
+public class ApplicationUtils implements ApplicationContextAware {
+
+    private static ApplicationContext mContext;
+    private final static Logger logger = LoggerFactory.getLogger(ApplicationUtils.class);
+
+
+    public static <T> T getBean(Class<T> clz) {
+        if (mContext == null) return null;
+        try {
+            return mContext.getBean(clz);
+        } catch (Exception e) {
+            logger.error("getBean", e);
+        }
+        return null;
+    }
+
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        mContext = applicationContext;
+    }
+}

+ 22 - 0
zen-web/src/main/java/cn/gygxzc/envir/api/test/controller/TestMQLogController.java

@@ -0,0 +1,22 @@
+package cn.gygxzc.envir.api.test.controller;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * Created by niantuo on 2018/11/5.
+ */
+@RestController
+@RequestMapping("/test/mq/log")
+public class TestMQLogController {
+
+    private Logger logger = LoggerFactory.getLogger(TestMQLogController.class);
+
+    @PostMapping
+    public void send(String content) {
+        logger.info(content);
+    }
+}

+ 0 - 1
zen-web/src/main/resources/bootstrap.yml

@@ -17,7 +17,6 @@ spring:
       allow-override: true
 
 
-
 ---
 spring:
   profiles: dev

+ 24 - 9
zen-web/src/main/resources/logback-spring.xml

@@ -1,5 +1,13 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <configuration scan="true">
+
+    <springProperty  scope="context" name="appName" source="spring.application.name"/>
+
+    <!--<include resource="org/springframework/boot/logging/logback/base.xml"/>-->
+    <contextName>${appName}</contextName>
+    <property name="LOG_PATH" value=".logs"/>
+    <!--设置系统日志目录-->
+    <property name="APPDIR" value="${appName}" />
     <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
     <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, -->
     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
@@ -10,11 +18,6 @@
             <charset>UTF-8</charset>
         </encoder>
     </appender>
-    <!--<include resource="org/springframework/boot/logging/logback/base.xml"/>-->
-    <contextName>Envir-Framework</contextName>
-    <property name="LOG_PATH" value=".logs"/>
-    <!--设置系统日志目录-->
-    <property name="APPDIR" value="app"/>
     <!--
           说明:
           1、日志级别及文件
@@ -122,6 +125,16 @@
             <onMismatch>DENY</onMismatch>
         </filter>
     </appender>
+
+    <appender name="MQ" class="cn.gygxzc.envir.core.log.MQAppender">
+        <sysName>${appName}</sysName>
+    </appender>
+    <!--实现分布式收集日志的appender-->
+    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
+        <appender-ref ref ="MQ"/>
+    </appender>
+
+
     <!--日志异步到数据库  -->
     <!--<appender name="DBAPPENDER" class="ch.qos.logback.classic.db.DBAppender">-->
     <!--<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">-->
@@ -151,7 +164,8 @@
             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern>
         </encoder>
     </appender>
-    <logger name="com.gxzc" level="DEBUG"/>
+    <!---->
+    <logger name="com.gxzc" level="DEBUG" additivity="true"/>
     <logger name="cn.gygxzc" level="DEBUG"/>
     <logger name="org.springframework.data.mybatis" level="DEBUG"/>
     <logger name="org.springframework.aop.aspectj" level="ERROR"/>
@@ -192,25 +206,26 @@
     <!--<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">-->
     <!--<resetJUL>true</resetJUL>-->
     <!--</contextListener>-->
-
     <springProfile name="prod">
         <root level="INFO">
-            <!--<appender-ref ref="STDOUT"/>-->
+            <appender-ref ref="STDOUT"/>
             <appender-ref ref="FILEERROR"/>
             <appender-ref ref="FILEWARN"/>
             <appender-ref ref="FILEINFO"/>
             <appender-ref ref="DBAPPENDER"/>
+            <appender-ref ref="ASYNC"/>
         </root>
     </springProfile>
 
 
     <springProfile name="dev">
         <root level="INFO">
+            <appender-ref ref="STDOUT"/>
             <appender-ref ref="CONSOLE"/>
             <appender-ref ref="FILEERROR"/>
             <appender-ref ref="FILEWARN"/>
             <appender-ref ref="FILEINFO"/>
-            <!--<appender-ref ref="DBAPPENDER"/>-->
+            <appender-ref ref="ASYNC"/>
         </root>
     </springProfile>