鸿蒙开发日志工具类

日志工具类

import hilog from '@ohos.hilog'
import { BusinessError } from '@kit.BasicServicesKit'

const LOGGER_DOMAIN: number = 0x0000
const LOGGER_TAG: string = 'HarmonyLogUtil'

/**
 * 日志工具类
 */
export class LogUtil {
  private static domain: number = LOGGER_DOMAIN // 日志领域标识
  private static tag: string = LOGGER_TAG // 日志标签
  private static format: string = '%{public}s' // 日志格式
  private static isLoggingEnabled: boolean = true // 是否启用日志

  /**
   * 配置日志参数(建议在Ability中调用)
   * @param domain - 日志领域标识
   * @param tag - 日志标签
   * @param isLoggingEnabled - 是否启用日志
   */
  static configure(domain: number = LOGGER_DOMAIN, tag: string = LOGGER_TAG, isLoggingEnabled: boolean = true): void {
    LogUtil.domain = domain
    LogUtil.tag = tag
    LogUtil.isLoggingEnabled = isLoggingEnabled
  }

  /**
   * 设置日志领域标识,范围是0x0~0xFFFF。
   * @param domain - 日志领域标识
   */
  static setDomain(domain: number): void {
    LogUtil.domain = domain
  }

  /**
   * 设置日志标签。
   * @param tag - 日志标签
   */
  static setTag(tag: string): void {
    LogUtil.tag = tag
  }

  /**
   * 启用或禁用日志记录。
   * @param isLoggingEnabled - 是否启用日志
   */
  static setLoggingEnabled(isLoggingEnabled: boolean): void {
    LogUtil.isLoggingEnabled = isLoggingEnabled
  }

  /**
   * 打印DEBUG级别日志
   * @param messages - 日志内容
   */
  static debug(...messages: string[]): void {
    if (LogUtil.isLoggingEnabled) {
      hilog.debug(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(messages.length), ...messages)
    }
  }

  /**
   * 打印INFO级别日志
   * @param messages - 日志内容
   */
  static info(...messages: string[]): void {
    if (LogUtil.isLoggingEnabled) {
      hilog.info(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(messages.length), ...messages)
    }
  }

  /**
   * 打印WARN级别日志
   * @param messages - 日志内容
   */
  static warn(...messages: string[]): void {
    if (LogUtil.isLoggingEnabled) {
      hilog.warn(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(messages.length), ...messages)
    }
  }

  /**
   * 打印ERROR级别日志
   * @param messages - 日志内容
   */
  static error(...messages: string[]): void {
    if (LogUtil.isLoggingEnabled) {
      const message = messages.join(' ')
      hilog.error(LogUtil.domain, LogUtil.tag, `[ERROR]: ${message}`)
    }
  }

  /**
   * 打印FATAL级别日志
   * @param messages - 日志内容
   */
  static fatal(...messages: string[]): void {
    if (LogUtil.isLoggingEnabled) {
      hilog.fatal(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(messages.length), ...messages)
    }
  }

  /**
   * 打印JSON对象或JSON字符串
   * @param obj - JSON对象或JSON字符串
   */
  static logJson(obj: object | string): void {
    try {
      let formattedJson: string

      if (typeof obj === 'string') {
        // 尝试将字符串解析为JSON对象
        const parsedObj = JSON.parse(obj) as object
        formattedJson = JSON.stringify(parsedObj, null, 2)
      } else {
        // 直接将对象转换为格式化的JSON字符串
        formattedJson = JSON.stringify(obj, null, 2)
      }

      // 逐行打印JSON字符串
      formattedJson.split('\n').forEach(line => console.debug(line))
    } catch (err) {
      LogUtil.error(JSON.stringify(err))
    }
  }
}

export default LogUtil

posted @ 2024-07-20 13:48  鸿蒙布道师  阅读(16)  评论(0)    收藏  举报  来源