nestjs-pino 2.0.2 → 2.2.0-alpha.0a1510
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/InjectPinoLogger.js +1 -1
- package/Logger.js +2 -2
- package/LoggerModule.js +16 -8
- package/PinoLogger.d.ts +7 -1
- package/PinoLogger.js +17 -7
- package/README.md +61 -3
- package/package.json +3 -3
- package/storage.d.ts +5 -1
- package/storage.js +7 -1
package/InjectPinoLogger.js
CHANGED
|
@@ -7,7 +7,7 @@ const decoratedTokenPrefix = 'PinoLogger:';
|
|
|
7
7
|
const decoratedLoggers = new Set();
|
|
8
8
|
function InjectPinoLogger(context = '') {
|
|
9
9
|
decoratedLoggers.add(context);
|
|
10
|
-
return common_1.Inject(getLoggerToken(context));
|
|
10
|
+
return (0, common_1.Inject)(getLoggerToken(context));
|
|
11
11
|
}
|
|
12
12
|
exports.InjectPinoLogger = InjectPinoLogger;
|
|
13
13
|
function createDecoratedLoggerProvider(context) {
|
package/Logger.js
CHANGED
|
@@ -60,8 +60,8 @@ let Logger = class Logger {
|
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
62
|
Logger = __decorate([
|
|
63
|
-
common_1.Injectable(),
|
|
64
|
-
__param(1, common_1.Inject(params_1.PARAMS_PROVIDER_TOKEN)),
|
|
63
|
+
(0, common_1.Injectable)(),
|
|
64
|
+
__param(1, (0, common_1.Inject)(params_1.PARAMS_PROVIDER_TOKEN)),
|
|
65
65
|
__metadata("design:paramtypes", [PinoLogger_1.PinoLogger, Object])
|
|
66
66
|
], Logger);
|
|
67
67
|
exports.Logger = Logger;
|
package/LoggerModule.js
CHANGED
|
@@ -31,7 +31,7 @@ let LoggerModule = LoggerModule_1 = class LoggerModule {
|
|
|
31
31
|
provide: params_1.PARAMS_PROVIDER_TOKEN,
|
|
32
32
|
useValue: params || {},
|
|
33
33
|
};
|
|
34
|
-
const decorated = InjectPinoLogger_1.createProvidersForDecorated();
|
|
34
|
+
const decorated = (0, InjectPinoLogger_1.createProvidersForDecorated)();
|
|
35
35
|
return {
|
|
36
36
|
module: LoggerModule_1,
|
|
37
37
|
providers: [Logger_1.Logger, ...decorated, PinoLogger_1.PinoLogger, paramsProvider],
|
|
@@ -44,7 +44,7 @@ let LoggerModule = LoggerModule_1 = class LoggerModule {
|
|
|
44
44
|
useFactory: params.useFactory,
|
|
45
45
|
inject: params.inject,
|
|
46
46
|
};
|
|
47
|
-
const decorated = InjectPinoLogger_1.createProvidersForDecorated();
|
|
47
|
+
const decorated = (0, InjectPinoLogger_1.createProvidersForDecorated)();
|
|
48
48
|
const providers = [
|
|
49
49
|
Logger_1.Logger,
|
|
50
50
|
...decorated,
|
|
@@ -74,9 +74,9 @@ let LoggerModule = LoggerModule_1 = class LoggerModule {
|
|
|
74
74
|
}
|
|
75
75
|
};
|
|
76
76
|
LoggerModule = LoggerModule_1 = __decorate([
|
|
77
|
-
common_1.Global(),
|
|
78
|
-
common_1.Module({ providers: [Logger_1.Logger], exports: [Logger_1.Logger] }),
|
|
79
|
-
__param(0, common_1.Inject(params_1.PARAMS_PROVIDER_TOKEN)),
|
|
77
|
+
(0, common_1.Global)(),
|
|
78
|
+
(0, common_1.Module)({ providers: [Logger_1.Logger], exports: [Logger_1.Logger] }),
|
|
79
|
+
__param(0, (0, common_1.Inject)(params_1.PARAMS_PROVIDER_TOKEN)),
|
|
80
80
|
__metadata("design:paramtypes", [Object])
|
|
81
81
|
], LoggerModule);
|
|
82
82
|
exports.LoggerModule = LoggerModule;
|
|
@@ -85,15 +85,23 @@ function createLoggerMiddlewares(params, useExisting) {
|
|
|
85
85
|
return [bindLoggerMiddleware];
|
|
86
86
|
}
|
|
87
87
|
if (Array.isArray(params)) {
|
|
88
|
-
|
|
88
|
+
const middleware = pinoHttp(...params);
|
|
89
|
+
// @ts-expect-error: root is readonly field, but this is the place where
|
|
90
|
+
// it's set actually
|
|
91
|
+
PinoLogger_1.PinoLogger.root = middleware.logger;
|
|
92
|
+
return [middleware, bindLoggerMiddleware];
|
|
89
93
|
}
|
|
94
|
+
const middleware = pinoHttp(params);
|
|
95
|
+
// @ts-expect-error: root is readonly field, but this is the place where
|
|
96
|
+
// it's set actually
|
|
97
|
+
PinoLogger_1.PinoLogger.root = middleware.logger;
|
|
90
98
|
// FIXME: params type here is pinoHttp.Options | pino.DestinationStream
|
|
91
99
|
// pinoHttp has two overloads, each of them takes those types
|
|
92
|
-
return [
|
|
100
|
+
return [middleware, bindLoggerMiddleware];
|
|
93
101
|
}
|
|
94
102
|
function bindLoggerMiddleware(req, _res, next) {
|
|
95
103
|
// @ts-ignore: run requires arguments for next but should not because it can
|
|
96
104
|
// be called without arguments
|
|
97
|
-
storage_1.storage.run(req.log, next);
|
|
105
|
+
storage_1.storage.run(new storage_1.Store(req.log), next);
|
|
98
106
|
}
|
|
99
107
|
//# sourceMappingURL=LoggerModule.js.map
|
package/PinoLogger.d.ts
CHANGED
|
@@ -3,9 +3,14 @@ import { Params } from './params';
|
|
|
3
3
|
declare type PinoMethods = Pick<pino.BaseLogger, 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'>;
|
|
4
4
|
export declare function __resetOutOfContextForTests(): void;
|
|
5
5
|
export declare class PinoLogger implements PinoMethods {
|
|
6
|
+
/**
|
|
7
|
+
* root is the most root logger that can be used to change params at runtime.
|
|
8
|
+
* Accessible only when `useExisting` is not set to `true` in `Params`.
|
|
9
|
+
* Readonly, but you can change it's properties.
|
|
10
|
+
*/
|
|
11
|
+
static readonly root: pino.Logger;
|
|
6
12
|
private context;
|
|
7
13
|
private readonly contextName;
|
|
8
|
-
private readonly opts;
|
|
9
14
|
constructor({ pinoHttp, renameContext }: Params);
|
|
10
15
|
trace(msg: string, ...args: any[]): void;
|
|
11
16
|
trace(obj: object, msg?: string, ...args: any[]): void;
|
|
@@ -22,5 +27,6 @@ export declare class PinoLogger implements PinoMethods {
|
|
|
22
27
|
setContext(value: string): void;
|
|
23
28
|
private call;
|
|
24
29
|
get logger(): pino.Logger;
|
|
30
|
+
assign(fields: pino.Bindings): void;
|
|
25
31
|
}
|
|
26
32
|
export {};
|
package/PinoLogger.js
CHANGED
|
@@ -11,6 +11,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
+
var PinoLogger_1;
|
|
14
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
16
|
exports.PinoLogger = exports.__resetOutOfContextForTests = void 0;
|
|
16
17
|
/* eslint-disable @typescript-eslint/ban-types */
|
|
@@ -21,16 +22,18 @@ const storage_1 = require("./storage");
|
|
|
21
22
|
let outOfContext;
|
|
22
23
|
function __resetOutOfContextForTests() {
|
|
23
24
|
outOfContext = undefined;
|
|
25
|
+
// @ts-ignore reset root for tests only
|
|
26
|
+
PinoLogger.root = undefined;
|
|
24
27
|
}
|
|
25
28
|
exports.__resetOutOfContextForTests = __resetOutOfContextForTests;
|
|
26
|
-
let PinoLogger = class PinoLogger {
|
|
29
|
+
let PinoLogger = PinoLogger_1 = class PinoLogger {
|
|
27
30
|
constructor({ pinoHttp, renameContext }) {
|
|
28
31
|
this.context = '';
|
|
29
32
|
if (!outOfContext) {
|
|
30
33
|
if (Array.isArray(pinoHttp)) {
|
|
31
34
|
outOfContext = pino(...pinoHttp);
|
|
32
35
|
}
|
|
33
|
-
else if (params_1.isPassedLogger(pinoHttp)) {
|
|
36
|
+
else if ((0, params_1.isPassedLogger)(pinoHttp)) {
|
|
34
37
|
outOfContext = pinoHttp.logger;
|
|
35
38
|
}
|
|
36
39
|
else if (typeof pinoHttp === 'object' &&
|
|
@@ -43,7 +46,6 @@ let PinoLogger = class PinoLogger {
|
|
|
43
46
|
}
|
|
44
47
|
}
|
|
45
48
|
this.contextName = renameContext || 'context';
|
|
46
|
-
this.opts = pinoHttp;
|
|
47
49
|
}
|
|
48
50
|
trace(...args) {
|
|
49
51
|
this.call('trace', ...args);
|
|
@@ -91,14 +93,22 @@ let PinoLogger = class PinoLogger {
|
|
|
91
93
|
this.logger[method](...args);
|
|
92
94
|
}
|
|
93
95
|
get logger() {
|
|
96
|
+
var _a;
|
|
94
97
|
// outOfContext is always set in runtime before starts using
|
|
95
98
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
96
|
-
return storage_1.storage.getStore() || outOfContext;
|
|
99
|
+
return ((_a = storage_1.storage.getStore()) === null || _a === void 0 ? void 0 : _a.logger) || outOfContext;
|
|
100
|
+
}
|
|
101
|
+
assign(fields) {
|
|
102
|
+
const store = storage_1.storage.getStore();
|
|
103
|
+
if (!store) {
|
|
104
|
+
throw new Error(`${PinoLogger_1.name}: unable to assign extra fields out of request scope`);
|
|
105
|
+
}
|
|
106
|
+
store.logger = store.logger.child(fields);
|
|
97
107
|
}
|
|
98
108
|
};
|
|
99
|
-
PinoLogger = __decorate([
|
|
100
|
-
common_1.Injectable({ scope: common_1.Scope.TRANSIENT }),
|
|
101
|
-
__param(0, common_1.Inject(params_1.PARAMS_PROVIDER_TOKEN)),
|
|
109
|
+
PinoLogger = PinoLogger_1 = __decorate([
|
|
110
|
+
(0, common_1.Injectable)({ scope: common_1.Scope.TRANSIENT }),
|
|
111
|
+
__param(0, (0, common_1.Inject)(params_1.PARAMS_PROVIDER_TOKEN)),
|
|
102
112
|
__metadata("design:paramtypes", [Object])
|
|
103
113
|
], PinoLogger);
|
|
104
114
|
exports.PinoLogger = PinoLogger;
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
<a href="https://www.npmjs.com/package/nestjs-pino">
|
|
9
9
|
<img alt="npm" src="https://img.shields.io/npm/v/nestjs-pino" />
|
|
10
10
|
</a>
|
|
11
|
-
<img alt="GitHub branch checks state" src="https://
|
|
11
|
+
<img alt="GitHub branch checks state" src="https://badgen.net/github/checks/iamolegga/nestjs-pino">
|
|
12
12
|
<a href="https://codeclimate.com/github/iamolegga/nestjs-pino/test_coverage">
|
|
13
13
|
<img src="https://api.codeclimate.com/v1/badges/2821150bb93506cb66fc/test_coverage" />
|
|
14
14
|
</a>
|
|
@@ -374,6 +374,53 @@ Since logger substitution has appeared in NestJS@8 the main purpose of `Logger`
|
|
|
374
374
|
|
|
375
375
|
With such change it's not possible to detect if method was called by app internaly and the last argument is context or `Logger` was injected in some service via `constructor(private logger: Logger) {}` and the last argument is interpolation value for example.
|
|
376
376
|
|
|
377
|
+
## Assign extra fields for future calls
|
|
378
|
+
|
|
379
|
+
You can enrich logs before calling log methods. It's possible by using `assign` method of `PinoLogger` instance. As `Logger` class is used only for NestJS built-in `Logger` substitution via `app.useLogger(...)` this feature is only limited to `PinoLogger` class. Example:
|
|
380
|
+
|
|
381
|
+
```ts
|
|
382
|
+
|
|
383
|
+
@Controller('/')
|
|
384
|
+
class TestController {
|
|
385
|
+
constructor(
|
|
386
|
+
private readonly logger: PinoLogger,
|
|
387
|
+
private readonly service: MyService,
|
|
388
|
+
) {}
|
|
389
|
+
|
|
390
|
+
@Get()
|
|
391
|
+
get() {
|
|
392
|
+
// assign extra fields in one place...
|
|
393
|
+
this.logger.assign({ userID: '42' });
|
|
394
|
+
return this.service.test();
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
@Injectable()
|
|
399
|
+
class MyService {
|
|
400
|
+
private readonly logger = new Logger(MyService.name);
|
|
401
|
+
|
|
402
|
+
test() {
|
|
403
|
+
// ...and it will be logged in another one
|
|
404
|
+
this.logger.log('hello world');
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
## Change pino params at runtime
|
|
410
|
+
|
|
411
|
+
Pino root instance with passed via module registration params creates a separate child logger for every request. This root logger params can be changed at runtime via `PinoLogger.root` property which is the pointer to logger instance. Example:
|
|
412
|
+
|
|
413
|
+
```ts
|
|
414
|
+
@Controller('/')
|
|
415
|
+
class TestController {
|
|
416
|
+
@Post('/change-loggin-level')
|
|
417
|
+
setLevel() {
|
|
418
|
+
PinoLogger.root.level = 'info';
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
377
424
|
## Migration
|
|
378
425
|
|
|
379
426
|
### v1
|
|
@@ -396,6 +443,19 @@ import { Logger } from 'nestjs-pino';
|
|
|
396
443
|
// ...
|
|
397
444
|
```
|
|
398
445
|
|
|
446
|
+
Note that for [standalone applications](https://docs.nestjs.com/standalone-applications), buffering has to be disabled manually after custom logger is ready to be used by NestJS (refer to [this issue](https://github.com/iamolegga/nestjs-pino/issues/553) for more details):
|
|
447
|
+
|
|
448
|
+
```ts
|
|
449
|
+
// main.ts
|
|
450
|
+
import { Logger } from 'nestjs-pino';
|
|
451
|
+
import { Logger as BaseLogger } from '@nestjs/common';
|
|
452
|
+
// ...
|
|
453
|
+
const app = await NestFactory.createApplicationContext(AppModule, { bufferLogs: true });
|
|
454
|
+
app.useLogger(app.get(Logger));
|
|
455
|
+
BaseLogger.flush();
|
|
456
|
+
// ...
|
|
457
|
+
```
|
|
458
|
+
|
|
399
459
|
In all the other places you can use built-in `Logger`:
|
|
400
460
|
|
|
401
461
|
```ts
|
|
@@ -498,8 +558,6 @@ All the other cases are lead to either code duplication or unexpected behavior.
|
|
|
498
558
|
|
|
499
559
|
<h2 align="center">Do you use this library?<br/>Don't be shy to give it a star! ★</h2>
|
|
500
560
|
|
|
501
|
-
---
|
|
502
|
-
|
|
503
561
|
Also if you are into NestJS ecosystem you may be interested in one of my other libs:
|
|
504
562
|
|
|
505
563
|
[nestjs-pino](https://github.com/iamolegga/nestjs-pino)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nestjs-pino",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.2.0-alpha.0a1510",
|
|
4
4
|
"description": "Pino logger for NestJS",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
"eslint": "^7.32.0",
|
|
55
55
|
"eslint-config-prettier": "^8.3.0",
|
|
56
56
|
"eslint-plugin-import": "^2.24.2",
|
|
57
|
-
"eslint-plugin-prettier": "^
|
|
58
|
-
"jest": "27.
|
|
57
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
58
|
+
"jest": "27.2.2",
|
|
59
59
|
"memorystream": "^0.3.1",
|
|
60
60
|
"pino-http": "^5.6.0",
|
|
61
61
|
"prettier": "^2.3.2",
|
package/storage.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
3
3
|
import { Logger } from 'pino';
|
|
4
|
-
export declare
|
|
4
|
+
export declare class Store {
|
|
5
|
+
logger: Logger;
|
|
6
|
+
constructor(logger: Logger);
|
|
7
|
+
}
|
|
8
|
+
export declare const storage: AsyncLocalStorage<Store>;
|
package/storage.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.storage = void 0;
|
|
3
|
+
exports.storage = exports.Store = void 0;
|
|
4
4
|
const async_hooks_1 = require("async_hooks");
|
|
5
|
+
class Store {
|
|
6
|
+
constructor(logger) {
|
|
7
|
+
this.logger = logger;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.Store = Store;
|
|
5
11
|
exports.storage = new async_hooks_1.AsyncLocalStorage();
|
|
6
12
|
//# sourceMappingURL=storage.js.map
|