| Name | Date | Size | #Lines | LOC | ||
|---|---|---|---|---|---|---|
| .. | - | - | ||||
| AppScope/ | 06-May-2025 | - | 36 | 33 | ||
| entry/ | 06-May-2025 | - | 727 | 661 | ||
| hvigor/ | 06-May-2025 | - | 24 | 22 | ||
| log/ | 06-May-2025 | - | 473 | 368 | ||
| screenshots/device/ | 06-May-2025 | - | ||||
| .gitignore | D | 06-May-2025 | 145 | 12 | 11 | |
| README.md | D | 06-May-2025 | 1.5 KiB | 80 | 57 | |
| README_zh.md | D | 06-May-2025 | 2.3 KiB | 61 | 43 | |
| build-profile.json5 | D | 06-May-2025 | 1.1 KiB | 47 | 45 | |
| hvigorfile.ts | D | 06-May-2025 | 763 | 17 | 1 | |
| hvigorw | D | 06-May-2025 | 2.1 KiB | 62 | 28 | |
| hvigorw.bat | D | 06-May-2025 | 2 KiB | 72 | 56 | |
| oh-package.json5 | D | 06-May-2025 | 812 | 27 | 25 | |
| ohosTest.md | D | 06-May-2025 | 381 | 8 | 6 |
README.md
1# Log 2 3## Introduction 4 5This sample shows how to use log APIs to print log information and save the information to the application installation directory. 6 7## APIs 8 9``` 10 import logger from '@ohos/log' 11 import { LogLevel } from '@ohos/log' 12 import { Configure } from '@ohos/log' 13``` 14 15## Usage Instruction 16 171. Configure log printing parameters. 18 19``` 20 Configure = { 21 cheese: { 22 types: string[], 23 filename: string 24 } 25 defaults: { 26 appenders: string, 27 level: LogLevel 28 } 29 } 30 // If types is set to file, logs are saved to the file named by filename. appenders indicates the log tag, and level indicates the lowest log level. 31``` 32 332. Initialize a **logger** instance. 34 35``` 36 logger.setConfigure(configure: Configure) 37 // configure indicates the parameter configuration. 38``` 39 403. Print log information of the debug level. 41 42``` 43 logger.debug(value) 44 // value indicates the log content. 45``` 46 474. Print log information of the info level. 48 49``` 50 logger.info(value) 51 // value indicates the log content. 52``` 53 545. Print log information of the warn level. 55 56``` 57 logger.warn(value) 58 // value indicates the log content. 59``` 60 616. Print log information of the error level. 62 63``` 64 logger.error(value) 65 // value indicates the log content. 66``` 67 687. Print log information of the fatal level. 69 70``` 71 logger.fatal(value, bool) 72 // value indicates the log content. 73``` 74 75## Constraints 76 771. This sample requires API version 9 or later. 78 792. This sample requires DevEco Studio 3.1 Canary1 (Build Version: 3.1.0.100) to compile and run. 80
README_zh.md
1# 日志打印 2 3### 介绍 4 5本示例使用[hilog日志系统](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-performance-analysis-kit/js-apis-hilog.md) ,提供日志打印类,使应用/服务可以按照指定级别、标识和格式字符串输出日志内容,帮助开发者了解应用/服务的运行状态,更好地调试程序。 6 7### 效果预览 8|首页| 9|---------| 10|| 11 12使用说明: 13 141.进入应用会自动生成一个空的日志文件。 15 162.点击log按钮即可输出日志,并将日志生成到日志文件当中。 17 18### 工程目录 19``` 20entry/src/main/ets/ 21|---pages 22| |---Index.ets // 首页 23log/src/main/ets/ 24|---components 25| |---mainpage 26| | |---Configure.ets // 日志打印配置项 27| | |---Logger.ets // 日志打印类 28| | |---LoggerModel.ts // 封装日志打印类 29| | |---LogLevel.ts // 日志等级枚举值 30``` 31 32### 具体实现 33+ 日志输出功能封装在Logger,源码参考:[Logger.ets](log/src/main/ets/components/mainpage/Logger.ets): 34 + 日志输出:Logger类根据Configure的types参数将日志分为三个类型,其中file类型会将日志写入本地文件,console类型调用ConsoleLoggerStrategy类输出,hilog类型调用HilogLoggerStrategy类输出; 35 + 日志文件写入本地:FileLoggerStrategy类使用[@ohos.file.fs](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-file-fs.md) 将日志写入本地文件中,本示例只是展示了文件写入文件操作的使用方法,在实战场景中,建议把耗时操作放入子线程中运行。 36 37### 相关权限 38 39不涉及。 40 41### 依赖 42 43不涉及。 44 45### 约束与限制 46 471.本示例已适配API version 9版本SDK,版本号:3.2.11.9; 48 492.本示例需要使用DevEco Studio 3.1 Beta2 (Build Version: 3.1.0.400 构建 2023年4月7日)及以上版本才可编译运行。 50 51### 下载 52如需单独下载本工程,执行如下命令: 53``` 54git init 55git config core.sparsecheckout true 56echo code/BasicFeature/DFX/Logger/ > .git/info/sparse-checkout 57git remote add origin https://gitee.com/openharmony/applications_app_samples.git 58git pull origin master 59``` 60 61