【鸿蒙】封装日志工具类 ohos.hilog打印日志

封装一个ohos.hilog打印日志

首先要了解hilog四大日志类型: info、debug、warm、error

方法中四个参数的作用
domain: number
tag: string
format: string
...args: any[ ]

实例:

//普通的info日志,使用info方法来打印

//第一个参数 : 0x0000 表示当前日志的域为0

//第二个参数 : 表示此系统的日志过滤关键字为 mylog,可以在deveco的日志面板中去过滤查看

//第三个参数 : %{public}s 表示以明文的形式来显示字符串日志

//第四个及其以后的参数 : 表示要打印的日志

testTag 表示日志输出的关键字

%{public } s s指的是String 表示后面是以字符串的格式输出的

意思是 这个是以明文形式字符串输出

public 私有的 加密写死的

封装项目日志文件 :common/utils/Logger.ets

日志类的封装思路:

使用类的静态方法进行封装四个方法

导出这个类

复制代码
import  { hilog }  from  '@kit.PerformanceAnalysisKit'

const domain =0x0000
const tag ='mylog'
const format ='%{public}s %{public}s'

// 按需导出
export class Logger {
static info(...args: string[ ]){
 hilog.info(domain,tag,format, ...args)
}
static debug(...args: string[]){
hilog.debug(domain, tag,format,...args)
}
static warn(...args: string[]){
hilog.warn(domain,tag,format,...args)
}
static error(...args: string[]){
hilog.error(domain,tag,format, ...args)
}
}

用的时候 直接导入即可

相关推荐
浮生若茶808818 小时前
Flutter环境搭建全攻略之-Macos环境搭建
flutter·macos
Georgewu19 小时前
【HarmonyOS 6】 The target can not be empty. check the build.profile,json5 file of
harmonyos
Georgewu19 小时前
【HarmonyOS 6】Install Failed: error: failed to install bundle.code:9568322
harmonyos
爱笑的眼睛1121 小时前
HarmonyOS 应用开发新范式:深入剖析 Stage 模型与 ArkTS 状态管理
华为·harmonyos
爱笑的眼睛111 天前
深入浅出 HarmonyOS ArkUI 3.0:基于声明式开发范式与高级状态管理构建高性能应用
华为·harmonyos
农夫三拳_有点甜1 天前
Flutter Expanded 组件总结
flutter
电手1 天前
时隔4年麒麟重新登场!华为这8.8英寸新「手机」给我看麻了
华为·智能手机
火柴就是我1 天前
跟着官方demo 学flame 之 word 坐标系以及Camera的一些属性
flutter
程序员潘Sir1 天前
鸿蒙应用开发从入门到实战(一):鸿蒙应用开发概述
harmonyos
新镜1 天前
【Flutter】drag_select_grid_view: ^0.6.2 使用
flutter