Logger.ets 1.43 KB
import hilog from '@ohos.hilog';

/**
 * Log level define
 *
 * @syscap SystemCapability.HiviewDFX.HiLog
 */
enum LogLevel {
  DEBUG = 3,
  INFO = 4,
  WARN = 5,
  ERROR = 6,
  FATAL = 7
}

/**
 * Common log for all features.
 *
 * @param {string} prefix Identifies the log tag.
 */
export class Logger {
  private static domain: number = 0xFF00;
  private static prefix: string = 'PeopleDailyApp';
  private static format: string = `%{public}s, %{public}s`;

  /**
   * constructor.
   *
   * @param Prefix Identifies the log tag.
   * @param domain Domain Indicates the service domain, which is a hexadecimal integer ranging from 0x0 to 0xFFFFF.
   */
  constructor(prefix: string = Logger.prefix, domain: number = Logger.domain) {
    Logger.prefix = prefix;
    Logger.domain = domain;
  }

  static debug(...args: string[]) {
    hilog.debug(Logger.domain, Logger.prefix, Logger.format, args);
  }

  static info(...args: string[]) {
    hilog.info(Logger.domain, Logger.prefix, Logger.format, args);
  }

  static warn(...args: string[]) {
    hilog.warn(Logger.domain, Logger.prefix, Logger.format, args);
  }

  static error(...args: string[]) {
    hilog.error(Logger.domain, Logger.prefix, Logger.format, args);
  }

  static fatal(...args: string[]) {
    hilog.fatal(Logger.domain, Logger.prefix, Logger.format, args);
  }

  static isLoggable(level: LogLevel) {
    hilog.isLoggable(Logger.domain, Logger.prefix, level);
  }
}

export default new Logger()