@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 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.
@@ -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,6 @@
1
+ export interface LogProcessor {
2
+ logInfo(message: string): Promise<void>;
3
+ logWarn(message: string): Promise<void>;
4
+ logError(message: string): Promise<void>;
5
+ logDebug(message: string): Promise<void>;
6
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export default class LogError extends Error {
2
+ }
@@ -0,0 +1,2 @@
1
+ export default class LogError extends Error {
2
+ }
@@ -0,0 +1,4 @@
1
+ import LogError from './LogError';
2
+ export default class UnknownImplementation extends LogError {
3
+ constructor(name: string);
4
+ }
@@ -0,0 +1,6 @@
1
+ import LogError from './LogError';
2
+ export default class UnknownImplementation extends LogError {
3
+ constructor(name) {
4
+ super(`Unknown logger implementation: ${name}`);
5
+ }
6
+ }
@@ -0,0 +1,3 @@
1
+ import type { LogProcessor } from './definitions/interfaces';
2
+ declare const _default: LogProcessor;
3
+ export default _default;
@@ -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,2 @@
1
+ import Console from './Console';
2
+ export default function create(): Console;
@@ -0,0 +1,4 @@
1
+ import Console from './Console';
2
+ export default function create() {
3
+ return new Console();
4
+ }
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ import Void from './Void';
2
+ export default function create(): Void;
@@ -0,0 +1,4 @@
1
+ import Void from './Void';
2
+ export default function create() {
3
+ return new Void();
4
+ }
@@ -0,0 +1,3 @@
1
+ import Logger from './Logger';
2
+ declare const logger: Logger;
3
+ export default logger;
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import Logger from './Logger';
2
+ import implementation from './implementation';
3
+ const debugEnabled = process.env.LOGGING_DEBUG_ENABLED === 'true';
4
+ const logger = new Logger(implementation, debugEnabled);
5
+ export default logger;
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
+ }