# 鸿蒙 next 封装日志工具类 LogUtil
鸿蒙 next 封装日志工具类 LogUtil
由于每次使用系统的 hilog 不太方便,所以这里封装 hilog 成日志工具工具类,方便每次调用.
1.封装工具类
import hilog from '@ohos.hilog'
const LOGGER_DOMAIN: number = 0x0000
const LOGGER_TAG: string = 'LogUtil'
/**
* TODO 日志工具类
*/
export class LogUtil {
private static domain: number = LOGGER_DOMAIN
private static tag: string = LOGGER_TAG //日志Tag
private static format: string = '%{public}s'
private static showLog: boolean = true //是否显示打印日志
/**
* 是否打印日志(该方法建议在Ability里调用)
* @param showLog
*/
static setShowLog(showLog: boolean = true) {
LogUtil.showLog = showLog
}
/**
* 打印DEBUG级别日志
* @param args
*/
static debug(...args: string[]): void {
if (LogUtil.showLog) {
hilog.debug(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(args.length), args)
}
}
/**
* 打印INFO级别日志
* @param args
*/
static info(...args: string[]): void {
if (LogUtil.showLog) {
hilog.info(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(args.length), args)
}
}
/**
* 打印WARN级别日志
* @param args
*/
static warn(...args: string[]): void {
if (LogUtil.showLog) {
hilog.warn(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(args.length), args)
}
}
/**
* 打印ERROR级别日志
* @param args
*/
static error(...args: string[]): void {
if (LogUtil.showLog) {
hilog.error(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(args.length), args)
}
}
/**
* 打印FATAL级别日志
* @param args
*/
static fatal(...args: string[]): void {
if (LogUtil.showLog) {
hilog.fatal(LogUtil.domain, LogUtil.tag, LogUtil.format.repeat(args.length), args)
}
}
}
2.在自己的代码中使用工具类
LogUtil.info("run log..............")
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!