1 | WDIO Logger Utility
|
2 | ===================
|
3 |
|
4 | > A helper utility for logging of WebdriverIO packages
|
5 |
|
6 | This 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 |
|
10 | To install the package just call
|
11 |
|
12 | ```sh
|
13 | npm install @wdio/logger
|
14 | ```
|
15 |
|
16 | or when adding it to a WebdriverIO subpackage:
|
17 |
|
18 | ```sh
|
19 | lerna add @wdio/logger --scope <subpackage>
|
20 | ```
|
21 |
|
22 | ## Usage
|
23 |
|
24 | The package exposes a logger function that you can use to register an instance for your scoped package:
|
25 |
|
26 | ```js
|
27 | import logger from '@wdio/logger'
|
28 |
|
29 | const log = logger('myPackage')
|
30 | log.info('some logs')
|
31 | ```
|
32 |
|
33 | For more info see [`loglevel`](https://www.npmjs.com/package/loglevel) package on NPM.
|
34 |
|
35 | ## Custom Log Levels
|
36 |
|
37 | This package extends the log levels available in [`loglevel`](https://www.npmjs.com/package/loglevel) by introducing a new level called `progress`.
|
38 |
|
39 | The `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 |
|
41 | Notably, 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 |
|
43 | It's important to mention that `progress` writes directly to `process.stdout`, and these logs won't be captured in any log files.
|
44 |
|
45 | To 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 | ```
|
48 | log.progress('')
|
49 | ```
|
50 |
|
51 | ### Illustrative Usage of Progress
|
52 |
|
53 | ```javascript
|
54 | import logger from '@wdio/logger';
|
55 |
|
56 | const log = logger('internal');
|
57 |
|
58 | const totalSize = 100;
|
59 | let uploadedSize = 0;
|
60 |
|
61 | const 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 | ```
|