@theshelf/logging 0.0.1
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/README.md +72 -0
- package/dist/Logger.d.ts +9 -0
- package/dist/Logger.js +51 -0
- package/dist/definitions/interfaces.d.ts +6 -0
- package/dist/definitions/interfaces.js +1 -0
- package/dist/errors/LogError.d.ts +2 -0
- package/dist/errors/LogError.js +2 -0
- package/dist/errors/UnknownImplementation.d.ts +4 -0
- package/dist/errors/UnknownImplementation.js +6 -0
- package/dist/implementation.d.ts +3 -0
- package/dist/implementation.js +14 -0
- package/dist/implementations/console/Console.d.ts +7 -0
- package/dist/implementations/console/Console.js +14 -0
- package/dist/implementations/console/create.d.ts +2 -0
- package/dist/implementations/console/create.js +4 -0
- package/dist/implementations/void/Void.d.ts +7 -0
- package/dist/implementations/void/Void.js +18 -0
- package/dist/implementations/void/create.d.ts +2 -0
- package/dist/implementations/void/create.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -0
- package/package.json +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
|
|
2
|
+
# Logging | The Shelf
|
|
3
|
+
|
|
4
|
+
The logging package provides a universal interaction layer with an actual logging solution.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @theshelf/logging
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Implementations
|
|
13
|
+
|
|
14
|
+
Currently, there are two implementations:
|
|
15
|
+
|
|
16
|
+
* **Void** - dummy implementation that doesn't log anything (suited for testing).
|
|
17
|
+
* **Console** - implementation based on the Node.js console.
|
|
18
|
+
|
|
19
|
+
## Configuration
|
|
20
|
+
|
|
21
|
+
The used implementation needs to be configured in the `.env` file with the debug enabled setting.
|
|
22
|
+
|
|
23
|
+
```env
|
|
24
|
+
LOGGING_IMPLEMENTATION="console" # (void | console)
|
|
25
|
+
LOGGING_DEBUG_ENABLED=true
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## How to use
|
|
29
|
+
|
|
30
|
+
An instance of the configured logger implementation can be imported for performing logging operations.
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import logger from '@theshelf/logging';
|
|
34
|
+
|
|
35
|
+
// Perform operations with the logger instance
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Operations
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import logger from '@theshelf/logging';
|
|
42
|
+
|
|
43
|
+
// Log info
|
|
44
|
+
await logger.logInfo(message);
|
|
45
|
+
|
|
46
|
+
// Log warning
|
|
47
|
+
await logger.logWarn(message);
|
|
48
|
+
|
|
49
|
+
// Log error
|
|
50
|
+
await logger.logError(message);
|
|
51
|
+
|
|
52
|
+
// Log debug information
|
|
53
|
+
await logger.logDebug(message);
|
|
54
|
+
|
|
55
|
+
// Log multiple messages (works for all levels)
|
|
56
|
+
await logger.logInfo(message1, message2, ...);
|
|
57
|
+
|
|
58
|
+
// Logging multiple types of values (works for all levels)
|
|
59
|
+
await logger.logInfo('string', new Error('Oops...'), 42, [ 'a', 3.14 ], { name: 'John Doe', age: null });
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Value interpretation
|
|
63
|
+
|
|
64
|
+
Currently, the logger has support for the following types of values:
|
|
65
|
+
|
|
66
|
+
* All primitive types
|
|
67
|
+
* Null / undefined
|
|
68
|
+
* Errors (its stack if available or else its message)
|
|
69
|
+
* Arrays (all values will be interpreted and concatenated with a space between them)
|
|
70
|
+
* Objects (will be stringyfied)
|
|
71
|
+
|
|
72
|
+
In case multiple messages are given, they will be concatenated with a space between them.
|
package/dist/Logger.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { LogProcessor } from './definitions/interfaces';
|
|
2
|
+
export default class Logger implements LogProcessor {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(processor: LogProcessor, debugEnabled?: boolean);
|
|
5
|
+
logInfo(...message: unknown[]): Promise<void>;
|
|
6
|
+
logWarn(...message: unknown[]): Promise<void>;
|
|
7
|
+
logError(...message: unknown[]): Promise<void>;
|
|
8
|
+
logDebug(...message: unknown[]): Promise<void>;
|
|
9
|
+
}
|
package/dist/Logger.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export default class Logger {
|
|
2
|
+
#processor;
|
|
3
|
+
#debugEnabled;
|
|
4
|
+
constructor(processor, debugEnabled = false) {
|
|
5
|
+
this.#processor = processor;
|
|
6
|
+
this.#debugEnabled = debugEnabled;
|
|
7
|
+
}
|
|
8
|
+
logInfo(...message) {
|
|
9
|
+
const messageString = this.#createMessage(message);
|
|
10
|
+
return this.#processor.logInfo(messageString);
|
|
11
|
+
}
|
|
12
|
+
logWarn(...message) {
|
|
13
|
+
const messageString = this.#createMessage(message);
|
|
14
|
+
return this.#processor.logWarn(messageString);
|
|
15
|
+
}
|
|
16
|
+
logError(...message) {
|
|
17
|
+
const messageString = this.#createMessage(message);
|
|
18
|
+
return this.#processor.logError(messageString);
|
|
19
|
+
}
|
|
20
|
+
logDebug(...message) {
|
|
21
|
+
if (this.#debugEnabled === false) {
|
|
22
|
+
return Promise.resolve();
|
|
23
|
+
}
|
|
24
|
+
const messageString = this.#createMessage(message);
|
|
25
|
+
return this.#processor.logDebug(messageString);
|
|
26
|
+
}
|
|
27
|
+
#createMessage(message) {
|
|
28
|
+
return message.map(value => this.#interpretValue(value)).join(' ');
|
|
29
|
+
}
|
|
30
|
+
#interpretValue(value) {
|
|
31
|
+
switch (typeof value) {
|
|
32
|
+
case 'string': return value;
|
|
33
|
+
case 'object': return this.#interpretObject(value);
|
|
34
|
+
case 'undefined': return 'undefined';
|
|
35
|
+
case 'function': return 'function';
|
|
36
|
+
default: return String(value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
#interpretObject(object) {
|
|
40
|
+
if (object === null) {
|
|
41
|
+
return 'null';
|
|
42
|
+
}
|
|
43
|
+
if (Array.isArray(object)) {
|
|
44
|
+
return object.map(value => this.#interpretValue(value)).join(' ');
|
|
45
|
+
}
|
|
46
|
+
if (object instanceof Error) {
|
|
47
|
+
return object.stack ?? object.message;
|
|
48
|
+
}
|
|
49
|
+
return JSON.stringify(object);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import UnknownImplementation from './errors/UnknownImplementation';
|
|
2
|
+
import createConsole from './implementations/console/create';
|
|
3
|
+
import createVoid from './implementations/void/create';
|
|
4
|
+
const implementations = new Map([
|
|
5
|
+
['console', createConsole],
|
|
6
|
+
['void', createVoid]
|
|
7
|
+
]);
|
|
8
|
+
const DEFAULT_LOGGING_IMPLEMENTATION = 'void';
|
|
9
|
+
const implementationName = process.env.LOGGING_IMPLEMENTATION ?? DEFAULT_LOGGING_IMPLEMENTATION;
|
|
10
|
+
const creator = implementations.get(implementationName.toLowerCase());
|
|
11
|
+
if (creator === undefined) {
|
|
12
|
+
throw new UnknownImplementation(implementationName);
|
|
13
|
+
}
|
|
14
|
+
export default creator();
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LogProcessor } from '../../definitions/interfaces';
|
|
2
|
+
export default class Console implements LogProcessor {
|
|
3
|
+
logInfo(message: string): Promise<void>;
|
|
4
|
+
logWarn(message: string): Promise<void>;
|
|
5
|
+
logError(message: string): Promise<void>;
|
|
6
|
+
logDebug(message: string): Promise<void>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default class Console {
|
|
2
|
+
async logInfo(message) {
|
|
3
|
+
return console.info(message);
|
|
4
|
+
}
|
|
5
|
+
async logWarn(message) {
|
|
6
|
+
return console.warn(message);
|
|
7
|
+
}
|
|
8
|
+
async logError(message) {
|
|
9
|
+
return console.error(message);
|
|
10
|
+
}
|
|
11
|
+
async logDebug(message) {
|
|
12
|
+
return console.debug(message);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LogProcessor } from '../../definitions/interfaces';
|
|
2
|
+
export default class Void implements LogProcessor {
|
|
3
|
+
logInfo(message: string): Promise<void>;
|
|
4
|
+
logWarn(message: string): Promise<void>;
|
|
5
|
+
logError(message: string): Promise<void>;
|
|
6
|
+
logDebug(message: string): Promise<void>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default class Void {
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3
|
+
logInfo(message) {
|
|
4
|
+
return Promise.resolve();
|
|
5
|
+
}
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
|
+
logWarn(message) {
|
|
8
|
+
return Promise.resolve();
|
|
9
|
+
}
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
11
|
+
logError(message) {
|
|
12
|
+
return Promise.resolve();
|
|
13
|
+
}
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
15
|
+
logDebug(message) {
|
|
16
|
+
return Promise.resolve();
|
|
17
|
+
}
|
|
18
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@theshelf/logging",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"clean": "rimraf dist",
|
|
9
|
+
"lint": "eslint",
|
|
10
|
+
"review": "npm run build && npm run lint",
|
|
11
|
+
"prepublishOnly": "npm run clean && npm run build"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"README.md",
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"exports": "./dist/index.js"
|
|
19
|
+
}
|