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

com.heavenark.infrastructure.log.ArkLogLoader Maven / Gradle / Ivy

The newest version!
package com.heavenark.infrastructure.log;

import com.heavenark.infrastructure.base.ArkBaseLoader;
import com.heavenark.infrastructure.base.model.ArkLogConfModel;
import com.heavenark.infrastructure.base.model.ArkPublicConfModel;
import com.heavenark.infrastructure.log.hook.ArkLogHookReg;
import com.heavenark.infrastructure.log.model.ArkLogType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Ark-Log config loader, load config from ark-base and do some pre-prepare
 *
 * @author 冰糕Luminous BGLuminous Luminous
 * @since 2.0.0
 */
public class ArkLogLoader {
  /** Logger Prefix */
  public static final String PROJECT_NAME = "AIL";
  /** Logger Prefix */
  public static final String PROJECT_NAME_SLF = "AIL4J";

  /** Ark public config */
  private final ArkPublicConfModel publicConfig = ArkBaseLoader.getPublicConf();
  /** Ark-Log global config */
  private final ArkLogConfModel config;
  /** log process direct skip list */
  private final List skipList = new ArrayList<>();

  /** Constructor load global config from Ark-Base and init logFormat, skip list */
  private ArkLogLoader() {
    // start initialization config, if is null return a new config model
    ArkLogConfModel confModel = ArkBaseLoader.getArkLogConf();
    this.config = confModel == null ? new ArkLogConfModel() : confModel;
    // prepare skip list
    preGenerateSkipList();
    // register shutdown hook
    ArkLogHookReg.regShutdownHook();
  }

  /**
   * Get ArkLogConfigLoader instance
   *
   * @return ArkLogConfig instance
   */
  private static ArkLogLoader instance() {
    return Holder.INSTANCE;
  }

  /**
   * Get charset
   *
   * @return charset
   */
  public static String getCharset() {
    String charset = instance().publicConfig.getCharset();
    return charset == null ? "UTF-8" : charset;
  }

  /**
   * Get Docker Env
   *
   * @return --
   */
  public static boolean isDockerEnv() {
    Boolean dockerEnv = instance().publicConfig.getDockerEnv();
    return dockerEnv != null && dockerEnv;
  }

  /**
   * Get Container Id
   *
   * @return --
   */
  public static String getContainerId() {
    return instance().publicConfig.getContainerId();
  }

  /**
   * Get log date format, if not set use default { yyyy-MM-dd HH:mm:ss.S }
   *
   * @return date format
   */
  public static String getDateFormat() {
    String dateFormat = instance().config.getDateFormat();
    return (dateFormat == null || dateFormat.length() == 0) ? "yyyy-MM-dd HH:mm:ss.S" : dateFormat;
  }

  /**
   * Get path config
   *
   * @return path
   */
  public static String getPath() {
    return instance().config.getPath();
  }

  /**
   * Get config file prefix
   *
   * @return file prefix
   */
  public static String getPrefix() {
    String prefix = instance().config.getFilePrefix();
    return prefix == null ? "" : prefix + "-";
  }

  /**
   * Get push host
   *
   * @return --
   */
  public static String getHost() {
    String host = instance().config.getHost();
    if (host == null) {
      return null;
    }
    // if base url end is "/" remove it
    if (host.charAt(host.length() - 1) == '/') {
      host = host.substring(0, host.length() - 1);
    }
    return host;
  }

  /**
   * Get push token
   *
   * @return --
   */
  public static String getToken() {
    return instance().config.getToken();
  }

  public static ArkLogConfModel.Switch getLogSwitch(ArkLogType type) {
    Map switchMap = instance().config.getSwitchMap();
    if (switchMap == null) {
      return new ArkLogConfModel.Switch();
    }
    ArkLogConfModel.Switch switchModel = switchMap.get(type.getIdf());
    return switchModel == null ? new ArkLogConfModel.Switch() : switchModel;
  }

  /**
   * check log level in skip list
   *
   * @param type log level { ArkLogType }
   * @return should skip?
   */
  public static boolean shouldSkip(ArkLogType type) {
    return instance().skipList.contains(type);
  }

  /** Generate skip list step.1: anal each level config */
  private void preGenerateSkipList() {
    pushSkipList(ArkLogType.INF);
    pushSkipList(ArkLogType.DEB);
    pushSkipList(ArkLogType.WAR);
    pushSkipList(ArkLogType.ERR);
    pushSkipList(ArkLogType.FAL);
    pushSkipList(ArkLogType.KEY);
    pushSkipList(ArkLogType.VER);
  }

  /**
   * skip if specific switch all off
   *
   * @param type ArkLogType
   */
  private void pushSkipList(ArkLogType type) {
    Map switchMap = this.config.getSwitchMap();
    if (switchMap == null) {
      return;
    }
    ArkLogConfModel.Switch switchModel = switchMap.get(type.getIdf());
    if (switchModel == null) {
      return;
    }
    // skip if specific switch all off
    if (!switchModel.isPrint() && !switchModel.isWrite() && !switchModel.isUpload()) {
      this.skipList.add(type);
    }
  }

  /** Initialization on Demand Holder */
  private static class Holder {
    public static final ArkLogLoader INSTANCE = new ArkLogLoader();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy