nestjs-pino 2.1.0 → 2.2.0-alpha.2a10fe

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/LoggerModule.js CHANGED
@@ -85,11 +85,19 @@ function createLoggerMiddlewares(params, useExisting) {
85
85
  return [bindLoggerMiddleware];
86
86
  }
87
87
  if (Array.isArray(params)) {
88
- return [pinoHttp(...params), bindLoggerMiddleware];
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 [pinoHttp(params), bindLoggerMiddleware];
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
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;
package/PinoLogger.js CHANGED
@@ -22,6 +22,8 @@ const storage_1 = require("./storage");
22
22
  let outOfContext;
23
23
  function __resetOutOfContextForTests() {
24
24
  outOfContext = undefined;
25
+ // @ts-ignore reset root for tests only
26
+ PinoLogger.root = undefined;
25
27
  }
26
28
  exports.__resetOutOfContextForTests = __resetOutOfContextForTests;
27
29
  let PinoLogger = PinoLogger_1 = class PinoLogger {
@@ -44,7 +46,6 @@ let PinoLogger = PinoLogger_1 = class PinoLogger {
44
46
  }
45
47
  }
46
48
  this.contextName = renameContext || 'context';
47
- this.opts = pinoHttp;
48
49
  }
49
50
  trace(...args) {
50
51
  this.call('trace', ...args);
package/README.md CHANGED
@@ -406,6 +406,21 @@ class MyService {
406
406
  }
407
407
  ```
408
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
+
409
424
  ## Migration
410
425
 
411
426
  ### v1
@@ -428,6 +443,19 @@ import { Logger } from 'nestjs-pino';
428
443
  // ...
429
444
  ```
430
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
+
431
459
  In all the other places you can use built-in `Logger`:
432
460
 
433
461
  ```ts
@@ -530,8 +558,6 @@ All the other cases are lead to either code duplication or unexpected behavior.
530
558
 
531
559
  <h2 align="center">Do you use this library?<br/>Don't be shy to give it a star! ★</h2>
532
560
 
533
- ---
534
-
535
561
  Also if you are into NestJS ecosystem you may be interested in one of my other libs:
536
562
 
537
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.1.0",
3
+ "version": "2.2.0-alpha.2a10fe",
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": "^3.4.1",
58
- "jest": "27.1.0",
57
+ "eslint-plugin-prettier": "^4.0.0",
58
+ "jest": "27.2.1",
59
59
  "memorystream": "^0.3.1",
60
60
  "pino-http": "^5.6.0",
61
61
  "prettier": "^2.3.2",