当前位置:   article > 正文

HiLog日志打印

hilog

目录

1、导入模块

2、常用方法

3、示例

4、用DevEco Studio查看日志


1、导入模块

import hilog from '@ohos.hilog';

2、常用方法

(1) hilog.debug/info/warn/error

debug(domain: number, tag: string, format: string, ...args: any[]) : void

domain日志对应的领域标识,范围是0x0~0xFFFF。
tag指定日志标识,可以为任意字符串
format格式字符串,用于日志的格式化输出
args与格式字符串format对应的可变长度参数列表

3、示例

日志打印公共类

  1. import hilog from '@ohos.hilog';
  2. const LOGGER_PREFIX:string = 'MyApp';
  3. class Logger {
  4. private domain:number;
  5. private prefix:string;
  6. private format:string = '%{public}s, %{public}s'
  7. constructor(prefix:string, domain:number = 0xFF00) {
  8. this.prefix = prefix
  9. this.domain = domain
  10. }
  11. debug(...args:any[]):void {
  12. hilog.debug(this.domain, this.prefix, this.format, args)
  13. }
  14. info(...args:any[]):void {
  15. hilog.info(this.domain, this.prefix, this.format, args)
  16. }
  17. warn(...args:any[]):void{
  18. hilog.warn(this.domain, this.prefix, this.format, args)
  19. }
  20. error(...args:any[]):void{
  21. hilog.error(this.domain, this.prefix, this.format, args)
  22. }
  23. }
  24. export default new Logger(LOGGER_PREFIX, 0xFF02)

业务调用日志打印公共类,因为我们公共日志类参数是一个可变数组,我们可以把第一个参数作为业务类型的TAG,比如下面的"EntryAbility",第二个参数传入具体打印的日志,如下

  1. let params = 6666
  2. Logger.debug('EntryAbility', `this is EntryAbilit ${params}`);

4、用DevEco Studio查看日志

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/245048?site
推荐阅读
相关标签
  

闽ICP备14008679号