package com.gxzc.zen.logging.util import com.gxzc.zen.common.util.PlatformUtil import com.gxzc.zen.logging.model.LogAuth import com.gxzc.zen.logging.model.LogBiz import com.gxzc.zen.logging.model.LogDB import com.gxzc.zen.logging.model.LogRequest import com.gxzc.zen.msg.mq.MQProducerUtil import com.gxzc.zen.msg.mq.constants.MQConstants import com.gxzc.zen.umps.util.SSOUtil import com.maihaoche.starter.mq.base.MessageBuilder import org.apache.rocketmq.client.producer.SendCallback import org.apache.rocketmq.client.producer.SendResult import org.slf4j.LoggerFactory import java.util.* /** * 基于消息队列MQ的日志工具类 * @author NorthLan * @date 2018/8/23 * @url https://noahlan.com */ object MQLogUtil { private val logger = LoggerFactory.getLogger(MQLogUtil::class.java) private val mqProducer = MQProducerUtil.producer!! /** * 发送登陆日志消息 */ fun logAuth(logAuth: LogAuth) { mqProducer.asyncSend(MessageBuilder.of(logAuth).topic(MQConstants.TOPIC_LOGS).tag(MQConstants.TAGS_LOGS_AUTH).build(), object : SendCallback { override fun onException(e: Throwable) { logger.error("Send logAuth error, cause ", e) } override fun onSuccess(sendResult: SendResult) { logger.debug("Send logAuth success! {0}", sendResult.sendStatus) } }) } /** * 发送请求日志消息 */ fun logRequest(logRequest: LogRequest) { logRequest.apply { this.platformId = PlatformUtil.getPlatformId() this.operatorAccount = SSOUtil.getCurAccount() this.operatorName = SSOUtil.getCurUserName() } mqProducer.asyncSend(MessageBuilder.of(logRequest).topic(MQConstants.TOPIC_LOGS).tag(MQConstants.TAGS_LOGS_REQUEST).build(), object : SendCallback { override fun onException(e: Throwable) { logger.error("Send logRequest error, cause ", e) } override fun onSuccess(sendResult: SendResult) { logger.debug("Send logRequest success! " + sendResult.sendStatus) } }) } /** * 发送DB日志消息 */ fun logDB(type: Int, sql: String?, tableId: String?, tableName: String?, remark: String?, startTime: Date, endTime: Date = Date()) { val logDB = LogDB().apply { this.platformId = PlatformUtil.getPlatformId() this.operatorStartTime = startTime this.operatorEndTime = endTime this.operatorAccount = SSOUtil.getCurAccount() this.operatorName = SSOUtil.getCurUserName() this.type = type this.sql = sql this.tableId = tableId this.tableName = tableName this.remark = remark } mqProducer.asyncSend(MessageBuilder.of(logDB).topic(MQConstants.TOPIC_LOGS).tag(MQConstants.TAGS_LOGS_DB).build(), object : SendCallback { override fun onException(e: Throwable) { logger.error("Send logDB error, cause ", e) } override fun onSuccess(sendResult: SendResult) { logger.debug("Send logDB success! {0}", sendResult.sendStatus) } }) } /** * 发送业务日志消息 */ fun logBiz(bizCode: String, bizContent: String, remark: String?, startTime: Date, endTime: Date = Date()) { MQLogUtil.logBizInner(LogBiz().apply { this.bizCode = bizCode this.bizContent = bizContent this.platformId = PlatformUtil.getPlatformId() this.operatorAccount = SSOUtil.getCurAccount() this.operatorName = SSOUtil.getCurUserName() this.operatorStartTime = startTime this.operatorEndTime = endTime this.remark = remark }) } private fun logBizInner(logBiz: LogBiz) { mqProducer.asyncSend(MessageBuilder.of(logBiz).topic(MQConstants.TOPIC_LOGS).tag(MQConstants.TAGS_LOGS_BIZ).build(), object : SendCallback { override fun onException(e: Throwable) { logger.error("Send logBiz error, cause ", e) } override fun onSuccess(sendResult: SendResult) { logger.debug("Send logBiz success! {0}", sendResult.sendStatus) } }) } }