@zamatica/logger-configuration 0.1.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/README.md +76 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/log-settings.d.ts +18 -0
- package/dist/log-settings.d.ts.map +1 -0
- package/dist/log-settings.js +8 -0
- package/dist/logger-configuration.d.ts +24 -0
- package/dist/logger-configuration.d.ts.map +1 -0
- package/dist/logger-configuration.js +65 -0
- package/dist/nestjs-helpers.d.ts +12 -0
- package/dist/nestjs-helpers.d.ts.map +1 -0
- package/dist/nestjs-helpers.js +21 -0
- package/package.json +52 -0
- package/src/index.ts +14 -0
- package/src/log-settings.ts +32 -0
- package/src/logger-configuration.ts +81 -0
- package/src/nestjs-helpers.ts +30 -0
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @zamatica/logger-configuration
|
|
2
|
+
|
|
3
|
+
Configuration parameters and parsing for log levels, prefix, and color toggle. Built on `@zamatica/configuration`. Framework-agnostic core; NestJS-specific helpers behind the `/nestjs` subpath.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @zamatica/logger-configuration
|
|
9
|
+
# Optional: install only if you want the NestJS helper
|
|
10
|
+
bun add @nestjs/common
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Registered configuration parameters
|
|
14
|
+
|
|
15
|
+
Importing `LoggerConfiguration` (decorator side-effect) registers three parameters with `ConfigurationService`:
|
|
16
|
+
|
|
17
|
+
| Key | Type | Default | Purpose |
|
|
18
|
+
|------------------------|---------|-------------------------------|----------------------------------------------------|
|
|
19
|
+
| `LOG_LEVELS` | string | `"fatal,error,warn,info"` | Comma-separated list of visible levels. |
|
|
20
|
+
| `LOG_PREFIX` | string | _falls back to env-var prefix_ | Prefix string for structured loggers. |
|
|
21
|
+
| `DISABLE_LOG_COLORING` | boolean | `false` | Strip ANSI color codes from console output. |
|
|
22
|
+
|
|
23
|
+
Canonical levels: `fatal`, `error`, `warn`, `info`, `debug`, `verbose`. Unknown levels in `LOG_LEVELS` are silently dropped.
|
|
24
|
+
|
|
25
|
+
## Quick example (framework-agnostic)
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { LoggerConfiguration } from '@zamatica/logger-configuration';
|
|
29
|
+
import { ConfigurationService } from '@zamatica/configuration';
|
|
30
|
+
|
|
31
|
+
ConfigurationService.setAppEnvVarPrefix('MYAPP');
|
|
32
|
+
ConfigurationService.initialize();
|
|
33
|
+
|
|
34
|
+
const settings = LoggerConfiguration.parseLogSettings();
|
|
35
|
+
// settings.levels → ['fatal', 'error', 'warn', 'info']
|
|
36
|
+
// settings.prefix → 'myapp'
|
|
37
|
+
// settings.disableColoring → false
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## NestJS subpath
|
|
41
|
+
|
|
42
|
+
For NestJS apps, map canonical levels to NestJS's `LogLevel` (note: NestJS uses `'log'` for info-level):
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { ConsoleLogger } from '@nestjs/common';
|
|
46
|
+
import { mapToNestJsLogLevels } from '@zamatica/logger-configuration/nestjs';
|
|
47
|
+
|
|
48
|
+
const settings = LoggerConfiguration.parseLogSettings();
|
|
49
|
+
const nestLevels = mapToNestJsLogLevels(settings.levels);
|
|
50
|
+
const logger = new ConsoleLogger({
|
|
51
|
+
prefix: settings.prefix,
|
|
52
|
+
logLevels: nestLevels,
|
|
53
|
+
colors: !settings.disableColoring,
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The `/nestjs` subpath is the only place `NestJsLogLevel` types appear, so non-NestJS consumers don't pull NestJS-shaped names into their type graph.
|
|
58
|
+
|
|
59
|
+
## Testing the library
|
|
60
|
+
|
|
61
|
+
Tests that use this library should re-register its parameters after `ConfigurationService.__resetForTesting()`:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import {
|
|
65
|
+
LOGGER_CONFIGURATION_PARAMETERS,
|
|
66
|
+
} from '@zamatica/logger-configuration';
|
|
67
|
+
|
|
68
|
+
beforeEach(() => {
|
|
69
|
+
ConfigurationService.__resetForTesting();
|
|
70
|
+
ConfigurationService.setAppEnvVarPrefix('TESTAPP');
|
|
71
|
+
ConfigurationService.register(
|
|
72
|
+
[...LOGGER_CONFIGURATION_PARAMETERS],
|
|
73
|
+
'LoggerConfiguration',
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,+BAA+B,GAChC,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,oBAAoB,EACpB,KAAK,iBAAiB,EACtB,KAAK,WAAW,GACjB,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { LoggerConfiguration, LOGGER_CONFIGURATION_PARAMETERS, } from './logger-configuration.js';
|
|
2
|
+
export { CANONICAL_LOG_LEVELS, } from './log-settings.js';
|
|
3
|
+
// NOTE: The NestJS-specific helpers (`mapToNestJsLogLevels`, `NestJsLogLevel`)
|
|
4
|
+
// are intentionally NOT re-exported. Import them from
|
|
5
|
+
// `@zamatica/logger-configuration/nestjs` — that subpath is the only place
|
|
6
|
+
// pulling NestJS-shaped names into a consumer's type graph.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical log levels recognized by `@zamatica/logger-configuration`.
|
|
3
|
+
*
|
|
4
|
+
* Frameworks that use different names (e.g. NestJS uses `'log'` for info-level
|
|
5
|
+
* messages) provide a mapping helper in their respective subpath export
|
|
6
|
+
* (`@zamatica/logger-configuration/nestjs`).
|
|
7
|
+
*/
|
|
8
|
+
export type CanonicalLogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'verbose';
|
|
9
|
+
export declare const CANONICAL_LOG_LEVELS: readonly CanonicalLogLevel[];
|
|
10
|
+
export interface LogSettings {
|
|
11
|
+
/** Visible levels in declared (priority) order. */
|
|
12
|
+
levels: CanonicalLogLevel[];
|
|
13
|
+
/** Prefix string used by structured loggers. Defaults to the app env-var prefix lowercased. */
|
|
14
|
+
prefix: string;
|
|
15
|
+
/** Whether to strip ANSI color codes from console output. */
|
|
16
|
+
disableColoring: boolean;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=log-settings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log-settings.d.ts","sourceRoot":"","sources":["../src/log-settings.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GACzB,OAAO,GACP,OAAO,GACP,MAAM,GACN,MAAM,GACN,OAAO,GACP,SAAS,CAAC;AAEd,eAAO,MAAM,oBAAoB,EAAE,SAAS,iBAAiB,EAOnD,CAAC;AAEX,MAAM,WAAW,WAAW;IAC1B,mDAAmD;IACnD,MAAM,EAAE,iBAAiB,EAAE,CAAC;IAC5B,+FAA+F;IAC/F,MAAM,EAAE,MAAM,CAAC;IACf,6DAA6D;IAC7D,eAAe,EAAE,OAAO,CAAC;CAC1B"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type ConfigurationParameterDefinition } from '@zamatica/configuration';
|
|
2
|
+
import { type LogSettings } from './log-settings.js';
|
|
3
|
+
/**
|
|
4
|
+
* The three configuration parameters owned by this library, exported so that
|
|
5
|
+
* tests can re-register them after `ConfigurationService.__resetForTesting()`
|
|
6
|
+
* clears the registry. The decorator below registers the same list at
|
|
7
|
+
* class-load time for normal runtime use.
|
|
8
|
+
*/
|
|
9
|
+
export declare const LOGGER_CONFIGURATION_PARAMETERS: readonly ConfigurationParameterDefinition[];
|
|
10
|
+
/**
|
|
11
|
+
* Registers `LOG_LEVELS`, `LOG_PREFIX`, and `DISABLE_LOG_COLORING` at module
|
|
12
|
+
* load time. Consumers call `LoggerConfiguration.parseLogSettings()` after
|
|
13
|
+
* `ConfigurationService.initialize()` runs (or any time after registration if
|
|
14
|
+
* the defaults are acceptable).
|
|
15
|
+
*
|
|
16
|
+
* The returned object is plain — frameworks adapt it to their own log-level
|
|
17
|
+
* types via subpath helpers (e.g. the NestJS adapter at
|
|
18
|
+
* `@zamatica/logger-configuration/nestjs`).
|
|
19
|
+
*/
|
|
20
|
+
export declare class LoggerConfiguration {
|
|
21
|
+
/** Reads the registered parameters and returns a normalized settings object. */
|
|
22
|
+
static parseLogSettings(): LogSettings;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=logger-configuration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger-configuration.d.ts","sourceRoot":"","sources":["../src/logger-configuration.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,gCAAgC,EAEtC,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAGL,KAAK,WAAW,EACjB,MAAM,mBAAmB,CAAC;AAE3B;;;;;GAKG;AACH,eAAO,MAAM,+BAA+B,EAAE,SAAS,gCAAgC,EAsBtF,CAAC;AAEF;;;;;;;;;GASG;AACH,qBACa,mBAAmB;IAC9B,gFAAgF;IAChF,MAAM,CAAC,gBAAgB,IAAI,WAAW;CAcvC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { Configuration, ConfigurationParameter, ConfigurationService, } from '@zamatica/configuration';
|
|
3
|
+
import { CANONICAL_LOG_LEVELS, } from './log-settings.js';
|
|
4
|
+
/**
|
|
5
|
+
* The three configuration parameters owned by this library, exported so that
|
|
6
|
+
* tests can re-register them after `ConfigurationService.__resetForTesting()`
|
|
7
|
+
* clears the registry. The decorator below registers the same list at
|
|
8
|
+
* class-load time for normal runtime use.
|
|
9
|
+
*/
|
|
10
|
+
export const LOGGER_CONFIGURATION_PARAMETERS = [
|
|
11
|
+
ConfigurationParameter.named('LOG_LEVELS')
|
|
12
|
+
.string()
|
|
13
|
+
.default('fatal,error,warn,info')
|
|
14
|
+
.description('Comma-separated list of visible log levels: fatal, error, warn, info, debug, verbose')
|
|
15
|
+
.required()
|
|
16
|
+
.build(),
|
|
17
|
+
ConfigurationParameter.named('LOG_PREFIX')
|
|
18
|
+
.string()
|
|
19
|
+
.description('Prefix string used by structured loggers; defaults to the app env-var prefix lowercase')
|
|
20
|
+
.optional()
|
|
21
|
+
.build(),
|
|
22
|
+
ConfigurationParameter.named('DISABLE_LOG_COLORING')
|
|
23
|
+
.boolean()
|
|
24
|
+
.default(false)
|
|
25
|
+
.description('Strip ANSI color codes from console output')
|
|
26
|
+
.optional()
|
|
27
|
+
.build(),
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* Registers `LOG_LEVELS`, `LOG_PREFIX`, and `DISABLE_LOG_COLORING` at module
|
|
31
|
+
* load time. Consumers call `LoggerConfiguration.parseLogSettings()` after
|
|
32
|
+
* `ConfigurationService.initialize()` runs (or any time after registration if
|
|
33
|
+
* the defaults are acceptable).
|
|
34
|
+
*
|
|
35
|
+
* The returned object is plain — frameworks adapt it to their own log-level
|
|
36
|
+
* types via subpath helpers (e.g. the NestJS adapter at
|
|
37
|
+
* `@zamatica/logger-configuration/nestjs`).
|
|
38
|
+
*/
|
|
39
|
+
let LoggerConfiguration = class LoggerConfiguration {
|
|
40
|
+
/** Reads the registered parameters and returns a normalized settings object. */
|
|
41
|
+
static parseLogSettings() {
|
|
42
|
+
const rawLevels = ConfigurationService.get('LOG_LEVELS');
|
|
43
|
+
const levels = parseCanonicalLevels(rawLevels);
|
|
44
|
+
const explicitPrefix = ConfigurationService.get('LOG_PREFIX');
|
|
45
|
+
const prefix = explicitPrefix && explicitPrefix.length > 0
|
|
46
|
+
? explicitPrefix
|
|
47
|
+
: ConfigurationService.getAppEnvVarPrefix().toLowerCase();
|
|
48
|
+
const disableColoring = ConfigurationService.get('DISABLE_LOG_COLORING') ?? false;
|
|
49
|
+
return { levels, prefix, disableColoring };
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
LoggerConfiguration = __decorate([
|
|
53
|
+
Configuration('LoggerConfiguration', ...LOGGER_CONFIGURATION_PARAMETERS)
|
|
54
|
+
], LoggerConfiguration);
|
|
55
|
+
export { LoggerConfiguration };
|
|
56
|
+
function parseCanonicalLevels(raw) {
|
|
57
|
+
if (raw === undefined || raw.length === 0)
|
|
58
|
+
return [];
|
|
59
|
+
const tokens = raw
|
|
60
|
+
.split(',')
|
|
61
|
+
.map((token) => token.trim().toLowerCase())
|
|
62
|
+
.filter((token) => token.length > 0);
|
|
63
|
+
const validSet = new Set(CANONICAL_LOG_LEVELS);
|
|
64
|
+
return tokens.filter((token) => validSet.has(token));
|
|
65
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subset of `@nestjs/common`'s `LogLevel` we map to. Defined locally so this
|
|
3
|
+
* file compiles even when `@nestjs/common` isn't installed (it's an optional
|
|
4
|
+
* peer dep). Structurally compatible with NestJS's actual `LogLevel`.
|
|
5
|
+
*/
|
|
6
|
+
export type NestJsLogLevel = 'fatal' | 'error' | 'warn' | 'log' | 'debug' | 'verbose';
|
|
7
|
+
/**
|
|
8
|
+
* Maps canonical log levels to NestJS's level naming. The only non-identity
|
|
9
|
+
* mapping is `'info'` → `'log'`. Unknown level strings are silently dropped.
|
|
10
|
+
*/
|
|
11
|
+
export declare function mapToNestJsLogLevels(levels: readonly string[]): NestJsLogLevel[];
|
|
12
|
+
//# sourceMappingURL=nestjs-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nestjs-helpers.d.ts","sourceRoot":"","sources":["../src/nestjs-helpers.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,SAAS,CAAC;AAWtF;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,cAAc,EAAE,CAOhF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const CANONICAL_TO_NEST = {
|
|
2
|
+
fatal: 'fatal',
|
|
3
|
+
error: 'error',
|
|
4
|
+
warn: 'warn',
|
|
5
|
+
info: 'log', // NestJS uses 'log' for info-level messages.
|
|
6
|
+
debug: 'debug',
|
|
7
|
+
verbose: 'verbose',
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Maps canonical log levels to NestJS's level naming. The only non-identity
|
|
11
|
+
* mapping is `'info'` → `'log'`. Unknown level strings are silently dropped.
|
|
12
|
+
*/
|
|
13
|
+
export function mapToNestJsLogLevels(levels) {
|
|
14
|
+
const result = [];
|
|
15
|
+
for (const level of levels) {
|
|
16
|
+
const mapped = CANONICAL_TO_NEST[level];
|
|
17
|
+
if (mapped !== undefined)
|
|
18
|
+
result.push(mapped);
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zamatica/logger-configuration",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Configuration parameters and parsing for log levels, prefix, and color toggle. Framework-agnostic core; NestJS-specific helpers behind the /nestjs subpath.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
".": {
|
|
13
|
+
"@zamatica/source": "./src/index.ts",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./nestjs": {
|
|
19
|
+
"@zamatica/source": "./src/nestjs-helpers.ts",
|
|
20
|
+
"types": "./dist/nestjs-helpers.d.ts",
|
|
21
|
+
"import": "./dist/nestjs-helpers.js",
|
|
22
|
+
"default": "./dist/nestjs-helpers.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src",
|
|
28
|
+
"!**/*.tsbuildinfo",
|
|
29
|
+
"!**/*.spec.*",
|
|
30
|
+
"!**/*.test.*"
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/zoobroker/zamatica-libs.git",
|
|
35
|
+
"directory": "libs/logger-configuration"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@zamatica/configuration": "workspace:^",
|
|
42
|
+
"tslib": "^2.3.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@nestjs/common": "^11.0.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"@nestjs/common": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export {
|
|
2
|
+
LoggerConfiguration,
|
|
3
|
+
LOGGER_CONFIGURATION_PARAMETERS,
|
|
4
|
+
} from './logger-configuration.js';
|
|
5
|
+
export {
|
|
6
|
+
CANONICAL_LOG_LEVELS,
|
|
7
|
+
type CanonicalLogLevel,
|
|
8
|
+
type LogSettings,
|
|
9
|
+
} from './log-settings.js';
|
|
10
|
+
|
|
11
|
+
// NOTE: The NestJS-specific helpers (`mapToNestJsLogLevels`, `NestJsLogLevel`)
|
|
12
|
+
// are intentionally NOT re-exported. Import them from
|
|
13
|
+
// `@zamatica/logger-configuration/nestjs` — that subpath is the only place
|
|
14
|
+
// pulling NestJS-shaped names into a consumer's type graph.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical log levels recognized by `@zamatica/logger-configuration`.
|
|
3
|
+
*
|
|
4
|
+
* Frameworks that use different names (e.g. NestJS uses `'log'` for info-level
|
|
5
|
+
* messages) provide a mapping helper in their respective subpath export
|
|
6
|
+
* (`@zamatica/logger-configuration/nestjs`).
|
|
7
|
+
*/
|
|
8
|
+
export type CanonicalLogLevel =
|
|
9
|
+
| 'fatal'
|
|
10
|
+
| 'error'
|
|
11
|
+
| 'warn'
|
|
12
|
+
| 'info'
|
|
13
|
+
| 'debug'
|
|
14
|
+
| 'verbose';
|
|
15
|
+
|
|
16
|
+
export const CANONICAL_LOG_LEVELS: readonly CanonicalLogLevel[] = [
|
|
17
|
+
'fatal',
|
|
18
|
+
'error',
|
|
19
|
+
'warn',
|
|
20
|
+
'info',
|
|
21
|
+
'debug',
|
|
22
|
+
'verbose',
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
export interface LogSettings {
|
|
26
|
+
/** Visible levels in declared (priority) order. */
|
|
27
|
+
levels: CanonicalLogLevel[];
|
|
28
|
+
/** Prefix string used by structured loggers. Defaults to the app env-var prefix lowercased. */
|
|
29
|
+
prefix: string;
|
|
30
|
+
/** Whether to strip ANSI color codes from console output. */
|
|
31
|
+
disableColoring: boolean;
|
|
32
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Configuration,
|
|
3
|
+
ConfigurationParameter,
|
|
4
|
+
type ConfigurationParameterDefinition,
|
|
5
|
+
ConfigurationService,
|
|
6
|
+
} from '@zamatica/configuration';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
CANONICAL_LOG_LEVELS,
|
|
10
|
+
type CanonicalLogLevel,
|
|
11
|
+
type LogSettings,
|
|
12
|
+
} from './log-settings.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The three configuration parameters owned by this library, exported so that
|
|
16
|
+
* tests can re-register them after `ConfigurationService.__resetForTesting()`
|
|
17
|
+
* clears the registry. The decorator below registers the same list at
|
|
18
|
+
* class-load time for normal runtime use.
|
|
19
|
+
*/
|
|
20
|
+
export const LOGGER_CONFIGURATION_PARAMETERS: readonly ConfigurationParameterDefinition[] = [
|
|
21
|
+
ConfigurationParameter.named('LOG_LEVELS')
|
|
22
|
+
.string()
|
|
23
|
+
.default('fatal,error,warn,info')
|
|
24
|
+
.description(
|
|
25
|
+
'Comma-separated list of visible log levels: fatal, error, warn, info, debug, verbose',
|
|
26
|
+
)
|
|
27
|
+
.required()
|
|
28
|
+
.build(),
|
|
29
|
+
ConfigurationParameter.named('LOG_PREFIX')
|
|
30
|
+
.string()
|
|
31
|
+
.description(
|
|
32
|
+
'Prefix string used by structured loggers; defaults to the app env-var prefix lowercase',
|
|
33
|
+
)
|
|
34
|
+
.optional()
|
|
35
|
+
.build(),
|
|
36
|
+
ConfigurationParameter.named('DISABLE_LOG_COLORING')
|
|
37
|
+
.boolean()
|
|
38
|
+
.default(false)
|
|
39
|
+
.description('Strip ANSI color codes from console output')
|
|
40
|
+
.optional()
|
|
41
|
+
.build(),
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Registers `LOG_LEVELS`, `LOG_PREFIX`, and `DISABLE_LOG_COLORING` at module
|
|
46
|
+
* load time. Consumers call `LoggerConfiguration.parseLogSettings()` after
|
|
47
|
+
* `ConfigurationService.initialize()` runs (or any time after registration if
|
|
48
|
+
* the defaults are acceptable).
|
|
49
|
+
*
|
|
50
|
+
* The returned object is plain — frameworks adapt it to their own log-level
|
|
51
|
+
* types via subpath helpers (e.g. the NestJS adapter at
|
|
52
|
+
* `@zamatica/logger-configuration/nestjs`).
|
|
53
|
+
*/
|
|
54
|
+
@Configuration('LoggerConfiguration', ...LOGGER_CONFIGURATION_PARAMETERS)
|
|
55
|
+
export class LoggerConfiguration {
|
|
56
|
+
/** Reads the registered parameters and returns a normalized settings object. */
|
|
57
|
+
static parseLogSettings(): LogSettings {
|
|
58
|
+
const rawLevels = ConfigurationService.get<string>('LOG_LEVELS');
|
|
59
|
+
const levels = parseCanonicalLevels(rawLevels);
|
|
60
|
+
|
|
61
|
+
const explicitPrefix = ConfigurationService.get<string>('LOG_PREFIX');
|
|
62
|
+
const prefix =
|
|
63
|
+
explicitPrefix && explicitPrefix.length > 0
|
|
64
|
+
? explicitPrefix
|
|
65
|
+
: ConfigurationService.getAppEnvVarPrefix().toLowerCase();
|
|
66
|
+
|
|
67
|
+
const disableColoring = ConfigurationService.get<boolean>('DISABLE_LOG_COLORING') ?? false;
|
|
68
|
+
|
|
69
|
+
return { levels, prefix, disableColoring };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseCanonicalLevels(raw: string | undefined): CanonicalLogLevel[] {
|
|
74
|
+
if (raw === undefined || raw.length === 0) return [];
|
|
75
|
+
const tokens = raw
|
|
76
|
+
.split(',')
|
|
77
|
+
.map((token) => token.trim().toLowerCase())
|
|
78
|
+
.filter((token) => token.length > 0);
|
|
79
|
+
const validSet = new Set<string>(CANONICAL_LOG_LEVELS);
|
|
80
|
+
return tokens.filter((token): token is CanonicalLogLevel => validSet.has(token));
|
|
81
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CanonicalLogLevel } from './log-settings.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Subset of `@nestjs/common`'s `LogLevel` we map to. Defined locally so this
|
|
5
|
+
* file compiles even when `@nestjs/common` isn't installed (it's an optional
|
|
6
|
+
* peer dep). Structurally compatible with NestJS's actual `LogLevel`.
|
|
7
|
+
*/
|
|
8
|
+
export type NestJsLogLevel = 'fatal' | 'error' | 'warn' | 'log' | 'debug' | 'verbose';
|
|
9
|
+
|
|
10
|
+
const CANONICAL_TO_NEST: Readonly<Record<CanonicalLogLevel, NestJsLogLevel>> = {
|
|
11
|
+
fatal: 'fatal',
|
|
12
|
+
error: 'error',
|
|
13
|
+
warn: 'warn',
|
|
14
|
+
info: 'log', // NestJS uses 'log' for info-level messages.
|
|
15
|
+
debug: 'debug',
|
|
16
|
+
verbose: 'verbose',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Maps canonical log levels to NestJS's level naming. The only non-identity
|
|
21
|
+
* mapping is `'info'` → `'log'`. Unknown level strings are silently dropped.
|
|
22
|
+
*/
|
|
23
|
+
export function mapToNestJsLogLevels(levels: readonly string[]): NestJsLogLevel[] {
|
|
24
|
+
const result: NestJsLogLevel[] = [];
|
|
25
|
+
for (const level of levels) {
|
|
26
|
+
const mapped = CANONICAL_TO_NEST[level as CanonicalLogLevel];
|
|
27
|
+
if (mapped !== undefined) result.push(mapped);
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|