@ui5/logger 3.0.0-next.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.reuse/dep5 +1 -1
- package/CHANGELOG.md +50 -9
- package/CONTRIBUTING.md +1 -1
- package/LICENSE.txt +1 -1
- package/README.md +10 -9
- package/jsdoc.json +59 -58
- package/lib/index.js +51 -0
- package/lib/loggers/Build.js +91 -0
- package/lib/loggers/Logger.js +240 -0
- package/lib/loggers/ProjectBuild.js +88 -0
- package/lib/writers/Console.js +378 -0
- package/package.json +39 -28
- package/index.js +0 -5
- package/lib/logger.js +0 -160
package/.reuse/dep5
CHANGED
|
@@ -25,5 +25,5 @@ Disclaimer: The code in this project may include calls to APIs (“API Calls”)
|
|
|
25
25
|
parties the right to use of access any SAP External Product, through API Calls.
|
|
26
26
|
|
|
27
27
|
Files: *
|
|
28
|
-
Copyright: 2018-
|
|
28
|
+
Copyright: 2018-2023 SAP SE or an SAP affiliate company and UI5 Tooling contributors
|
|
29
29
|
License: Apache-2.0
|
package/CHANGELOG.md
CHANGED
|
@@ -2,17 +2,62 @@
|
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
4
4
|
|
|
5
|
-
A list of unreleased changes can be found [here](https://github.com/SAP/ui5-logger/compare/v3.0.0
|
|
5
|
+
A list of unreleased changes can be found [here](https://github.com/SAP/ui5-logger/compare/v3.0.0...HEAD).
|
|
6
|
+
|
|
7
|
+
<a name="v3.0.0"></a>
|
|
8
|
+
## [v3.0.0] - 2023-02-09
|
|
9
|
+
### Breaking Changes
|
|
10
|
+
|
|
11
|
+
- Replace npmlog with @ui5/logger/Logger (#363) [`66a159a`](https://github.com/SAP/ui5-logger/commit/66a159acd9b67a27dd66d1e8056c362585f51bcf)
|
|
12
|
+
- Deprecate advanced APIs in preparation of refactoring [`3aea5e7`](https://github.com/SAP/ui5-logger/commit/3aea5e766f9bda156e8c7e62a2e8c65f613ef7e9)
|
|
13
|
+
- Transform to ES Modules ([#306](https://github.com/SAP/ui5-logger/issues/306)) [`c79608b`](https://github.com/SAP/ui5-logger/commit/c79608b0e432168ca8570530b63a456b9ddd12cb)
|
|
14
|
+
- Require Node.js ^16.18.0, >=18.12.0 / npm >= 8 [`a8af8a7`](https://github.com/SAP/ui5-logger/commit/a8af8a7a82c6f657ac10b5018e654939d90fd81f)
|
|
15
|
+
|
|
16
|
+
### BREAKING CHANGE
|
|
17
|
+
|
|
18
|
+
The @ui5/logger got refactored and as a result its API went public.
|
|
19
|
+
|
|
20
|
+
Remove the usage of npmlog and refactor @ui5/logger modules to emit log events which are then caught in dedicated handlers. This is somewhat inspired by npm's proc-log module.
|
|
21
|
+
|
|
22
|
+
This breaking change removes capabilities that are likely to change and should not be part of a public API.
|
|
23
|
+
|
|
24
|
+
This will ensure that later changes to the module can be donen in a
|
|
25
|
+
compatible manner.
|
|
26
|
+
|
|
27
|
+
Relevant changes:
|
|
28
|
+
|
|
29
|
+
- Restrict log-methods to two argument only. The use of placeholders
|
|
30
|
+
like '%s' is no longer supported. A warning will be logged if more
|
|
31
|
+
than two argument is supplied. Placeholders will be replaced with a
|
|
32
|
+
deprecation message. We suggest the use of template literals.
|
|
33
|
+
- Deprecate #getGroupLogger method. Calling it throws an error.
|
|
34
|
+
It will be removed in one of the next patch releases
|
|
35
|
+
- Deprecate #setShowProgress method. Calling it throws an error.
|
|
36
|
+
It will be removed in one of the next patch releases
|
|
37
|
+
- Remove GroupLogger and TaskLogger classes. Similar functionality might
|
|
38
|
+
be re-added in a later release.
|
|
39
|
+
|
|
40
|
+
This package has been transformed to ES Modules. Therefore it no longer provides a CommonJS export.
|
|
41
|
+
If your project uses CommonJS, it needs to be converted to ES Modules or use a dynamic import.
|
|
42
|
+
|
|
43
|
+
For more information see also:
|
|
44
|
+
|
|
45
|
+
- https://sap.github.io/ui5-tooling/updates/migrate-v3/
|
|
46
|
+
- https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
|
47
|
+
|
|
48
|
+
Support for older Node.js and npm releases has been dropped.
|
|
49
|
+
Only Node.js versions v16.18.0, v18.12.0 or higher as well as npm v8 or higher are supported.
|
|
50
|
+
|
|
51
|
+
### Features
|
|
52
|
+
|
|
53
|
+
- Add new log level "perf" [`acf0c71`](https://github.com/SAP/ui5-logger/commit/acf0c717612f440ea7a114e757c05d358ae523a7)
|
|
6
54
|
|
|
7
|
-
<a name="v3.0.0-next.0"></a>
|
|
8
|
-
## [v3.0.0-next.0] - 2021-11-23
|
|
9
55
|
|
|
10
56
|
<a name="v2.0.1"></a>
|
|
11
57
|
## [v2.0.1] - 2020-10-22
|
|
12
58
|
### Bug Fixes
|
|
13
59
|
- Typos in error messages [`1d25902`](https://github.com/SAP/ui5-logger/commit/1d2590223c4332f5ea6f1326b23ecf584fea5934)
|
|
14
60
|
|
|
15
|
-
|
|
16
61
|
<a name="v2.0.0"></a>
|
|
17
62
|
## [v2.0.0] - 2020-03-31
|
|
18
63
|
### Breaking Changes
|
|
@@ -23,13 +68,11 @@ A list of unreleased changes can be found [here](https://github.com/SAP/ui5-logg
|
|
|
23
68
|
Support for older Node.js releases has been dropped.
|
|
24
69
|
Only Node.js v10 or higher is supported.
|
|
25
70
|
|
|
26
|
-
|
|
27
71
|
<a name="v1.0.2"></a>
|
|
28
72
|
## [v1.0.2] - 2019-10-14
|
|
29
73
|
### Bug Fixes
|
|
30
74
|
- Fix handling of log level "silent" [`020ced8`](https://github.com/SAP/ui5-logger/commit/020ced85a82d33c94e429aa28983affa0d8341ba)
|
|
31
75
|
|
|
32
|
-
|
|
33
76
|
<a name="v1.0.1"></a>
|
|
34
77
|
## [v1.0.1] - 2019-03-21
|
|
35
78
|
|
|
@@ -41,7 +84,6 @@ Only Node.js v10 or higher is supported.
|
|
|
41
84
|
### Features
|
|
42
85
|
- Add UI5_LOG_LVL environment variable [`c3e65c4`](https://github.com/SAP/ui5-logger/commit/c3e65c444045832773e4dc43ffa2baf903a27e52)
|
|
43
86
|
|
|
44
|
-
|
|
45
87
|
<a name="v0.2.1"></a>
|
|
46
88
|
## [v0.2.1] - 2018-10-29
|
|
47
89
|
|
|
@@ -53,8 +95,7 @@ Only Node.js v10 or higher is supported.
|
|
|
53
95
|
|
|
54
96
|
<a name="v0.0.1"></a>
|
|
55
97
|
## v0.0.1 - 2018-06-06
|
|
56
|
-
|
|
57
|
-
[v3.0.0-next.0]: https://github.com/SAP/ui5-logger/compare/v2.0.1...v3.0.0-next.0
|
|
98
|
+
[v3.0.0]: https://github.com/SAP/ui5-logger/compare/v2.0.1...v3.0.0
|
|
58
99
|
[v2.0.1]: https://github.com/SAP/ui5-logger/compare/v2.0.0...v2.0.1
|
|
59
100
|
[v2.0.0]: https://github.com/SAP/ui5-logger/compare/v1.0.2...v2.0.0
|
|
60
101
|
[v1.0.2]: https://github.com/SAP/ui5-logger/compare/v1.0.1...v1.0.2
|
package/CONTRIBUTING.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
# Contributing to the UI5 Tooling
|
|
2
2
|
|
|
3
|
-
See CONTRIBUTING.md in the [SAP/ui5-tooling](https://github.com/SAP/ui5-tooling/blob/
|
|
3
|
+
See CONTRIBUTING.md in the [SAP/ui5-tooling](https://github.com/SAP/ui5-tooling/blob/main/CONTRIBUTING.md) repository.
|
package/LICENSE.txt
CHANGED
|
@@ -186,7 +186,7 @@
|
|
|
186
186
|
same "printed page" as the copyright notice for easier
|
|
187
187
|
identification within third-party archives.
|
|
188
188
|
|
|
189
|
-
Copyright 2018-
|
|
189
|
+
Copyright 2018-2023 SAP SE or an SAP affiliate company and UI5 Tooling contributors
|
|
190
190
|
|
|
191
191
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
192
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-

|
|
2
2
|
|
|
3
3
|
# ui5-logger
|
|
4
|
-
>
|
|
5
|
-
>
|
|
4
|
+
> Providing logging functionality for all UI5 Tooling modules and extensions.
|
|
5
|
+
> Features log levels and rendering of a progress bar.
|
|
6
|
+
> Part of [UI5 Tooling](https://github.com/SAP/ui5-tooling)
|
|
6
7
|
|
|
7
8
|
[](https://api.reuse.software/info/github.com/SAP/ui5-logger)
|
|
8
|
-
[](https://dev.azure.com/sap/opensource/_build/latest?definitionId=37&branchName=main)
|
|
9
10
|
[](https://www.npmjs.com/package/@ui5/logger)
|
|
11
|
+
[](https://coveralls.io/github/SAP/ui5-logger)
|
|
10
12
|
|
|
11
13
|
## Documentation
|
|
12
|
-
General
|
|
14
|
+
General UI5 Tooling documentation can be found here: [sap.github.io/ui5-tooling](https://sap.github.io/ui5-tooling/)
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
Used for logging purposes across the UI5 Tooling and plugins. Features log levels and progress logging. Basically, it is a wrapper of [npmlog](https://github.com/npm/npmlog).
|
|
16
|
+
The UI5 Logger API Reference can be found here: [`@ui5/logger`](https://sap.github.io/ui5-tooling/v3/api/module-@ui5_logger.html)
|
|
16
17
|
|
|
17
18
|
## Contributing
|
|
18
|
-
Please check our [Contribution Guidelines](https://github.com/SAP/ui5-tooling/blob/
|
|
19
|
+
Please check our [Contribution Guidelines](https://github.com/SAP/ui5-tooling/blob/main/CONTRIBUTING.md).
|
|
19
20
|
|
|
20
21
|
## Support
|
|
21
|
-
Please follow our [Contribution Guidelines](https://github.com/SAP/ui5-tooling/blob/
|
|
22
|
+
Please follow our [Contribution Guidelines](https://github.com/SAP/ui5-tooling/blob/main/CONTRIBUTING.md#report-an-issue) on how to report an issue.
|
|
22
23
|
|
|
23
24
|
Please report issues in the main [UI5 Tooling](https://github.com/SAP/ui5-tooling) repository.
|
|
24
25
|
|
package/jsdoc.json
CHANGED
|
@@ -1,60 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
2
|
+
"tags": {
|
|
3
|
+
"allowUnknownTags": false
|
|
4
|
+
},
|
|
5
|
+
"source": {
|
|
6
|
+
"include": ["README.md"],
|
|
7
|
+
"includePattern": ".+\\.js$",
|
|
8
|
+
"excludePattern": "(node_modules(\\\\|/))"
|
|
9
|
+
},
|
|
10
|
+
"plugins": [
|
|
11
|
+
"jsdoc-plugin.cjs"
|
|
12
|
+
],
|
|
13
|
+
"opts": {
|
|
14
|
+
"encoding": "utf8",
|
|
15
|
+
"destination": "jsdocs/",
|
|
16
|
+
"recurse": true,
|
|
17
|
+
"verbose": true,
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"templates": {
|
|
21
|
+
"cleverLinks": false,
|
|
22
|
+
"monospaceLinks": false,
|
|
23
|
+
"default": {
|
|
24
|
+
"useLongnameInNav": true
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"openGraph": {
|
|
28
|
+
"title": "UI5 Tooling - API Reference",
|
|
29
|
+
"type": "website",
|
|
30
|
+
"image": "https://sap.github.io/ui5-tooling/v3/images/UI5_logo_wide.png",
|
|
31
|
+
"site_name": "UI5 Tooling - API Reference",
|
|
32
|
+
"url": "https://sap.github.io/ui5-tooling/"
|
|
33
|
+
},
|
|
34
|
+
"docdash": {
|
|
35
|
+
"sectionOrder": [
|
|
36
|
+
"Modules",
|
|
37
|
+
"Namespaces",
|
|
38
|
+
"Classes",
|
|
39
|
+
"Externals",
|
|
40
|
+
"Events",
|
|
41
|
+
"Mixins",
|
|
42
|
+
"Tutorials",
|
|
43
|
+
"Interfaces"
|
|
44
|
+
],
|
|
45
|
+
"meta": {
|
|
46
|
+
"title": "UI5 Tooling - API Reference - UI5 Logger",
|
|
47
|
+
"description": "UI5 Tooling - API Reference - UI5 Logger",
|
|
48
|
+
"keyword": "openui5 sapui5 ui5 build development tool api reference"
|
|
49
|
+
},
|
|
50
|
+
"search": true,
|
|
51
|
+
"wrap": true,
|
|
52
|
+
"menu": {
|
|
53
|
+
"GitHub": {
|
|
54
|
+
"href": "https://github.com/SAP/ui5-logger",
|
|
55
|
+
"target": "_blank",
|
|
56
|
+
"class": "menu-item",
|
|
57
|
+
"id": "github_link"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
60
61
|
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Logger from "./loggers/Logger.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interface for the UI5 Tooling logging module
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
* @module @ui5/logger
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Convenience function to create an instance of [@ui5/logger/Logger]{@link @ui5/logger/Logger}
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
* @param {string} moduleName Identifier for messages created by the logger.
|
|
15
|
+
* Example: <code>module:submodule:Class</code>
|
|
16
|
+
* @returns {@ui5/logger/Logger}
|
|
17
|
+
*/
|
|
18
|
+
export function getLogger(moduleName) {
|
|
19
|
+
return new Logger(moduleName);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Tests whether the provided log level is enabled by the current log level
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
* @function
|
|
27
|
+
* @param {string} levelName Log level to test
|
|
28
|
+
* @returns {boolean} True if the provided level is enabled
|
|
29
|
+
*/
|
|
30
|
+
export const isLogLevelEnabled = Logger.isLevelEnabled;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Sets the standard log level.
|
|
34
|
+
* <br>
|
|
35
|
+
* <b>Example:</b> Setting it to <code>perf</code> would suppress all <code>silly</code> and <code>verbose</code>
|
|
36
|
+
* logging, and only show <code>perf</code>, <code>info</code>, <code>warn</code> and <code>error</code> logs.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
* @function
|
|
40
|
+
* @param {string} levelName New log level
|
|
41
|
+
*/
|
|
42
|
+
export const setLogLevel = Logger.setLevel;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Gets the current log level
|
|
46
|
+
*
|
|
47
|
+
* @public
|
|
48
|
+
* @function
|
|
49
|
+
* @returns {string} The current log level. Defaults to <code>info</code>
|
|
50
|
+
*/
|
|
51
|
+
export const getLogLevel = Logger.getLevel;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import Logger from "./Logger.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Logger for emitting status events on the progress a UI5 Tooling build process.
|
|
5
|
+
* <br><br>
|
|
6
|
+
* Emits <code>ui5.log</code>, <code>ui5.build-metadata</code> and <code>ui5.build-status</code> events on the
|
|
7
|
+
* [<code>process</code>]{@link https://nodejs.org/api/process.html} object, which can which can be handled
|
|
8
|
+
* by dedicated writers, like [@ui5/logger/writers/Console]{@link @ui5/logger/writers/Console}.
|
|
9
|
+
* <br><br>
|
|
10
|
+
* If no listener is attached to the the event, messages are written directly to the <code>process.stderr</code> stream.
|
|
11
|
+
*
|
|
12
|
+
* @private
|
|
13
|
+
* @class
|
|
14
|
+
* @alias @ui5/logger/loggers/Build
|
|
15
|
+
*/
|
|
16
|
+
class Build extends Logger {
|
|
17
|
+
#projectsToBuild;
|
|
18
|
+
|
|
19
|
+
static BUILD_METADATA_EVENT_NAME = "ui5.build-metadata";
|
|
20
|
+
static BUILD_STATUS_EVENT_NAME = "ui5.build-status";
|
|
21
|
+
|
|
22
|
+
setProjects(projects) {
|
|
23
|
+
if (!projects || !Array.isArray(projects)) {
|
|
24
|
+
throw new Error("loggers/Build#setProjects: Missing or incorrect projects parameter");
|
|
25
|
+
}
|
|
26
|
+
this.#projectsToBuild = projects;
|
|
27
|
+
|
|
28
|
+
this._emit(Build.BUILD_METADATA_EVENT_NAME, {
|
|
29
|
+
projectsToBuild: projects
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
startProjectBuild(projectName, projectType) {
|
|
34
|
+
if (!this.#projectsToBuild || !this.#projectsToBuild.includes(projectName)) {
|
|
35
|
+
throw new Error(`loggers/Build#startProjectBuild: Unknown project ${projectName}`);
|
|
36
|
+
}
|
|
37
|
+
if (!projectType) {
|
|
38
|
+
throw new Error(`loggers/Build#startProjectBuild: Missing projectType parameter`);
|
|
39
|
+
}
|
|
40
|
+
const level = "info";
|
|
41
|
+
const hasListeners = this._emit(Build.BUILD_STATUS_EVENT_NAME, {
|
|
42
|
+
level,
|
|
43
|
+
projectName,
|
|
44
|
+
projectType,
|
|
45
|
+
status: "project-build-start",
|
|
46
|
+
});
|
|
47
|
+
if (!hasListeners) {
|
|
48
|
+
this._log(level, `Building ${projectType} project ${projectName}...`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
endProjectBuild(projectName, projectType) {
|
|
53
|
+
if (!this.#projectsToBuild || !this.#projectsToBuild.includes(projectName)) {
|
|
54
|
+
throw new Error(`loggers/Build#endProjectBuild: Unknown project ${projectName}`);
|
|
55
|
+
}
|
|
56
|
+
if (!projectType) {
|
|
57
|
+
throw new Error(`loggers/Build#endProjectBuild: Missing projectType parameter`);
|
|
58
|
+
}
|
|
59
|
+
const level = "verbose";
|
|
60
|
+
const hasListeners = this._emit(Build.BUILD_STATUS_EVENT_NAME, {
|
|
61
|
+
level,
|
|
62
|
+
projectName,
|
|
63
|
+
projectType,
|
|
64
|
+
status: "project-build-end",
|
|
65
|
+
});
|
|
66
|
+
if (!hasListeners) {
|
|
67
|
+
this._log(level, `Finished building ${projectType} project ${projectName}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
skipProjectBuild(projectName, projectType) {
|
|
72
|
+
if (!this.#projectsToBuild || !this.#projectsToBuild.includes(projectName)) {
|
|
73
|
+
throw new Error(`loggers/Build#skipProjectBuild: Unknown project ${projectName}`);
|
|
74
|
+
}
|
|
75
|
+
if (!projectType) {
|
|
76
|
+
throw new Error(`loggers/Build#skipProjectBuild: Missing projectType parameter`);
|
|
77
|
+
}
|
|
78
|
+
const level = "info";
|
|
79
|
+
const hasListeners = this._emit(Build.BUILD_STATUS_EVENT_NAME, {
|
|
80
|
+
level,
|
|
81
|
+
projectName,
|
|
82
|
+
projectType,
|
|
83
|
+
status: "project-build-skip",
|
|
84
|
+
});
|
|
85
|
+
if (!hasListeners) {
|
|
86
|
+
this._log(level, `Skipping build of ${projectType} project ${projectName}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export default Build;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import process from "node:process";
|
|
2
|
+
import {inspect} from "node:util";
|
|
3
|
+
|
|
4
|
+
// Module name must not contain any other characters than alphanumerical and some specials
|
|
5
|
+
const rIllegalModuleNameChars = /[^0-9a-zA-Z-_:@./]/i;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Standard logging module for UI5 Tooling and extensions.
|
|
9
|
+
* <br><br>
|
|
10
|
+
* Emits <code>ui5.log</code> events on the [<code>process</code>]{@link https://nodejs.org/api/process.html} object,
|
|
11
|
+
* which can be handled by dedicated writers,
|
|
12
|
+
* like [@ui5/logger/writers/Console]{@link @ui5/logger/writers/Console}.
|
|
13
|
+
* <br><br>
|
|
14
|
+
* If no listener is attached to an event, messages are written directly to the <code>process.stderr</code> stream.
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
* @class
|
|
18
|
+
* @alias @ui5/logger/Logger
|
|
19
|
+
*/
|
|
20
|
+
class Logger {
|
|
21
|
+
/**
|
|
22
|
+
* Available log levels, ordered by priority:
|
|
23
|
+
* <br>
|
|
24
|
+
* <ol>
|
|
25
|
+
* <li>silly</li>
|
|
26
|
+
* <li>verbose</li>
|
|
27
|
+
* <li>perf</li>
|
|
28
|
+
* <li>info <i>(default)</i></li>
|
|
29
|
+
* <li>warn</li>
|
|
30
|
+
* <li>error</li>
|
|
31
|
+
* <li>silent</li>
|
|
32
|
+
* </ol>
|
|
33
|
+
*
|
|
34
|
+
* Log level <code>silent</code> is special in the sense that no messages can be submitted with that level.
|
|
35
|
+
* It can be used to suppress all logging.
|
|
36
|
+
*
|
|
37
|
+
* @member {string[]}
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
static LOG_LEVELS = ["silly", "verbose", "perf", "info", "warn", "error", "silent"];
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Event name used for emitting new log-message event on the
|
|
44
|
+
* [<code>process</code>]{@link https://nodejs.org/api/process.html} object
|
|
45
|
+
*
|
|
46
|
+
* @member {string}
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
static LOG_EVENT_NAME = "ui5.log";
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sets the standard log level.
|
|
53
|
+
* <br>
|
|
54
|
+
* <b>Example:</b> Setting it to <code>perf</code> would suppress all <code>silly</code> and <code>verbose</code>
|
|
55
|
+
* logging, and only show <code>perf</code>, <code>info</code>, <code>warn</code> and <code>error</code> logs.
|
|
56
|
+
*
|
|
57
|
+
* @public
|
|
58
|
+
* @param {string} levelName New log level
|
|
59
|
+
*/
|
|
60
|
+
static setLevel(levelName) {
|
|
61
|
+
process.env.UI5_LOG_LVL = levelName;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Gets the current log level
|
|
66
|
+
*
|
|
67
|
+
* @public
|
|
68
|
+
* @returns {string} The current log level. Defaults to <code>info</code>
|
|
69
|
+
*/
|
|
70
|
+
static getLevel() {
|
|
71
|
+
if (process.env.UI5_LOG_LVL) {
|
|
72
|
+
// Check whether set log level is valid
|
|
73
|
+
const levelName = process.env.UI5_LOG_LVL;
|
|
74
|
+
if (!Logger.LOG_LEVELS.includes(levelName)) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`UI5 Logger: Environment variable UI5_LOG_LVL is set to an unknown log level "${levelName}". ` +
|
|
77
|
+
`Valid levels are ${Logger.LOG_LEVELS.join(", ")}`);
|
|
78
|
+
}
|
|
79
|
+
return levelName;
|
|
80
|
+
} else {
|
|
81
|
+
return "info";
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Tests whether the provided log level is enabled by the current log level
|
|
87
|
+
*
|
|
88
|
+
* @public
|
|
89
|
+
* @param {string} levelName Log level to test
|
|
90
|
+
* @returns {boolean} True if the provided level is enabled
|
|
91
|
+
*/
|
|
92
|
+
static isLevelEnabled(levelName) {
|
|
93
|
+
const currIdx = Logger.LOG_LEVELS.indexOf(Logger.getLevel());
|
|
94
|
+
const reqIdx = Logger.LOG_LEVELS.indexOf(levelName);
|
|
95
|
+
if (reqIdx === -1) {
|
|
96
|
+
throw new Error(`Unknown log level "${levelName}"`);
|
|
97
|
+
}
|
|
98
|
+
return reqIdx >= currIdx;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Formats a given parameter into a string
|
|
103
|
+
*
|
|
104
|
+
* @param {any} message Single log message parameter passed by a program
|
|
105
|
+
* @returns {string} String representation for the given message
|
|
106
|
+
*/
|
|
107
|
+
static _formatMessage(message) {
|
|
108
|
+
if (typeof message === "string" || message instanceof String) {
|
|
109
|
+
return message;
|
|
110
|
+
}
|
|
111
|
+
return inspect(message, {
|
|
112
|
+
depth: 3,
|
|
113
|
+
compact: 2,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
#moduleName;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
*
|
|
121
|
+
*
|
|
122
|
+
* @public
|
|
123
|
+
* @param {string} moduleName Identifier for messages created by this logger.
|
|
124
|
+
* Example: <code>module:submodule:Class</code>
|
|
125
|
+
*/
|
|
126
|
+
constructor(moduleName) {
|
|
127
|
+
if (!moduleName) {
|
|
128
|
+
throw new Error("Logger: Missing moduleName parameter");
|
|
129
|
+
}
|
|
130
|
+
if (rIllegalModuleNameChars.test(moduleName)) {
|
|
131
|
+
throw new Error(`Logger: Invalid module name: ${moduleName}`);
|
|
132
|
+
}
|
|
133
|
+
this.#moduleName = moduleName;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Tests whether the provided log level is enabled by the current log level
|
|
138
|
+
*
|
|
139
|
+
* @public
|
|
140
|
+
* @param {string} levelName Log level to test
|
|
141
|
+
* @returns {boolean} True if the provided level is enabled
|
|
142
|
+
*/
|
|
143
|
+
isLevelEnabled(levelName) {
|
|
144
|
+
return Logger.isLevelEnabled(levelName);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_emit(eventName, payload) {
|
|
148
|
+
return process.emit(eventName, payload);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
_log(level, message) {
|
|
152
|
+
if (this.isLevelEnabled(level)) {
|
|
153
|
+
process.stderr.write(`[${level}] ${message}\n`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
_emitOrLog(level, message) {
|
|
158
|
+
const hasListeners = this._emit(Logger.LOG_EVENT_NAME, {
|
|
159
|
+
level,
|
|
160
|
+
message,
|
|
161
|
+
moduleName: this.#moduleName,
|
|
162
|
+
});
|
|
163
|
+
if (!hasListeners) {
|
|
164
|
+
this._log(level, `${this.#moduleName}: ${message}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Create a log entry with the <code>silly</code> level
|
|
171
|
+
*
|
|
172
|
+
* @public
|
|
173
|
+
* @name @ui5/logger/Logger#silly
|
|
174
|
+
* @function
|
|
175
|
+
* @memberof @ui5/logger/Logger
|
|
176
|
+
* @param {...any} message Messages to log. An automatic string conversion is applied if necessary
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Create a log entry with the <code>verbose</code> level
|
|
181
|
+
*
|
|
182
|
+
* @public
|
|
183
|
+
* @name @ui5/logger/Logger#verbose
|
|
184
|
+
* @function
|
|
185
|
+
* @memberof @ui5/logger/Logger
|
|
186
|
+
* @param {...any} message Messages to log. An automatic string conversion is applied if necessary
|
|
187
|
+
*/
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Create a log entry with the <code>perf</code> level
|
|
191
|
+
*
|
|
192
|
+
* @public
|
|
193
|
+
* @name @ui5/logger/Logger#perf
|
|
194
|
+
* @function
|
|
195
|
+
* @memberof @ui5/logger/Logger
|
|
196
|
+
* @param {...any} message Messages to log. An automatic string conversion is applied if necessary
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Create a log entry with the <code>info</code> level
|
|
201
|
+
*
|
|
202
|
+
* @public
|
|
203
|
+
* @name @ui5/logger/Logger#info
|
|
204
|
+
* @function
|
|
205
|
+
* @memberof @ui5/logger/Logger
|
|
206
|
+
* @param {...any} message Messages to log. An automatic string conversion is applied if necessary
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Create a log entry with the <code>warn</code> level
|
|
211
|
+
*
|
|
212
|
+
* @public
|
|
213
|
+
* @name @ui5/logger/Logger#warn
|
|
214
|
+
* @function
|
|
215
|
+
* @memberof @ui5/logger/Logger
|
|
216
|
+
* @param {...any} message Messages to log. An automatic string conversion is applied if necessary
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Create a log entry with the <code>error</code> level
|
|
221
|
+
*
|
|
222
|
+
* @public
|
|
223
|
+
* @name @ui5/logger/Logger#error
|
|
224
|
+
* @function
|
|
225
|
+
* @memberof @ui5/logger/Logger
|
|
226
|
+
* @param {...any} message Messages to log. An automatic string conversion is applied if necessary
|
|
227
|
+
*/
|
|
228
|
+
|
|
229
|
+
Logger.LOG_LEVELS.forEach((logLevel) => {
|
|
230
|
+
if (logLevel === "silent") {
|
|
231
|
+
// This level is to suppress any logging. Hence we do not provide a dedicated log-function
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
Logger.prototype[logLevel] = function(...args) {
|
|
235
|
+
const message = args.map(Logger._formatMessage).join(" ");
|
|
236
|
+
this._emitOrLog(logLevel, message);
|
|
237
|
+
};
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
export default Logger;
|