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

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

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

import com.heavenark.infrastructure.log.exception.ArkLogRuntimeException;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Ark-Log factory for create logger instance
 *
 * @author 冰糕Luminous BGLuminous Luminous
 * @since 2.2.1
 */
public class LogFactory {
  /** logger instance save map */
  private static final ConcurrentHashMap loggerMap = new ConcurrentHashMap<>();
  /** factory prefix */
  protected String factoryPrefix = "MAIN";

  /** Constructor Private */
  private LogFactory() {}

  /**
   * Get current factory instance
   *
   * @return ArkLogFactory instance
   */
  private static LogFactory getInstance() {
    return Holder.INSTANCE;
  }

  /**
   * Set new logger prefix
   *
   * @param factoryPrefix factory prefix
   */
  public static void setFactoryPrefix(String factoryPrefix) {
    getInstance().factoryPrefix = factoryPrefix;
  }

  /**
   * Get logger instance
   *
   * @param clz logger target class
   * @return logger
   */
  public static Logger getLogger(Class clz) {
    if (clz == null) {
      throw new ArkLogRuntimeException("Record class should not be null.");
    }
    return getInstance().getOrCreateLogger(clz.getName());
  }

  /**
   * Get or create logger instance, if logger already exist return it, if not found at map create a
   * new logger and return it.
   *
   * @param className logger target class name
   * @return logger
   */
  private Logger getOrCreateLogger(String className) {
    Logger logger = loggerMap.get(className);
    if (logger != null) {
      return logger;
    }
    logger = new Logger(factoryPrefix, className);
    loggerMap.putIfAbsent(className, logger);
    return logger;
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy