All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wiki.hadoop.log4j.bean.LogBean Maven / Gradle / Ivy

package wiki.hadoop.log4j.bean;

import lombok.Builder;

import java.io.*;
import java.util.Objects;

/**
 * @author Jast
 * @description
 * @date 2023-10-30 11:33
 */
@Builder
public class LogBean {

  /** 系统名称 */
  private String systemName;
  /** 程序功能 */
  private String actionOrFunction;
  /** 业务类型 */
  private String businessType;
  /** 处理时间 */
  private Long processTime;
  /** 状态 */
  private Status status;
  /** 操作时间戳毫秒 */
  private Long currentTimeMillisecond = System.currentTimeMillis();
  /** 异常信息 */
  private String exceptionInfo;
  /** 数据全链路ID */
  private String fullLinkId;
  /** 子数据全链路ID */
  private String subFullLinkId;
  /** 日志内容 */
  private String content;
  /** 所在机器Host */
  private String host;
  /** 所在机器IP */
  private String ip;

  /***
   * 转为JSON格式发送日志,未引用第三方依赖,直接拼接为json
   * @name toString
   * @date 2023/10/30 上午11:56
   * @return java.lang.String
   *
   * @author Jast
   */
  @Override
  public String toString() {
    if (Objects.isNull(content) || Objects.isNull(systemName) || Objects.isNull(actionOrFunction)) {
      throw new RuntimeException("请检查日志必填字段是否填充");
    }
    StringBuilder jsonStringBuilder = new StringBuilder("{");
    if (systemName != null) {
      jsonStringBuilder.append("\"systemName\":\"").append(systemName).append("\",");
    }
    if (actionOrFunction != null) {
      jsonStringBuilder.append("\"actionOrFunction\":\"").append(actionOrFunction).append("\",");
    }
    if (businessType != null) {
      jsonStringBuilder.append("\"businessType\":\"").append(businessType).append("\",");
    }
    if (processTime != null) {
      jsonStringBuilder.append("\"processTime\":").append(processTime).append(",");
    }
    if (status != null) {
      jsonStringBuilder.append("\"status\":").append(status.type).append(",");
    }
    if (currentTimeMillisecond != null) {
      jsonStringBuilder
          .append("\"currentTimeMillisecond\":")
          .append(currentTimeMillisecond)
          .append(",");
    }
    if (exceptionInfo != null) {
      jsonStringBuilder.append("\"exceptionInfo\":\"").append(exceptionInfo).append("\",");
    }
    if (fullLinkId != null) {
      jsonStringBuilder.append("\"fullLinkId\":\"").append(fullLinkId).append("\",");
    }
    if (subFullLinkId != null) {
      jsonStringBuilder.append("\"subFullLinkId\":\"").append(subFullLinkId).append("\",");
    }
    if (content != null) {
      jsonStringBuilder.append("\"content\":\"").append(content).append("\",");
    }
    if (host != null) {
      jsonStringBuilder.append("\"host\":\"").append(host).append("\",");
    }
    if (ip != null) {
      jsonStringBuilder.append("\"ip\":\"").append(ip).append("\"");
    }

    if (jsonStringBuilder.length() > 1) {
      jsonStringBuilder.setLength(jsonStringBuilder.length() - 1);
    }

    jsonStringBuilder.append("}");

    return jsonStringBuilder.toString();
  }

  public String getSystemName() {
    return systemName;
  }

  public void setSystemName(String systemName) {
    this.systemName = systemName;
  }

  public String getActionOrFunction() {
    return actionOrFunction;
  }

  public void setActionOrFunction(String actionOrFunction) {
    this.actionOrFunction = actionOrFunction;
  }

  public String getBusinessType() {
    return businessType;
  }

  public void setBusinessType(String businessType) {
    this.businessType = businessType;
  }

  public Long getProcessTime() {
    return processTime;
  }

  public void setProcessTime(Long processTime) {
    this.processTime = processTime;
  }

  public Status getStatus() {
    return status;
  }

  public void setStatus(Status status) {
    this.status = status;
  }

  public Long getCurrentTimeMillisecond() {
    return currentTimeMillisecond;
  }

  public void setCurrentTimeMillisecond(Long currentTimeMillisecond) {
    this.currentTimeMillisecond = currentTimeMillisecond;
  }

  public String getExceptionInfo() {
    return exceptionInfo;
  }

  public void setExceptionInfo(String exceptionInfo) {
    this.exceptionInfo = exceptionInfo;
  }

  public String getFullLinkId() {
    return fullLinkId;
  }

  public void setFullLinkId(String fullLinkId) {
    this.fullLinkId = fullLinkId;
  }

  public String getSubFullLinkId() {
    return subFullLinkId;
  }

  public void setSubFullLinkId(String subFullLinkId) {
    this.subFullLinkId = subFullLinkId;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public String getHost() {
    return host;
  }

  public void setHost(String host) {
    this.host = host;
  }

  public String getIp() {
    return ip;
  }

  public void setIp(String ip) {
    this.ip = ip;
  }

  /**
   *
   * @name getExceptionStr
   * @date 2023/10/30 下午2:09
   * @return java.lang.String 
   * @param e
   * @author Jast
  */
  public static String exceptionToStr(Exception e) {
    try {
      ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
      e.printStackTrace(new PrintStream(arrayOutputStream));
      BufferedReader fr =
              new BufferedReader(
                      new InputStreamReader(
                              new ByteArrayInputStream(arrayOutputStream.toByteArray())));
      String str;
      StringBuilder exceptionStr = new StringBuilder();
      while ((str = fr.readLine()) != null) {
        exceptionStr.append(str + "\n");
      }
      fr.close();
      return exceptionStr.toString();
    } catch (Exception f) {
      f.printStackTrace();
      return null;
    }
  }
  public enum Status {

    SUCCESS(0),
    WARN(-1),
    FAIL(-2);

    private final int type;

    Status(int type) {
      this.type = type;
    }

    public static Status getStatus(int type) {
      switch (type) {
        case 0:
          return SUCCESS;
        case -1:
          return WARN;
        case -2:
          return FAIL;
        default:
          throw new RuntimeException("Unsupported status " + type);
      }
    }

    public int getType() {
      return type;
    }
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy