UNPKG

2.24 kBMarkdownView Raw
1WDIO Logger Utility
2===================
3
4> A helper utility for logging of WebdriverIO packages
5
6This package is used across all WebdriverIO packages to log information using the [`loglevel`](https://www.npmjs.com/package/loglevel) package. It can also be used for any other arbitrary Node.js project.
7
8## Install
9
10To install the package just call
11
12```sh
13npm install @wdio/logger
14```
15
16or when adding it to a WebdriverIO subpackage:
17
18```sh
19lerna add @wdio/logger --scope <subpackage>
20```
21
22## Usage
23
24The package exposes a logger function that you can use to register an instance for your scoped package:
25
26```js
27import logger from '@wdio/logger'
28
29const log = logger('myPackage')
30log.info('some logs')
31```
32
33For more info see [`loglevel`](https://www.npmjs.com/package/loglevel) package on NPM.
34
35## Custom Log Levels
36
37This package extends the log levels available in [`loglevel`](https://www.npmjs.com/package/loglevel) by introducing a new level called `progress`.
38
39The `progress` level is particularly useful when you need to dynamically update a specific line in the terminal. For example, it can be utilized to display the download progress of browsers or drivers.
40
41Notably, the `progress` level is equivalent to the `info` level. Therefore, if you set the log level to `error` or `silent`, any `progress` logs will be suppressed.
42
43It's important to mention that `progress` writes directly to `process.stdout`, and these logs won't be captured in any log files.
44
45To ensure consistent formatting with subsequent logs while using `progress`, it's essential to clear it at the end. To do so, simply call `progress` with an empty string, which will clear the last line:
46
47```
48log.progress('')
49```
50
51### Illustrative Usage of Progress
52
53```javascript
54import logger from '@wdio/logger';
55
56const log = logger('internal');
57
58const totalSize = 100;
59let uploadedSize = 0;
60
61const uploadInterval = setInterval(() => {
62 const chunkSize = 10;
63 uploadedSize += chunkSize;
64 const data = `Progress: ${(uploadedSize * 100) / totalSize}%`;
65 log.progress(data);
66 if (uploadedSize >= totalSize) {
67 clearInterval(uploadInterval);
68 log.progress(''); // Called at the end to maintain the alignment of subsequent logs.
69 console.log('Upload complete.');
70 }
71}, 100);
72```