1# @ohos.logLibrary (Log Library) 2 3The **logLibrary** module provides APIs for obtaining various system maintenance and test logs. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - The APIs provided by this module are system APIs. 9 10## Modules to Import 11 12```ts 13import logLibrary from '@ohos.logLibrary'; 14``` 15 16## LogEntry 17 18Defines a **LogEntry** object. 19 20**System capability**: SystemCapability.HiviewDFX.Hiview.LogLibrary 21 22| Name| Type| Readable| Writable| Description| 23| -------- | -------- | -------- | -------- | -------- | 24| name | string | Yes| No| Log file name. | 25| mtime | number | Yes| No | Time of the last modification to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.| 26| size | number | Yes| No | File size, in bytes.| 27 28## logLibrary.list 29 30list(logType: string): LogEntry[] 31 32Obtains the list of log files of the specified type in synchronous mode. This API accepts objects of the string type as input parameters and returns a list log files of the specified type. 33 34**Required permission**: ohos.permission.READ_HIVIEW_SYSTEM 35 36**System capability**: SystemCapability.HiviewDFX.Hiview.LogLibrary 37 38**Parameters** 39 40| Name | Type | Mandatory| Description | 41| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 42| logType | string | Yes| Log type, for example, **HILOG**, **FAULTLOG**, **BETACLUB**, or **REMOTELOG**.| 43 44**Return value** 45 46| Type | Description | 47| ------------------- | ------------------------------------------------------------ | 48| LogEntry[] | Array of log file objects.| 49 50**Error codes** 51 52For details about error codes, see [Log Library Error Codes](../errorcodes/errorcode-loglibrary.md). 53 54| ID| Error Message| 55| ------- | ----------------------------------------------------------------- | 56| 201 | Permission denied. | 57| 202 | Permission denied, non-system app called system api. | 58| 401 | Invalid argument.| 59 60**Example** 61 62```ts 63import logLibrary from '@ohos.logLibrary'; 64 65try { 66 let logObj = logLibrary.list('HILOG'); 67 // do something here. 68} catch (error) { 69 console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 70} 71``` 72 73## logLibrary.copy 74 75copy(logType: string, logName: string, dest: string): Promise<void> 76 77Copies log files of the specified type to the target application directory. This API uses a promise to return the result. 78 79**Required permission**: ohos.permission.READ_HIVIEW_SYSTEM 80 81**System capability**: SystemCapability.HiviewDFX.Hiview.LogLibrary 82 83**Parameters** 84 85| Name | Type | Mandatory| Description| 86| --------- | ----------------------- | ---- | --------------- | 87| logType | string | Yes| Log type, for example, **HILOG**, **FAULTLOG**, **BETACLUB**, or **REMOTELOG**.| 88| logName | string | Yes | Log file name.| 89| dest | string | Yes | Target directory. Enter the relative path of the directory. If this parameter is specified, log files will be saved to the **hiview/dest** folder in the application cache path, that is, **../cache/hiview/dest**. You can enter a multi-level directory.<br>If you leave this parameter empty, log files will be saved to the root directory, that is, the **hiview** folder in the application cache path.| 90 91**Return value** 92 93| Type | Description | 94| ------------------- | ------------------------------------------------------------ | 95| Promise<void> | Promise used to return the result. Depending on whether the operation is successful, you can use the **then()** or **catch()** method to process the callback.| 96 97**Error codes** 98 99For details about error codes, see [Log Library Error Codes](../errorcodes/errorcode-loglibrary.md). 100 101| ID| Error Message| 102| -------- | ---------------------------------------------------------------- | 103| 201 | Permission denied. | 104| 202 | Permission denied, non-system app called system api. | 105| 401 | Invalid argument.| 106| 21300001 | Source file does not exists. | 107 108**Example** 109 110```ts 111import logLibrary from '@ohos.logLibrary'; 112import { BusinessError } from '@ohos.base'; 113 114try { 115 logLibrary.copy('HILOG', 'hiapplogcat-1.zip', '' 116 ).then( 117 (val) => { 118 // do something here. 119 } 120 ).catch( 121 (err: BusinessError) => { 122 // do something here. 123 } 124 ) 125} catch (error) { 126 console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 127} 128``` 129 130## logLibrary.copy 131 132copy(logType: string, logName: string, dest: string, callback: AsyncCallback<void>): void 133 134Copies log files of the specified type to the target application directory. This API uses an asynchronous callback to return the result. 135 136**Required permission**: ohos.permission.READ_HIVIEW_SYSTEM 137 138**System capability**: SystemCapability.HiviewDFX.Hiview.LogLibrary 139 140**Parameters** 141 142| Name | Type | Mandatory| Description | 143| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 144| logType | string | Yes| Log type, for example, **HILOG**, **FAULTLOG**, **BETACLUB**, or **REMOTELOG**.| 145| logName | string | Yes | Log file name.| 146| dest | string | Yes | Target directory. Enter the relative path of the directory. If this parameter is specified, log files will be saved to the **hiview/dest** folder in the application cache path, that is, **../cache/hiview/dest**. You can enter a multi-level directory.<br>If you leave this parameter empty, log files will be saved to the root directory, that is, the **hiview** folder in the application cache path.| 147| callback | AsyncCallback<void> | Yes| Callback used to process the received return value. The value **0** indicates that the operation is successful, and any other value indicates that the operation has failed.| 148 149**Error codes** 150 151For details about error codes, see [Log Library Error Codes](../errorcodes/errorcode-loglibrary.md). 152 153| ID| Error Message| 154| ------- | ----------------------------------------------------------------- | 155| 201 | Permission denied. | 156| 202 | Permission denied, non-system app called system api. | 157| 401 | Invalid argument.| 158| 21300001 | Source file does not exists. | 159 160**Example** 161 162```ts 163import logLibrary from '@ohos.logLibrary'; 164 165try { 166 logLibrary.copy('HILOG', 'hiapplogcat-1.zip', 'dir1', (error, val) => { 167 if (val === undefined) { 168 // copy failed. 169 } else { 170 // copy success. 171 } 172 }); 173} catch (error) { 174 console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 175} 176``` 177 178## logLibrary.move 179 180move(logType: string, logName: string, dest: string): Promise<void> 181 182Moves log files of the specified type to the target application directory. This API uses a promise to return the result. 183 184**Required permission**: ohos.permission.WRITE_HIVIEW_SYSTEM 185 186**System capability**: SystemCapability.HiviewDFX.Hiview.LogLibrary 187 188**Parameters** 189 190| Name | Type | Mandatory| Description| 191| --------- | ----------------------- | ---- | --------------- | 192| logType | string | Yes| Log type, for example, **FAULTLOG**, **BETACLUB**, or **REMOTELOG**.| 193| logName | string | Yes | Log file name.| 194| dest | string | Yes | Target directory. Enter the relative path of the directory. If this parameter is specified, log files will be saved to the **hiview/dest** folder in the application cache path, that is, **../cache/hiview/dest**. You can enter a multi-level directory.<br>If you leave this parameter empty, log files will be saved to the root directory, that is, the **hiview** folder in the application cache path.| 195 196**Return value** 197 198| Type | Description | 199| ------------------- | ------------------------------------------------------------ | 200| Promise<void> | Promise used to return the result. Depending on whether the operation is successful, you can use the **then()** or **catch()** method to process the callback.| 201 202**Error codes** 203 204For details about error codes, see [Log Library Error Codes](../errorcodes/errorcode-loglibrary.md). 205 206| ID| Error Message| 207| -------- | ---------------------------------------------------------------- | 208| 201 | Permission denied. | 209| 202 | Permission denied, non-system app called system api. | 210| 401 | Invalid argument.| 211| 21300001 | Source file does not exists. | 212 213**Example** 214 215```ts 216import logLibrary from '@ohos.logLibrary'; 217import { BusinessError } from '@ohos.base'; 218 219try { 220 logLibrary.move('FAULTLOG', 'fault_log_test.zip', '' 221 ).then( 222 (val) => { 223 // do something here. 224 } 225 ).catch( 226 (err: BusinessError) => { 227 // do something here. 228 } 229 ) 230} catch (error) { 231 console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 232} 233``` 234 235## logLibrary.move 236 237move(logType: string, logName: string, dest: string, callback: AsyncCallback<void>): void 238 239Moves log files of the specified type to the target application directory. This API uses an asynchronous callback to return the result. 240 241**Required permission**: ohos.permission.WRITE_HIVIEW_SYSTEM 242 243**System capability**: SystemCapability.HiviewDFX.Hiview.LogLibrary 244 245**Parameters** 246 247| Name | Type | Mandatory| Description | 248| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 249| logType | string | Yes| Log type, for example, **HILOG**, **FAULTLOG**, **BETACLUB**, or **REMOTELOG**.| 250| logName | string | Yes | Log file name.| 251| dest | string | Yes | Target directory. Enter the relative path of the directory. If this parameter is specified, log files will be saved to the **hiview/dest** folder in the application cache path, that is, **../cache/hiview/dest**. You can enter a multi-level directory.<br>If you leave this parameter empty, log files will be saved to the root directory, that is, the **hiview** folder in the application cache path.| 252| callback | AsyncCallback<void> | Yes| Callback used to process the received return value. The value **0** indicates that the operation is successful, and any other value indicates that the operation has failed.| 253 254**Error codes** 255 256For details about error codes, see [Log Library Error Codes](../errorcodes/errorcode-loglibrary.md). 257 258| ID| Error Message| 259| ------- | ----------------------------------------------------------------- | 260| 201 | Permission denied. | 261| 202 | Permission denied, non-system app called system api. | 262| 401 | Invalid argument.| 263| 21300001 | Source file does not exists. | 264 265**Example** 266 267```ts 268import logLibrary from '@ohos.logLibrary'; 269 270try { 271 logLibrary.move('FAULTLOG', 'fault_log_test.zip', 'dir1/dir2', (error, val) => { 272 if (val === undefined) { 273 // move failed. 274 } else { 275 // move success. 276 } 277 }); 278} catch (error) { 279 console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 280} 281``` 282 283## logLibrary.remove 284 285remove(logType: string, logName: string): void 286 287Deletes log files of the specified type in synchronous mode. 288 289**Required permission**: ohos.permission.WRITE_HIVIEW_SYSTEM 290 291**System capability**: SystemCapability.HiviewDFX.Hiview.LogLibrary 292 293**Parameters** 294 295| Name | Type | Mandatory| Description | 296| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 297| logType | string | Yes| Log type, for example, **FAULTLOG**, **BETACLUB**, or **REMOTELOG**.| 298| logName | string | Yes | Log file name.| 299 300**Error codes** 301 302For details about error codes, see [Log Library Error Codes](../errorcodes/errorcode-loglibrary.md). 303 304| ID| Error Message| 305| ------- | ----------------------------------------------------------------- | 306| 201 | Permission denied. | 307| 202 | Permission denied, non-system app called system api. | 308| 401 | Invalid argument.| 309| 21300001 | Source file does not exists. | 310 311**Example** 312 313```ts 314import logLibrary from '@ohos.logLibrary'; 315 316try { 317 logLibrary.remove('FAULTLOG', 'fault_log_test.zip'); 318} catch (error) { 319 console.error(`error code: ${error?.code}, error msg: ${error?.message}`); 320} 321``` 322