1e41f4b71Sopenharmony_ci# @ohos.logLibrary (维测日志获取)(系统接口) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci本模块提供了获取各类系统维测日志的能力。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> - 本模块为系统接口。 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 导入模块 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { logLibrary } from '@kit.PerformanceAnalysisKit'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## LogEntry 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci日志文件对象接口。 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.Hiview.LogLibrary 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 23e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 24e41f4b71Sopenharmony_ci| name | string | 是 | 否 | 文件名称。 | 25e41f4b71Sopenharmony_ci| mtime | number | 是 | 否 | 上次修改该文件的时间,表示距1970年1月1日0时0分0秒的秒数。 | 26e41f4b71Sopenharmony_ci| size | number | 是 | 否 | 文件大小,以字节为单位。 | 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci## logLibrary.list 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_cilist(logType: string): LogEntry[] 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci以同步方法查询指定类型的日志文件列表,接收string类型的对象作为参数,返回指定类型日志的文件列表信息。 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.READ_HIVIEW_SYSTEM 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.Hiview.LogLibrary 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**参数:** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 41e41f4b71Sopenharmony_ci| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 42e41f4b71Sopenharmony_ci| logType | string | 是 | 日志类型字符串,例如“HILOG”, "FAULTLOG", "BETACLUB", "REMOTELOG"等。 | 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci**返回值:** 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci| 类型 | 说明 | 47e41f4b71Sopenharmony_ci| ------------------- | ------------------------------------------------------------ | 48e41f4b71Sopenharmony_ci| LogEntry[] | 日志文件对象的数组。 | 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**错误码:** 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[维测日志错误码](errorcode-loglibrary-sys.md)。 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 55e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------------------------- | 56e41f4b71Sopenharmony_ci| 201 | Permission denied. | 57e41f4b71Sopenharmony_ci| 202 | Permission denied, non-system app called system api. | 58e41f4b71Sopenharmony_ci| 401 | Invalid argument.| 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci**示例:** 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci```ts 63e41f4b71Sopenharmony_ciimport { logLibrary } from '@kit.PerformanceAnalysisKit'; 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_citry { 66e41f4b71Sopenharmony_ci let logObj = logLibrary.list('HILOG'); 67e41f4b71Sopenharmony_ci // do something here. 68e41f4b71Sopenharmony_ci} catch (error) { 69e41f4b71Sopenharmony_ci console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 70e41f4b71Sopenharmony_ci} 71e41f4b71Sopenharmony_ci``` 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci## logLibrary.copy 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_cicopy(logType: string, logName: string, dest: string): Promise<void> 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci拷贝指定日志类型的指定文件到目标应用目录下。使用Promise回调。 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.READ_HIVIEW_SYSTEM 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.Hiview.LogLibrary 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**参数:** 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 86e41f4b71Sopenharmony_ci| --------- | ----------------------- | ---- | --------------- | 87e41f4b71Sopenharmony_ci| logType | string | 是 | 日志类型字符串,例如“HILOG”, "FAULTLOG", "BETACLUB", "REMOTELOG"等。 | 88e41f4b71Sopenharmony_ci| logName | string | 是 | 日志文件名称。 | 89e41f4b71Sopenharmony_ci| dest | string | 是 | 目标目录,需填入相对目录名称。传入dest字串后,日志文件将保存到应用缓存路径下的"hiview/*dest*"文件夹,即"../cache/hiview/*dest*"。可填入多层目录。<br>如果传入空字串,将保存到根目录下,即应用缓存路径下的hiview文件夹。 | 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci**返回值:** 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci| 类型 | 说明 | 94e41f4b71Sopenharmony_ci| ------------------- | ------------------------------------------------------------ | 95e41f4b71Sopenharmony_ci| Promise<void> | Promise实例,可以在其then()、catch()方法中分别对拷贝成功、拷贝异常的回调进行处理。 | 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci**错误码:** 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[维测日志错误码](errorcode-loglibrary-sys.md)。 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 102e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------------------- | 103e41f4b71Sopenharmony_ci| 201 | Permission denied. | 104e41f4b71Sopenharmony_ci| 202 | Permission denied, non-system app called system api. | 105e41f4b71Sopenharmony_ci| 401 | Invalid argument.| 106e41f4b71Sopenharmony_ci| 21300001 | Source file does not exists. | 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci**示例:** 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci```ts 111e41f4b71Sopenharmony_ciimport { logLibrary } from '@kit.PerformanceAnalysisKit'; 112e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_citry { 115e41f4b71Sopenharmony_ci logLibrary.copy('HILOG', 'hiapplogcat-1.zip', '' 116e41f4b71Sopenharmony_ci ).then( 117e41f4b71Sopenharmony_ci (val) => { 118e41f4b71Sopenharmony_ci // do something here. 119e41f4b71Sopenharmony_ci } 120e41f4b71Sopenharmony_ci ).catch( 121e41f4b71Sopenharmony_ci (err: BusinessError) => { 122e41f4b71Sopenharmony_ci // do something here. 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci ) 125e41f4b71Sopenharmony_ci} catch (error) { 126e41f4b71Sopenharmony_ci console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 127e41f4b71Sopenharmony_ci} 128e41f4b71Sopenharmony_ci``` 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci## logLibrary.copy 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_cicopy(logType: string, logName: string, dest: string, callback: AsyncCallback<void>): void 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci拷贝指定日志类型的指定文件到目标应用目录下。使用callback回调。 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.READ_HIVIEW_SYSTEM 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.Hiview.LogLibrary 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci**参数:** 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 143e41f4b71Sopenharmony_ci| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 144e41f4b71Sopenharmony_ci| logType | string | 是 | 日志类型字符串,例如“HILOG”, "FAULTLOG", "BETACLUB", "REMOTELOG"等。 | 145e41f4b71Sopenharmony_ci| logName | string | 是 | 日志文件名称。 | 146e41f4b71Sopenharmony_ci| dest | string | 是 | 目标目录,需填入相对目录名称。传入dest字串后,日志文件将保存到应用缓存路径下的"hiview/*dest*"文件夹,即"../cache/hiview/*dest*"。可填入多层目录。<br>如果传入空字串,将保存到根目录下,即应用缓存路径下的hiview文件夹。 | 147e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数,可以在回调函数中处理接口返回值。0表示拷贝成功,其它值表示拷贝失败。 | 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci**错误码:** 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[维测日志错误码](errorcode-loglibrary-sys.md)。 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 154e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------------------------- | 155e41f4b71Sopenharmony_ci| 201 | Permission denied. | 156e41f4b71Sopenharmony_ci| 202 | Permission denied, non-system app called system api. | 157e41f4b71Sopenharmony_ci| 401 | Invalid argument.| 158e41f4b71Sopenharmony_ci| 21300001 | Source file does not exists. | 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci**示例:** 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci```ts 163e41f4b71Sopenharmony_ciimport { logLibrary } from '@kit.PerformanceAnalysisKit'; 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_citry { 166e41f4b71Sopenharmony_ci logLibrary.copy('HILOG', 'hiapplogcat-1.zip', 'dir1', (error, val) => { 167e41f4b71Sopenharmony_ci if (val === undefined) { 168e41f4b71Sopenharmony_ci // copy failed. 169e41f4b71Sopenharmony_ci } else { 170e41f4b71Sopenharmony_ci // copy success. 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci }); 173e41f4b71Sopenharmony_ci} catch (error) { 174e41f4b71Sopenharmony_ci console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 175e41f4b71Sopenharmony_ci} 176e41f4b71Sopenharmony_ci``` 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci## logLibrary.move 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_cimove(logType: string, logName: string, dest: string): Promise<void> 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci移动指定日志类型的指定文件到目标应用目录下。使用Promise回调。 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.WRITE_HIVIEW_SYSTEM 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.Hiview.LogLibrary 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci**参数:** 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 191e41f4b71Sopenharmony_ci| --------- | ----------------------- | ---- | --------------- | 192e41f4b71Sopenharmony_ci| logType | string | 是 | 日志类型字符串,例如"FAULTLOG", "BETACLUB", "REMOTELOG"等。 | 193e41f4b71Sopenharmony_ci| logName | string | 是 | 日志文件名称。 | 194e41f4b71Sopenharmony_ci| dest | string | 是 | 目标目录,需填入相对目录名称。传入dest字串后,日志文件将保存到应用缓存路径下的"hiview/*dest*"文件夹,即"../cache/hiview/*dest*"。可填入多层目录。<br>如果传入空字串,将保存到根目录下,即应用缓存路径下的hiview文件夹。 | 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci**返回值:** 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci| 类型 | 说明 | 199e41f4b71Sopenharmony_ci| ------------------- | ------------------------------------------------------------ | 200e41f4b71Sopenharmony_ci| Promise<void> | Promise实例,可以在其then()、catch()方法中分别对移动成功、移动异常的回调进行处理。 | 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**错误码:** 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[维测日志错误码](errorcode-loglibrary-sys.md)。 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 207e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------------------- | 208e41f4b71Sopenharmony_ci| 201 | Permission denied. | 209e41f4b71Sopenharmony_ci| 202 | Permission denied, non-system app called system api. | 210e41f4b71Sopenharmony_ci| 401 | Invalid argument.| 211e41f4b71Sopenharmony_ci| 21300001 | Source file does not exists. | 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci**示例:** 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci```ts 216e41f4b71Sopenharmony_ciimport { logLibrary } from '@kit.PerformanceAnalysisKit'; 217e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_citry { 220e41f4b71Sopenharmony_ci logLibrary.move('FAULTLOG', 'fault_log_test.zip', '' 221e41f4b71Sopenharmony_ci ).then( 222e41f4b71Sopenharmony_ci (val) => { 223e41f4b71Sopenharmony_ci // do something here. 224e41f4b71Sopenharmony_ci } 225e41f4b71Sopenharmony_ci ).catch( 226e41f4b71Sopenharmony_ci (err: BusinessError) => { 227e41f4b71Sopenharmony_ci // do something here. 228e41f4b71Sopenharmony_ci } 229e41f4b71Sopenharmony_ci ) 230e41f4b71Sopenharmony_ci} catch (error) { 231e41f4b71Sopenharmony_ci console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 232e41f4b71Sopenharmony_ci} 233e41f4b71Sopenharmony_ci``` 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci## logLibrary.move 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_cimove(logType: string, logName: string, dest: string, callback: AsyncCallback<void>): void 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci移动指定日志类型的指定文件到目标应用目录下。使用callback回调。 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.WRITE_HIVIEW_SYSTEM 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.Hiview.LogLibrary 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci**参数:** 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 248e41f4b71Sopenharmony_ci| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 249e41f4b71Sopenharmony_ci| logType | string | 是 | 日志类型字符串,例如“HILOG”, "FAULTLOG", "BETACLUB", "REMOTELOG"等。 | 250e41f4b71Sopenharmony_ci| logName | string | 是 | 日志文件名称。 | 251e41f4b71Sopenharmony_ci| dest | string | 是 | 目标目录,需填入相对目录名称。传入dest字串后,日志文件将保存到应用缓存路径下的"hiview/*dest*"文件夹,即"../cache/hiview/*dest*"。可填入多层目录。<br>如果传入空字串,将保存到根目录下,即应用缓存路径下的hiview文件夹。 | 252e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数,可以在回调函数中处理接口返回值。0表示移动成功,其它值表示移动失败。 | 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci**错误码:** 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[维测日志错误码](errorcode-loglibrary-sys.md)。 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 259e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------------------------- | 260e41f4b71Sopenharmony_ci| 201 | Permission denied. | 261e41f4b71Sopenharmony_ci| 202 | Permission denied, non-system app called system api. | 262e41f4b71Sopenharmony_ci| 401 | Invalid argument.| 263e41f4b71Sopenharmony_ci| 21300001 | Source file does not exists. | 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci**示例:** 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci```ts 268e41f4b71Sopenharmony_ciimport { logLibrary } from '@kit.PerformanceAnalysisKit'; 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_citry { 271e41f4b71Sopenharmony_ci logLibrary.move('FAULTLOG', 'fault_log_test.zip', 'dir1/dir2', (error, val) => { 272e41f4b71Sopenharmony_ci if (val === undefined) { 273e41f4b71Sopenharmony_ci // move failed. 274e41f4b71Sopenharmony_ci } else { 275e41f4b71Sopenharmony_ci // move success. 276e41f4b71Sopenharmony_ci } 277e41f4b71Sopenharmony_ci }); 278e41f4b71Sopenharmony_ci} catch (error) { 279e41f4b71Sopenharmony_ci console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 280e41f4b71Sopenharmony_ci} 281e41f4b71Sopenharmony_ci``` 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci## logLibrary.remove 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ciremove(logType: string, logName: string): void 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci以同步方法删除指定日志类型的指定文件。 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.WRITE_HIVIEW_SYSTEM 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.Hiview.LogLibrary 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci**参数:** 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 296e41f4b71Sopenharmony_ci| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 297e41f4b71Sopenharmony_ci| logType | string | 是 | 日志类型字符串,例如"FAULTLOG", "BETACLUB", "REMOTELOG"等。 | 298e41f4b71Sopenharmony_ci| logName | string | 是 | 日志文件名称。 | 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci**错误码:** 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[维测日志错误码](errorcode-loglibrary-sys.md)。 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 305e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------------------------- | 306e41f4b71Sopenharmony_ci| 201 | Permission denied. | 307e41f4b71Sopenharmony_ci| 202 | Permission denied, non-system app called system api. | 308e41f4b71Sopenharmony_ci| 401 | Invalid argument.| 309e41f4b71Sopenharmony_ci| 21300001 | Source file does not exists. | 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci**示例:** 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci```ts 314e41f4b71Sopenharmony_ciimport { logLibrary } from '@kit.PerformanceAnalysisKit'; 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_citry { 317e41f4b71Sopenharmony_ci logLibrary.remove('FAULTLOG', 'fault_log_test.zip'); 318e41f4b71Sopenharmony_ci} catch (error) { 319e41f4b71Sopenharmony_ci console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 320e41f4b71Sopenharmony_ci} 321e41f4b71Sopenharmony_ci```