Search in sources :

Example 1 with Log

use of com.github.liuweijw.core.beans.system.Log in project fw-cloud-framework by liuweijw.

the class LogServiceImpl method send.

@Override
public void send(RequestContext requestContext) {
    HttpServletRequest request = requestContext.getRequest();
    String requestUri = request.getRequestURI();
    String method = request.getMethod();
    Log syslog = new Log();
    syslog.setType(CommonConstant.STATUS_NORMAL);
    syslog.setRemoteAddr(HttpUtil.getClientIP(request));
    syslog.setRequestUri(URLUtil.getPath(requestUri));
    syslog.setMethod(method);
    syslog.setUserAgent(request.getHeader("user-agent"));
    syslog.setParams(HttpUtil.toParams(request.getParameterMap()));
    Long startTime = (Long) requestContext.get("startTime");
    syslog.setTime(System.currentTimeMillis() - startTime);
    Date currentDate = new Date();
    syslog.setCreateTime(currentDate);
    syslog.setUpdateTime(currentDate);
    if (requestContext.get(CommonConstant.SERVICE_ID) != null) {
        syslog.setServiceId(requestContext.get(CommonConstant.SERVICE_ID).toString());
    }
    // 正常发送服务异常解析
    if (requestContext.getResponseStatusCode() != HttpStatus.SC_OK && null != requestContext.getResponseDataStream()) {
        InputStream responseStream = requestContext.getResponseDataStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream inputStream = null;
        InputStream responseDataStream = null;
        byte[] buffer = IoUtil.readBytes(responseStream);
        try {
            baos.write(buffer);
            baos.flush();
            inputStream = new ByteArrayInputStream(baos.toByteArray());
            responseDataStream = new ByteArrayInputStream(baos.toByteArray());
            String response = IoUtil.read(inputStream, CommonConstant.UTF8);
            syslog.setType(CommonConstant.STATUS_LOCK);
            syslog.setException(response);
            requestContext.setResponseDataStream(responseDataStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IoUtil.close(responseDataStream);
            IoUtil.close(baos);
            IoUtil.close(responseStream);
        }
    }
    // 网关内部异常
    Throwable throwable = requestContext.getThrowable();
    if (throwable != null) {
        syslog.setException(throwable.getMessage());
    }
    AuthLog authLog = new AuthLog();
    // 保存发往MQ(只保存授权)
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null && StringHelper.isNotBlank(authentication.getName())) {
        syslog.setCreateBy(authentication.getName());
        authLog.setLog(syslog);
        rabbitTemplate.convertAndSend(MqQueueConstant.LOG_QUEUE, authLog);
    }
}
Also used : AuthLog(com.github.liuweijw.core.beans.system.AuthLog) Log(com.github.liuweijw.core.beans.system.Log) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Date(java.util.Date) HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthLog(com.github.liuweijw.core.beans.system.AuthLog) ByteArrayInputStream(java.io.ByteArrayInputStream) Authentication(org.springframework.security.core.Authentication)

Example 2 with Log

use of com.github.liuweijw.core.beans.system.Log in project fw-cloud-framework by liuweijw.

the class LogRabbitListener method receive.

@RabbitHandler
public void receive(AuthLog authLog) {
    Log sysLog = authLog.getLog();
    MDC.put(CommonConstant.KEY_USER, authLog.getLog().getCreateBy());
    Date currentDate = new Date();
    if (null == sysLog.getCreateTime())
        sysLog.setCreateTime(currentDate);
    if (null == sysLog.getUpdateTime())
        sysLog.setUpdateTime(currentDate);
    LogInfo logInfo = new LogInfo();
    BeanUtils.copyProperties(sysLog, logInfo);
    logInfoService.saveOrUpdate(logInfo);
    MDC.remove(CommonConstant.KEY_USER);
}
Also used : LogInfo(com.github.liuweijw.business.admin.domain.LogInfo) AuthLog(com.github.liuweijw.core.beans.system.AuthLog) Log(com.github.liuweijw.core.beans.system.Log) Date(java.util.Date) RabbitHandler(org.springframework.amqp.rabbit.annotation.RabbitHandler)

Aggregations

AuthLog (com.github.liuweijw.core.beans.system.AuthLog)2 Log (com.github.liuweijw.core.beans.system.Log)2 Date (java.util.Date)2 LogInfo (com.github.liuweijw.business.admin.domain.LogInfo)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 RabbitHandler (org.springframework.amqp.rabbit.annotation.RabbitHandler)1 Authentication (org.springframework.security.core.Authentication)1