鸿蒙开发系列教程(十六)--日志处理

console控制台输出

以格式化输出方式打印调试信息
console.debug()
console.debug(‘debug。。。’ )

以格式化输出方式打印日志信息
console.log()
console.log(‘info。。。’)

以格式化输出方式打印警告信息
console.warn()

输出信息
console.info()

注意:输出字符串+变量

console.info(`Aceaaa${JSON.stringify(oldValue)} `)

一定注意引号是英文下的“`” ,即ESC下的"```"

自定义日志

新建logger.ets 文件
导入鸿蒙基础库
import hilog from ‘@ohos.hilog’;

import hilog from '@ohos.hilog';
class Logger {
  private domain: number;
  private prefix: string;
  private format: string = '%{public}s, %{public}s';

  constructor(prefix: string = 'MyApp', domain: number = 0xFF00) {
    this.prefix = prefix;
    this.domain = domain;
  }

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

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

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

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

export default new Logger('[OxHornCampus]', 0xFF00)

调用:

import Logger from '../Logger';

Logger.info(`输出内容。。。。。`);

你可能感兴趣的:(鸿蒙,harmonyos,华为)