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

com.heavenark.infrastructure.log.util.ArkLogPublicUtil Maven / Gradle / Ivy

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

import com.heavenark.infrastructure.base.model.ArkLogConfModel;
import com.heavenark.infrastructure.log.ArkLogLoader;
import com.heavenark.infrastructure.log.component.ArkLogPrinter;
import com.heavenark.infrastructure.log.model.ArkLogType;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.heavenark.infrastructure.log.ArkLogLoader.PROJECT_NAME;

/**
 * Ark-Log Util
 *
 * @author 冰糕Luminous BGLuminous Luminous
 * @since 2.0.0
 */
public class ArkLogPublicUtil {
  /** base log format */
  private static final String BASE_FORMAT = "[%-23s][%-5s][%-3s]%-24s-> %s: %s";

  /** Ark-log date format */
  private final SimpleDateFormat logDateFormat;
  /** Ark-log write date format */
  private final SimpleDateFormat writeFileDateFormat = new SimpleDateFormat("yyyy-MM-dd");

  /** Private Constructor */
  private ArkLogPublicUtil() {
    String date = ArkLogLoader.getDateFormat();
    logDateFormat = date.equals("TT") ? null : new SimpleDateFormat(date);
  }

  /**
   * Get Ark-Log util instance
   *
   * @return util instance
   */
  public static ArkLogPublicUtil instance() {
    return Holder.INSTANCE;
  }

  /**
   * Print Ark-Log internal log, this log only will print not include write and upload
   *
   * @param clz log target class
   * @param type log level type
   * @param log log data
   */
  public static void internalLog(Class clz, ArkLogType type, String log) {
    // pass config
    if (ArkLogLoader.shouldSkip(type)) {
      return;
    }
    ArkLogConfModel.Switch logSwitch = ArkLogLoader.getLogSwitch(type);
    if (!logSwitch.isPrint()) {
      return;
    }
    // prepare formatted final log
    Thread threadInfo = Thread.currentThread();
    String shotPackageInfo = ArkLogRunnerUtil.getShortPackageInfo(clz.getName());
    String className = clz.getSimpleName();
    String classFormat = String.format("%s(%s.java:%s)", shotPackageInfo, className, "%s");
    String date = ArkLogPublicUtil.getDate(true, new Date());
    String si = threadInfo.toString().substring(6);
    String stack = ArkLogRunnerUtil.stackLocation(className, classFormat);
    String basicLog = ArkLogRunnerUtil.formatProcess(log);
    String finalLog =
        String.format(BASE_FORMAT, date, PROJECT_NAME, type.getIdf(), si, stack, basicLog);
    ArkLogPrinter.print(type, finalLog);
  }

  /**
   * Check file or directory status
   *
   * @param file current file or directory
   * @param fileMode check file
   * @return is file or directory not exist or wrong type return true
   */
  public static boolean checkFileOrDirMissing(File file, boolean fileMode) {
    return fileMode ? (!file.exists() || !file.isFile()) : (!file.exists() || !file.isDirectory());
  }

  /**
   * Get date
   *
   * @param forLog get log date or write file date
   * @param date date time
   * @return date
   */
  public static String getDate(boolean forLog, Date date) {
    if (forLog && instance().logDateFormat == null) {
      return String.valueOf(date.getTime());
    }
    return forLog
        ? instance().logDateFormat.format(new Date())
        : instance().writeFileDateFormat.format(date);
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy