背景:有的时候,客户的线上问题无法复现,只能通过日志定位客户的问题,比如定位的日志信息,需要通过日志来确定问题,所以需要这样日志系统。
在开源的鸿蒙三方库中有这样的一个三方库:@pie/log4a(V1.5.0),当前最新版本1.5.0,亲测有效。
使用方法:
1.第一步安装三方库
ohpm install @pie/log4a
2.安装依赖库
ohpm install @ohos/mail
3.使用方法
- 入口类初始化LogManager,
LogManager.getLogger('Index')
.addFileAppender('Xlog.log', 'mainAppender', Level.INFO, {
maxCacheCount: 10,
maxFileSize: 10,
expireTime: 5,
useWorker: true
})
.bindAppender(smtpAppender);
参数说明:Xlog.log是日志文件名字
useWorker: true多线程处理日志
maxFileSize:单个文件最大size
maxCacheCount:最多存储日志文件数量
expireTime:日志文件最多保存天数
2. 具体使用方法
@State logger: Logger = LogManager.getLogger(this);
this.logger.withMarker(MarkerManager.getMarker('Test Marker'))
.info('this is a test log, this is a test log, this is a test log, this is a test log, this is a test log, ');
3.实际日志
4.日志路径
getFileAppender(path: string, name: string, level: Level, options?: FileAppenderOptions) {
if (this._fileAppende