@theshelf/logging 0.1.0 → 0.2.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 +52 -16
- package/dist/Logger.d.ts +8 -4
- package/dist/Logger.js +32 -13
- package/dist/definitions/constants.d.ts +8 -0
- package/dist/definitions/constants.js +7 -0
- package/dist/definitions/interfaces.d.ts +3 -2
- package/dist/{implementations/console → drivers}/Console.d.ts +4 -3
- package/dist/{implementations/console → drivers}/Console.js +6 -2
- package/dist/drivers/Database.d.ts +12 -0
- package/dist/drivers/Database.js +31 -0
- package/dist/drivers/Memory.d.ts +17 -0
- package/dist/drivers/Memory.js +23 -0
- package/dist/{implementations/void → drivers}/Void.d.ts +4 -3
- package/dist/{implementations/void → drivers}/Void.js +5 -5
- package/dist/errors/LogError.d.ts +1 -0
- package/dist/errors/LogError.js +7 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.js +7 -5
- package/package.json +14 -3
- package/dist/errors/UnknownImplementation.d.ts +0 -4
- package/dist/errors/UnknownImplementation.js +0 -6
- package/dist/implementation.d.ts +0 -3
- package/dist/implementation.js +0 -14
- package/dist/implementations/console/create.d.ts +0 -2
- package/dist/implementations/console/create.js +0 -4
- package/dist/implementations/void/create.d.ts +0 -2
- package/dist/implementations/void/create.js +0 -4
package/README.md
CHANGED
|
@@ -9,36 +9,72 @@ The logging package provides a universal interaction layer with an actual loggin
|
|
|
9
9
|
npm install @theshelf/logging
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Drivers
|
|
13
13
|
|
|
14
|
-
Currently, there are
|
|
14
|
+
Currently, there are three drivers available:
|
|
15
15
|
|
|
16
|
-
* **Void** - dummy
|
|
17
|
-
* **
|
|
16
|
+
* **Void** - dummy driver that doesn't log anything (suited for testing).
|
|
17
|
+
* **Memory** - in memory logging (suited for testing).
|
|
18
|
+
* **Console** - driver based on the Node.js console.
|
|
19
|
+
* **Database** - driver that logs messages into a database.
|
|
18
20
|
|
|
19
|
-
##
|
|
21
|
+
## How to use
|
|
22
|
+
|
|
23
|
+
The basic set up looks like this.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import Logger, { VoidDriver | MemoryDriver | ConsoleDriver as SelectedDriver } from '@theshelf/logging';
|
|
20
27
|
|
|
21
|
-
|
|
28
|
+
const driver = new SelectedDriver(/* configuration */);
|
|
29
|
+
const logger = new Logger(driver);
|
|
22
30
|
|
|
23
|
-
|
|
24
|
-
LOGGING_IMPLEMENTATION="console" # (void | console)
|
|
25
|
-
LOGGING_DEBUG_ENABLED=true
|
|
31
|
+
// Perform operations with the logger instance
|
|
26
32
|
```
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
### Configuration
|
|
29
35
|
|
|
30
|
-
|
|
36
|
+
#### Logger
|
|
37
|
+
|
|
38
|
+
The logger has a configurable log level.
|
|
31
39
|
|
|
32
40
|
```ts
|
|
33
|
-
import
|
|
41
|
+
import { LogLevels } from '@theshelf/logging';
|
|
34
42
|
|
|
35
|
-
|
|
43
|
+
logger.logLevel = LogLevels.DEBUG; // default level
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Other levels are: `INFO` | `WARN` | `ERROR` | `FATAL`.
|
|
47
|
+
|
|
48
|
+
#### Void driver
|
|
49
|
+
|
|
50
|
+
No configuration options.
|
|
51
|
+
|
|
52
|
+
#### Memory driver
|
|
53
|
+
|
|
54
|
+
No configuration options.
|
|
55
|
+
|
|
56
|
+
#### Console driver
|
|
57
|
+
|
|
58
|
+
No configuration options.
|
|
59
|
+
|
|
60
|
+
#### Database driver
|
|
61
|
+
|
|
62
|
+
The database driver requires a [database](https://github.com/MaskingTechnology/theshelf/tree/main/packages/database) from @theshelf and a table name (record type) to log messages into.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { database } from './your-database-instance';
|
|
66
|
+
|
|
67
|
+
const recordType = 'logs';
|
|
68
|
+
const driver = new DatabaseDriver(database, recordType);
|
|
69
|
+
|
|
70
|
+
const logger = new Logger(driver);
|
|
36
71
|
```
|
|
37
72
|
|
|
38
73
|
### Operations
|
|
39
74
|
|
|
40
75
|
```ts
|
|
41
|
-
|
|
76
|
+
// Log debug
|
|
77
|
+
await logger.logDebug(message);
|
|
42
78
|
|
|
43
79
|
// Log info
|
|
44
80
|
await logger.logInfo(message);
|
|
@@ -49,8 +85,8 @@ await logger.logWarn(message);
|
|
|
49
85
|
// Log error
|
|
50
86
|
await logger.logError(message);
|
|
51
87
|
|
|
52
|
-
// Log
|
|
53
|
-
await logger.
|
|
88
|
+
// Log fatal
|
|
89
|
+
await logger.logFatal(message);
|
|
54
90
|
|
|
55
91
|
// Log multiple messages (works for all levels)
|
|
56
92
|
await logger.logInfo(message1, message2, ...);
|
package/dist/Logger.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { LogLevel } from './definitions/constants.js';
|
|
2
|
+
import type { Driver } from './definitions/interfaces.js';
|
|
3
|
+
export default class Logger implements Driver {
|
|
3
4
|
#private;
|
|
4
|
-
constructor(
|
|
5
|
+
constructor(driver: Driver);
|
|
6
|
+
set logLevel(level: LogLevel);
|
|
7
|
+
get logLevel(): LogLevel;
|
|
8
|
+
logDebug(...message: unknown[]): Promise<void>;
|
|
5
9
|
logInfo(...message: unknown[]): Promise<void>;
|
|
6
10
|
logWarn(...message: unknown[]): Promise<void>;
|
|
7
11
|
logError(...message: unknown[]): Promise<void>;
|
|
8
|
-
|
|
12
|
+
logFatal(...message: unknown[]): Promise<void>;
|
|
9
13
|
}
|
package/dist/Logger.js
CHANGED
|
@@ -1,28 +1,47 @@
|
|
|
1
|
+
import { LogLevels } from './definitions/constants.js';
|
|
1
2
|
export default class Logger {
|
|
2
|
-
#
|
|
3
|
-
#
|
|
4
|
-
constructor(
|
|
5
|
-
this.#
|
|
6
|
-
|
|
3
|
+
#driver;
|
|
4
|
+
#logLevel = LogLevels.DEBUG;
|
|
5
|
+
constructor(driver) {
|
|
6
|
+
this.#driver = driver;
|
|
7
|
+
}
|
|
8
|
+
set logLevel(level) {
|
|
9
|
+
this.#logLevel = level;
|
|
10
|
+
}
|
|
11
|
+
get logLevel() {
|
|
12
|
+
return this.#logLevel;
|
|
13
|
+
}
|
|
14
|
+
logDebug(...message) {
|
|
15
|
+
if (this.#logLevel > LogLevels.DEBUG) {
|
|
16
|
+
return Promise.resolve();
|
|
17
|
+
}
|
|
18
|
+
const messageString = this.#createMessage(message);
|
|
19
|
+
return this.#driver.logDebug(messageString);
|
|
7
20
|
}
|
|
8
21
|
logInfo(...message) {
|
|
22
|
+
if (this.#logLevel > LogLevels.INFO) {
|
|
23
|
+
return Promise.resolve();
|
|
24
|
+
}
|
|
9
25
|
const messageString = this.#createMessage(message);
|
|
10
|
-
return this.#
|
|
26
|
+
return this.#driver.logInfo(messageString);
|
|
11
27
|
}
|
|
12
28
|
logWarn(...message) {
|
|
29
|
+
if (this.#logLevel > LogLevels.WARN) {
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
}
|
|
13
32
|
const messageString = this.#createMessage(message);
|
|
14
|
-
return this.#
|
|
33
|
+
return this.#driver.logWarn(messageString);
|
|
15
34
|
}
|
|
16
35
|
logError(...message) {
|
|
17
|
-
|
|
18
|
-
return this.#processor.logError(messageString);
|
|
19
|
-
}
|
|
20
|
-
logDebug(...message) {
|
|
21
|
-
if (this.#debugEnabled === false) {
|
|
36
|
+
if (this.#logLevel > LogLevels.ERROR) {
|
|
22
37
|
return Promise.resolve();
|
|
23
38
|
}
|
|
24
39
|
const messageString = this.#createMessage(message);
|
|
25
|
-
return this.#
|
|
40
|
+
return this.#driver.logError(messageString);
|
|
41
|
+
}
|
|
42
|
+
logFatal(...message) {
|
|
43
|
+
const messageString = this.#createMessage(message);
|
|
44
|
+
return this.#driver.logFatal(messageString);
|
|
26
45
|
}
|
|
27
46
|
#createMessage(message) {
|
|
28
47
|
return message.map(value => this.#interpretValue(value)).join(' ');
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface Driver {
|
|
2
|
+
logDebug(message: string): Promise<void>;
|
|
2
3
|
logInfo(message: string): Promise<void>;
|
|
3
4
|
logWarn(message: string): Promise<void>;
|
|
4
5
|
logError(message: string): Promise<void>;
|
|
5
|
-
|
|
6
|
+
logFatal(message: string): Promise<void>;
|
|
6
7
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export default class Console implements
|
|
1
|
+
import type { Driver } from '../definitions/interfaces.js';
|
|
2
|
+
export default class Console implements Driver {
|
|
3
|
+
logDebug(message: string): Promise<void>;
|
|
3
4
|
logInfo(message: string): Promise<void>;
|
|
4
5
|
logWarn(message: string): Promise<void>;
|
|
5
6
|
logError(message: string): Promise<void>;
|
|
6
|
-
|
|
7
|
+
logFatal(message: string): Promise<void>;
|
|
7
8
|
}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
/* eslint no-console: "off" */
|
|
1
2
|
export default class Console {
|
|
3
|
+
async logDebug(message) {
|
|
4
|
+
return console.debug(message);
|
|
5
|
+
}
|
|
2
6
|
async logInfo(message) {
|
|
3
7
|
return console.info(message);
|
|
4
8
|
}
|
|
@@ -8,7 +12,7 @@ export default class Console {
|
|
|
8
12
|
async logError(message) {
|
|
9
13
|
return console.error(message);
|
|
10
14
|
}
|
|
11
|
-
async
|
|
12
|
-
return console.
|
|
15
|
+
async logFatal(message) {
|
|
16
|
+
return console.error(message);
|
|
13
17
|
}
|
|
14
18
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type Database from '@theshelf/database';
|
|
2
|
+
import type { RecordType } from '@theshelf/database';
|
|
3
|
+
import type { Driver } from '../definitions/interfaces.js';
|
|
4
|
+
export default class DatabaseDriver implements Driver {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(database: Database, recordType: RecordType);
|
|
7
|
+
logDebug(message: string): Promise<void>;
|
|
8
|
+
logInfo(message: string): Promise<void>;
|
|
9
|
+
logWarn(message: string): Promise<void>;
|
|
10
|
+
logError(message: string): Promise<void>;
|
|
11
|
+
logFatal(message: string): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export default class DatabaseDriver {
|
|
2
|
+
#database;
|
|
3
|
+
#recordType;
|
|
4
|
+
constructor(database, recordType) {
|
|
5
|
+
this.#recordType = recordType;
|
|
6
|
+
this.#database = database;
|
|
7
|
+
}
|
|
8
|
+
logDebug(message) {
|
|
9
|
+
return this.#logRecord('DEBUG', message);
|
|
10
|
+
}
|
|
11
|
+
logInfo(message) {
|
|
12
|
+
return this.#logRecord('INFO', message);
|
|
13
|
+
}
|
|
14
|
+
logWarn(message) {
|
|
15
|
+
return this.#logRecord('WARN', message);
|
|
16
|
+
}
|
|
17
|
+
logError(message) {
|
|
18
|
+
return this.#logRecord('ERROR', message);
|
|
19
|
+
}
|
|
20
|
+
logFatal(message) {
|
|
21
|
+
return this.#logRecord('FATAL', message);
|
|
22
|
+
}
|
|
23
|
+
async #logRecord(level, message) {
|
|
24
|
+
const data = {
|
|
25
|
+
timestamp: new Date().toISOString(),
|
|
26
|
+
level,
|
|
27
|
+
message
|
|
28
|
+
};
|
|
29
|
+
await this.#database.createRecord(this.#recordType, data);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { LogLevel } from '../definitions/constants.js';
|
|
2
|
+
import type { Driver } from '../definitions/interfaces.js';
|
|
3
|
+
type Log = {
|
|
4
|
+
level: LogLevel;
|
|
5
|
+
message: string;
|
|
6
|
+
};
|
|
7
|
+
export default class Memory implements Driver {
|
|
8
|
+
#private;
|
|
9
|
+
get logs(): Log[];
|
|
10
|
+
logDebug(message: string): Promise<void>;
|
|
11
|
+
logInfo(message: string): Promise<void>;
|
|
12
|
+
logWarn(message: string): Promise<void>;
|
|
13
|
+
logError(message: string): Promise<void>;
|
|
14
|
+
logFatal(message: string): Promise<void>;
|
|
15
|
+
clear(): void;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { LogLevels } from '../definitions/constants.js';
|
|
2
|
+
export default class Memory {
|
|
3
|
+
#logs = [];
|
|
4
|
+
get logs() { return this.#logs; }
|
|
5
|
+
async logDebug(message) {
|
|
6
|
+
this.#logs.push({ level: LogLevels.DEBUG, message });
|
|
7
|
+
}
|
|
8
|
+
async logInfo(message) {
|
|
9
|
+
this.#logs.push({ level: LogLevels.INFO, message });
|
|
10
|
+
}
|
|
11
|
+
async logWarn(message) {
|
|
12
|
+
this.#logs.push({ level: LogLevels.WARN, message });
|
|
13
|
+
}
|
|
14
|
+
async logError(message) {
|
|
15
|
+
this.#logs.push({ level: LogLevels.ERROR, message });
|
|
16
|
+
}
|
|
17
|
+
async logFatal(message) {
|
|
18
|
+
this.#logs.push({ level: LogLevels.FATAL, message });
|
|
19
|
+
}
|
|
20
|
+
clear() {
|
|
21
|
+
this.#logs.splice(0);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export default class Void implements
|
|
1
|
+
import type { Driver } from '../definitions/interfaces.js';
|
|
2
|
+
export default class Void implements Driver {
|
|
3
|
+
logDebug(message: string): Promise<void>;
|
|
3
4
|
logInfo(message: string): Promise<void>;
|
|
4
5
|
logWarn(message: string): Promise<void>;
|
|
5
6
|
logError(message: string): Promise<void>;
|
|
6
|
-
|
|
7
|
+
logFatal(message: string): Promise<void>;
|
|
7
8
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
+
/* eslint @typescript-eslint/no-unused-vars: "off" */
|
|
1
2
|
export default class Void {
|
|
2
|
-
|
|
3
|
+
logDebug(message) {
|
|
4
|
+
return Promise.resolve();
|
|
5
|
+
}
|
|
3
6
|
logInfo(message) {
|
|
4
7
|
return Promise.resolve();
|
|
5
8
|
}
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
9
|
logWarn(message) {
|
|
8
10
|
return Promise.resolve();
|
|
9
11
|
}
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
11
12
|
logError(message) {
|
|
12
13
|
return Promise.resolve();
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
-
logDebug(message) {
|
|
15
|
+
logFatal(message) {
|
|
16
16
|
return Promise.resolve();
|
|
17
17
|
}
|
|
18
18
|
}
|
package/dist/errors/LogError.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export
|
|
1
|
+
export * from './definitions/constants.js';
|
|
2
|
+
export type * from './definitions/constants.js';
|
|
3
|
+
export type * from './definitions/interfaces.js';
|
|
4
|
+
export { default as LogError } from './errors/LogError.js';
|
|
5
|
+
export { default as ConsoleDriver } from './drivers/Console.js';
|
|
6
|
+
export { default as DatabaseDriver } from './drivers/Database.js';
|
|
7
|
+
export { default as MemoryDriver } from './drivers/Memory.js';
|
|
8
|
+
export { default as VoidDriver } from './drivers/Void.js';
|
|
9
|
+
export { default } from './Logger.js';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export default
|
|
1
|
+
export * from './definitions/constants.js';
|
|
2
|
+
export { default as LogError } from './errors/LogError.js';
|
|
3
|
+
export { default as ConsoleDriver } from './drivers/Console.js';
|
|
4
|
+
export { default as DatabaseDriver } from './drivers/Database.js';
|
|
5
|
+
export { default as MemoryDriver } from './drivers/Memory.js';
|
|
6
|
+
export { default as VoidDriver } from './drivers/Void.js';
|
|
7
|
+
export { default } from './Logger.js';
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theshelf/logging",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
|
-
"url": "https://github.com/MaskingTechnology/theshelf"
|
|
7
|
+
"url": "git+https://github.com/MaskingTechnology/theshelf.git"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "tsc",
|
|
11
11
|
"clean": "rimraf dist",
|
|
12
|
+
"test": "vitest run",
|
|
13
|
+
"test-coverage": "vitest run --coverage",
|
|
12
14
|
"lint": "eslint",
|
|
13
15
|
"review": "npm run build && npm run lint",
|
|
14
16
|
"prepublishOnly": "npm run clean && npm run build"
|
|
@@ -18,5 +20,14 @@
|
|
|
18
20
|
"dist"
|
|
19
21
|
],
|
|
20
22
|
"types": "dist/index.d.ts",
|
|
21
|
-
"exports": "./dist/index.js"
|
|
23
|
+
"exports": "./dist/index.js",
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@theshelf/database": "^0.2.1"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"@theshelf/database":
|
|
29
|
+
{
|
|
30
|
+
"optional": true
|
|
31
|
+
}
|
|
32
|
+
}
|
|
22
33
|
}
|
package/dist/implementation.d.ts
DELETED
package/dist/implementation.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import UnknownImplementation from './errors/UnknownImplementation.js';
|
|
2
|
-
import createConsole from './implementations/console/create.js';
|
|
3
|
-
import createVoid from './implementations/void/create.js';
|
|
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();
|