nest-logger-bundle 0.1.6 → 1.1.2
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 +164 -106
- package/dist/lib/bundle/context/async-logger-context.service.d.ts +5 -5
- package/dist/lib/bundle/context/async-logger-context.service.js +18 -21
- package/dist/lib/bundle/context/async-logger-context.service.js.map +1 -1
- package/dist/lib/bundle/context/async-logger.hook.d.ts +2 -2
- package/dist/lib/bundle/context/async-logger.hook.js +4 -4
- package/dist/lib/bundle/context/async-logger.hook.js.map +1 -1
- package/dist/lib/bundle/index.d.ts +0 -1
- package/dist/lib/bundle/index.js +0 -1
- package/dist/lib/bundle/index.js.map +1 -1
- package/dist/lib/bundle/logger-branch/logger-branch.d.ts +2 -2
- package/dist/lib/bundle/logger-branch/logger-branch.js +4 -4
- package/dist/lib/bundle/logger-branch/logger-branch.js.map +1 -1
- package/dist/lib/bundle/logger-branch/logger-leaf.d.ts +2 -2
- package/dist/lib/bundle/logger-branch/logger-leaf.js.map +1 -1
- package/dist/lib/bundle/logger-branch/logger-node.d.ts +2 -2
- package/dist/lib/bundle/logger-bundle.module.d.ts +1 -1
- package/dist/lib/bundle/logger-bundle.module.js +7 -7
- package/dist/lib/bundle/logger-bundle.module.js.map +1 -1
- package/dist/lib/bundle/logger-bundle.service.d.ts +4 -4
- package/dist/lib/bundle/logger-bundle.service.js +6 -6
- package/dist/lib/bundle/logger-bundle.service.js.map +1 -1
- package/dist/lib/core/filters/logger-exception.filter.d.ts +3 -3
- package/dist/lib/core/filters/logger-exception.filter.js +1 -1
- package/dist/lib/core/filters/logger-exception.filter.js.map +1 -1
- package/dist/lib/core/filters/logger-http.interceptor.d.ts +2 -2
- package/dist/lib/core/filters/logger-http.interceptor.js +1 -1
- package/dist/lib/core/filters/logger-http.interceptor.js.map +1 -1
- package/dist/lib/core/middleware/bind-logger.middleware.d.ts +3 -3
- package/dist/lib/core/middleware/bind-logger.middleware.js +7 -7
- package/dist/lib/core/middleware/bind-logger.middleware.js.map +1 -1
- package/dist/lib/core/providers/bundle-logger.provider.js +1 -1
- package/dist/lib/core/providers/bundle-logger.provider.js.map +1 -1
- package/dist/lib/core/providers/pretty-logger.provider.js +2 -2
- package/dist/lib/core/providers/pretty-logger.provider.js.map +1 -1
- package/dist/lib/core/providers/utils.d.ts +2 -2
- package/dist/lib/core/providers/utils.js +3 -3
- package/dist/lib/core/providers/utils.js.map +1 -1
- package/dist/lib/logger/logger.service.d.ts +5 -5
- package/dist/lib/logger/logger.service.js +7 -7
- package/dist/lib/logger/logger.service.js.map +1 -1
- package/dist/lib/nest-logger.module-definition.d.ts +2 -2
- package/dist/lib/nest-logger.module-definition.js.map +1 -1
- package/dist/lib/nest-logger.module.d.ts +4 -2
- package/dist/lib/nest-logger.module.js +18 -15
- package/dist/lib/nest-logger.module.js.map +1 -1
- package/dist/lib/nest-logger.params.d.ts +26 -33
- package/dist/lib/nest-logger.params.js +12 -21
- package/dist/lib/nest-logger.params.js.map +1 -1
- package/dist/lib/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -10,22 +10,17 @@
|
|
|
10
10
|
<img alt="Snyk Vulnerabilities for npm package" src="https://img.shields.io/snyk/vulnerabilities/npm/nest-logger-bundle" />
|
|
11
11
|
</a>
|
|
12
12
|
<img alt="Libraries.io" src="https://img.shields.io/librariesio/release/npm/nest-logger-bundle">
|
|
13
|
+
<img alt="Supported Language: Typescript" src="https://img.shields.io/npm/types/typescript" />
|
|
13
14
|
<img alt="Supported platforms: Express & Fastify" src="https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green" />
|
|
14
15
|
</p>
|
|
15
16
|
|
|
16
17
|
## Description
|
|
17
18
|
|
|
18
|
-
This library made to be used with <a href="http://nodejs.org" target="blank">Nest.js</a> it offers more flexibility for controlling logs in the application. The strongest point is that it offers a way to pack all the logs originating from an request or some asynchronous flow into a single bundle,
|
|
19
|
+
This library made to be used with <a href="http://nodejs.org" target="blank">Nest.js</a> it offers more flexibility for controlling logs in the application. The strongest point is that it offers a way to pack all the logs originating from an request or some asynchronous flow into a single bundle, you can also decide what to do with this bundle as well as regular logs.
|
|
19
20
|
|
|
20
|
-
For example, in a request several logs can occur and organizing this later or finding yourself in the middle of so many logs becomes a complicated task, with the LoggerBundle all the logs that occur in that request will be packed in a bundle and this bundle shows exactly the order that these logs were
|
|
21
|
+
For example, in a request several logs can occur and organizing this later or finding yourself in the middle of so many logs becomes a complicated task, with the `LoggerBundle` all the logs that occur in that request will be packed in a bundle and this bundle shows exactly the order that these logs were occurred using a tree, you can even create branches of these logs using the `enter()/ exit()` methods as will be explained later. This bundle will include a lot of useful information, such as the request that originated these logs and in the log tree you will be able to see a time profiling telling you how long it took in each branch tree.
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
________________
|
|
25
|
-
|
|
26
|
-
## Internal Dependencies
|
|
27
|
-
|
|
28
|
-
You don't need to install any extra dependencies. Internally this library is also made using some bases that are made on top of the <a href="https://github.com/pinojs/pino" target="blank">pino</a>, but I have plans to expose this dependencies in the future and leave the user free to choose which one to use.
|
|
23
|
+
Inside it works based on a context, be it a request or an asynchronous flow, so you can inject the `LoggerBundle` into any desired service and all calls between these services work correctly, so all logs occurring in a given request will be packed in the same bundle.
|
|
29
24
|
|
|
30
25
|
________________
|
|
31
26
|
|
|
@@ -35,25 +30,32 @@ ________________
|
|
|
35
30
|
$ npm i --save nest-logger-bundle
|
|
36
31
|
```
|
|
37
32
|
|
|
38
|
-
## Samples
|
|
39
33
|
|
|
40
34
|
________________
|
|
41
35
|
|
|
36
|
+
## Internal Dependencies
|
|
37
|
+
|
|
38
|
+
You don't need to install any extra dependencies. Internally this library is also made using some bases that are made on top of the <a href="https://github.com/pinojs/pino" target="blank">pino</a>. If you need to use some transporter you will need to configure the streams, for that, follow this [section](#streams)
|
|
39
|
+
|
|
40
|
+
________________
|
|
41
|
+
|
|
42
|
+
## Samples
|
|
43
|
+
|
|
42
44
|
If you want to see some usage examples use this repo <a href="https://github.com/pedrohcdo/nest-logger-bundle-samples" target="blank">NestLoggerBundleSamples</a>, In it you will find some projects with some use cases, the codes are commented for a better understanding.
|
|
43
45
|
|
|
44
|
-
>
|
|
46
|
+
> But if you want to see an simple example of how to configure it, see the test project [Example](test).
|
|
45
47
|
|
|
46
48
|
________________
|
|
47
49
|
|
|
48
50
|
## How to use
|
|
49
51
|
|
|
50
|
-
First we need to import the
|
|
52
|
+
First we need to import the LoggerBundleModule module in the module we want to use. Follow the minimum configuration:
|
|
51
53
|
|
|
52
54
|
```ts
|
|
53
55
|
import { Global, Module } from '@nestjs/common';
|
|
54
56
|
import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core';
|
|
55
57
|
import {
|
|
56
|
-
|
|
58
|
+
LoggerBundleModule,
|
|
57
59
|
LoggerExceptionFilter,
|
|
58
60
|
LoggerHttpInterceptor
|
|
59
61
|
} from 'nest-logger-bundle';
|
|
@@ -65,7 +67,7 @@ import {
|
|
|
65
67
|
imports: [
|
|
66
68
|
// .. imports
|
|
67
69
|
|
|
68
|
-
|
|
70
|
+
LoggerBundleModule.forRoot({})
|
|
69
71
|
],
|
|
70
72
|
|
|
71
73
|
providers: [
|
|
@@ -79,7 +81,7 @@ import {
|
|
|
79
81
|
},
|
|
80
82
|
],
|
|
81
83
|
|
|
82
|
-
exports: [
|
|
84
|
+
exports: [LoggerBundleModule /**, ... others exports */],
|
|
83
85
|
})
|
|
84
86
|
export class GlobalModule {}
|
|
85
87
|
|
|
@@ -87,7 +89,7 @@ export class GlobalModule {}
|
|
|
87
89
|
For the LoggerBundle to work correctly, it needs some points to be handled, for that there are two classes that are used to handle requests and errors, they are: `LoggerExceptionFilter` and `LoggerHttpInterceptor`.
|
|
88
90
|
These classes need to be used in the global-scoped filters and interceptors like the example to be work across the whole application. `Remember to provide this filter and interceptor as in the example above in a global module or in the main module of your application.`
|
|
89
91
|
|
|
90
|
-
> If you already have a global scope filter or interceptor, follow the [tutorial](#custom-filter-and-interceptor)
|
|
92
|
+
> If you already have a global scope filter or interceptor on your project, follow the [tutorial](#custom-filter-and-interceptor)
|
|
91
93
|
|
|
92
94
|
### Injecting LoggerBundle
|
|
93
95
|
|
|
@@ -98,7 +100,7 @@ To inject the Logger in some injectable service of your project follow the examp
|
|
|
98
100
|
export class SampleUserService {
|
|
99
101
|
|
|
100
102
|
constructor(
|
|
101
|
-
private logService:
|
|
103
|
+
private logService: LoggerBundleService
|
|
102
104
|
) {
|
|
103
105
|
this.logService.setContextToken(SampleService.name)
|
|
104
106
|
}
|
|
@@ -143,12 +145,17 @@ export class SampleUserService {
|
|
|
143
145
|
|
|
144
146
|
> Remembering that the name of the current service can be acquired by `<Class>.name`, so you can change the name of the context of the LoggerBundle right at the beginning using as shown in the example above: `this.logService.setContextToken(SampleService.name)`
|
|
145
147
|
|
|
146
|
-
|
|
148
|
+
________________
|
|
149
|
+
|
|
150
|
+
## Bundle Structure
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
The bundle is generated at the end of a flow such as a request, after that the generated bundle is dispatched according to the parameters passed in the module configuration (`the complete configuration can be seen here `[Setting-up](#setting-up)). Para demonstrar como é a estrutura do bundle vamos usar o exemplo acima [Injecting LoggerBundle](#injecting-loggerbundle), if the `SampleUserService.createUser(email, username)` function is called, the bundle structure that will be generated will be like the example below:
|
|
147
154
|
|
|
148
155
|
```json
|
|
149
156
|
{
|
|
150
157
|
logs: {
|
|
151
|
-
"profiling":
|
|
158
|
+
"profiling": <duration>,
|
|
152
159
|
"name": "root",
|
|
153
160
|
"logs": [
|
|
154
161
|
{
|
|
@@ -157,7 +164,7 @@ If the `SampleUserService.createUser(email, username)` function is called, the l
|
|
|
157
164
|
"context": "SampleService"
|
|
158
165
|
},
|
|
159
166
|
{
|
|
160
|
-
"profiling":
|
|
167
|
+
"profiling": <duration>,
|
|
161
168
|
"name": "creating user",
|
|
162
169
|
"logs": [
|
|
163
170
|
{
|
|
@@ -166,7 +173,7 @@ If the `SampleUserService.createUser(email, username)` function is called, the l
|
|
|
166
173
|
"context": "SampleService"
|
|
167
174
|
},
|
|
168
175
|
{
|
|
169
|
-
"profiling":
|
|
176
|
+
"profiling": <duration>,
|
|
170
177
|
"name": "finding user by email ",
|
|
171
178
|
"logs": [
|
|
172
179
|
{
|
|
@@ -189,27 +196,27 @@ If the `SampleUserService.createUser(email, username)` function is called, the l
|
|
|
189
196
|
"requestDuration": <duration>,
|
|
190
197
|
"method": "GET",
|
|
191
198
|
"path": "/sample/create-user/teste%40teste.com/Teste%20123",
|
|
192
|
-
"ip": <ip
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
199
|
+
"ip": <ip>,
|
|
200
|
+
tags: {
|
|
201
|
+
"test": "test 123"
|
|
202
|
+
},
|
|
196
203
|
},
|
|
197
204
|
req: <request object>,
|
|
198
205
|
res: <response object>
|
|
199
206
|
}
|
|
200
207
|
```
|
|
201
208
|
|
|
202
|
-
The
|
|
209
|
+
The bundle will contain these 5 objects
|
|
203
210
|
|
|
204
211
|
| Object | Description
|
|
205
212
|
| :--- | :----:
|
|
206
|
-
|
|
|
207
|
-
|
|
|
208
|
-
|
|
|
209
|
-
|
|
|
210
|
-
|
|
|
213
|
+
| ***logs*** | A object containing the entire `logs` tree including a time profiling between each log.
|
|
214
|
+
| ***context*** | The `context` in which this log bundle was created, containing information such as api path, method..
|
|
215
|
+
| ***context.tags*** | The `tags` created in this context
|
|
216
|
+
| ***req*** | The body of the `request` that originated this bundle
|
|
217
|
+
| ***res*** | If it is a complete request context here you will be able to see the `response` of that request
|
|
211
218
|
|
|
212
|
-
The generated
|
|
219
|
+
The generated logs tree follows the following structure, where the `logs` array can contain more log nodes like the example
|
|
213
220
|
|
|
214
221
|
```ts
|
|
215
222
|
{
|
|
@@ -235,7 +242,7 @@ The generated bundle follows the following structure, where the `logs` array can
|
|
|
235
242
|
}
|
|
236
243
|
```
|
|
237
244
|
|
|
238
|
-
There are some methods available for use in
|
|
245
|
+
There are some methods available for use in LoggerBundleService, here is a list of them
|
|
239
246
|
|
|
240
247
|
- Log Methods <br/>
|
|
241
248
|
```ts
|
|
@@ -258,10 +265,10 @@ There are some methods available for use in NestLoggerService, here is a list of
|
|
|
258
265
|
fatal(...args)
|
|
259
266
|
```
|
|
260
267
|
|
|
261
|
-
Where all log levels follow the same argument model, there are
|
|
268
|
+
Where all log levels follow the same argument model, there are three call combinations, here is an example with `log()` level
|
|
262
269
|
|
|
263
270
|
```ts
|
|
264
|
-
// The first way is sending a text that can contain special characters for formatting
|
|
271
|
+
// The first way is sending a text that can contain special characters of printf-like format for formatting (see https://github.com/pinojs/quick-format-unescaped), then the next arguments are the values referring to the provided formatting..
|
|
265
272
|
this.logService.log("message to format %d %d", 10, 20)
|
|
266
273
|
|
|
267
274
|
// The second form precedes an object that will be merged together with the formatted message
|
|
@@ -277,9 +284,9 @@ There are some methods available for use in NestLoggerService, here is a list of
|
|
|
277
284
|
|
|
278
285
|
| Method | Description
|
|
279
286
|
| :--- | :----:
|
|
280
|
-
|
|
|
281
|
-
|
|
|
282
|
-
|
|
|
287
|
+
| ***enter(`branchName`)*** | This method creates a node in the log tree where the '`branchName`' is an string that will be the name of the subtree of logs
|
|
288
|
+
| ***exit()*** | This method closes the current subtree, remembering that the same amount opened with `enter()` must be closed with `exit()`
|
|
289
|
+
| ***putTag(`tagName, tagValue`)*** | Where the '`tagName`' and '`tagValue`' are strings. This method adds a tag in the current context, the tags have no direct relation with the `enter()` and `exit()` methods, so regardless of the current state of the tree, the tags will be added separately in the bundle.
|
|
283
290
|
|
|
284
291
|
- Async Methods
|
|
285
292
|
|
|
@@ -287,19 +294,19 @@ There are some methods available for use in NestLoggerService, here is a list of
|
|
|
287
294
|
|
|
288
295
|
| Method | Description
|
|
289
296
|
| :--- | :----:
|
|
290
|
-
|
|
|
297
|
+
| ***createAsyncLogger()*** | Creates an asynchronous LoggerBundle, where the responsibility for transporting the bundle is on your side
|
|
291
298
|
|
|
292
299
|
______
|
|
293
300
|
|
|
294
301
|
## Setting-up
|
|
295
302
|
|
|
296
|
-
The
|
|
303
|
+
The LoggerBundleModule provides two ways of configuration, they are:
|
|
297
304
|
|
|
298
305
|
- *Statically Config*<br/>
|
|
299
306
|
If you want to configure it statically, just use
|
|
300
307
|
|
|
301
308
|
```ts
|
|
302
|
-
|
|
309
|
+
LoggerBundleModule.forRoot({
|
|
303
310
|
// ... params
|
|
304
311
|
})
|
|
305
312
|
```
|
|
@@ -308,9 +315,9 @@ ______
|
|
|
308
315
|
In case you want to pass the settings asynchronously
|
|
309
316
|
|
|
310
317
|
```ts
|
|
311
|
-
|
|
318
|
+
LoggerBundleModule.forRootAsync({
|
|
312
319
|
isGlobal: boolean, //
|
|
313
|
-
useFactory: (config: ConfigService):
|
|
320
|
+
useFactory: (config: ConfigService): LoggerBundleParams => {
|
|
314
321
|
return {
|
|
315
322
|
// ... params
|
|
316
323
|
}
|
|
@@ -319,18 +326,22 @@ ______
|
|
|
319
326
|
})
|
|
320
327
|
```
|
|
321
328
|
|
|
322
|
-
You must provide the desired parameters for the
|
|
329
|
+
You must provide the desired parameters for the LoggerBundleModule, the parameters follow the following schema
|
|
323
330
|
|
|
324
331
|
```ts
|
|
325
332
|
// default config
|
|
326
333
|
{
|
|
327
|
-
|
|
334
|
+
loggers: {
|
|
328
335
|
type: 'default',
|
|
329
336
|
prettyPrint: {
|
|
337
|
+
mode: LoggerBundleParamsLogggerMode, // DEFAULT IS LOG_BUNDLE
|
|
330
338
|
disabled: boolean,
|
|
331
339
|
options: pino.PrettyOptions,
|
|
332
340
|
},
|
|
333
|
-
streams:
|
|
341
|
+
streams: {
|
|
342
|
+
mode: LoggerBundleParamsLogggerMode, // DEFAULT IS LOG_BUNDLE
|
|
343
|
+
pinoStreams: pinoms.Streams
|
|
344
|
+
},
|
|
334
345
|
timestamp: {
|
|
335
346
|
format: {
|
|
336
347
|
template: string,
|
|
@@ -342,9 +353,7 @@ You must provide the desired parameters for the LoggerBundle, the parameters fol
|
|
|
342
353
|
// You can change this
|
|
343
354
|
contextBundle: {
|
|
344
355
|
strategy: {
|
|
345
|
-
|
|
346
|
-
level: NestLoggerLevelStrategy,
|
|
347
|
-
onError: NestLoggerOnErrorStrategy,
|
|
356
|
+
level: LoggerBundleLevelStrategy
|
|
348
357
|
},
|
|
349
358
|
}
|
|
350
359
|
}
|
|
@@ -352,17 +361,18 @@ You must provide the desired parameters for the LoggerBundle, the parameters fol
|
|
|
352
361
|
```ts
|
|
353
362
|
// custom config
|
|
354
363
|
{
|
|
355
|
-
|
|
364
|
+
loggers: {
|
|
356
365
|
type: 'custom',
|
|
357
|
-
logger: pino.Logger
|
|
366
|
+
logger: pino.Logger,
|
|
367
|
+
level?: string,
|
|
368
|
+
bundleLogger: pino.Logger
|
|
369
|
+
lineLogger?: pino.Logger
|
|
358
370
|
},
|
|
359
371
|
|
|
360
372
|
// You can change this
|
|
361
373
|
contextBundle: {
|
|
362
374
|
strategy: {
|
|
363
|
-
|
|
364
|
-
level: NestLoggerLevelStrategy,
|
|
365
|
-
onError: NestLoggerOnErrorStrategy,
|
|
375
|
+
level: LoggerBundleLevelStrategy
|
|
366
376
|
},
|
|
367
377
|
}
|
|
368
378
|
}
|
|
@@ -370,81 +380,129 @@ You must provide the desired parameters for the LoggerBundle, the parameters fol
|
|
|
370
380
|
|
|
371
381
|
Below is the description of each parameter
|
|
372
382
|
|
|
373
|
-
- **
|
|
383
|
+
- **LoggerBundleParams**<br/>
|
|
374
384
|
|
|
375
385
|
| Param | Description
|
|
376
386
|
| :--- | :----:
|
|
377
|
-
|
|
|
378
|
-
|
|
|
387
|
+
| ***loggers***?: LoggerBundleParamsStream \| LoggerBundleParamsCustom | The LoggerBundle uses the `pino-multi-stream ` to transport the logs to several different destinations at the same time, if you want to use the default implementation that makes managing these logs very easy use type `'default'` so some parameters of `LoggerBundleParamsStream` will be provided, but if you choose to use a type `'custom'` some parameters of `LoggerBundleParamsCustom` will be provided and you can use a `pino` logger configured in your own way.
|
|
388
|
+
| ***contextBundle***?: LoggerBundleParamsContextBundle | Here you can configure some behaviors related to how the bundle is created, for example, configure what the bundle's marjoritary level will be..
|
|
389
|
+
| ***forRoutes***?: (string \| Type<any> \| RouteInfo)[] | Pattern based routes are supported as well. For instance, the asterisk is used as a wildcard, and will match any combination of characters, for more datails see [NestJS-Middlewares](https://docs.nestjs.com/middleware), the default is `[{ path: '*', method: RequestMethod.ALL }]`
|
|
379
390
|
|
|
380
|
-
- **
|
|
381
|
-
If you choose to use the default configuration in `
|
|
391
|
+
- **LoggerBundleParamsStream**<br/>
|
|
392
|
+
If you choose to use the default configuration in `LoggerBundleParams`, using '`{ type: 'default', ... }`' the options for these parameters will be provided
|
|
382
393
|
> It is worth remembering that it is recommended to use this configuration if you do not have the need to create your own configuration.
|
|
383
394
|
|
|
384
395
|
| Param | Description
|
|
385
396
|
| :--- | :----:
|
|
386
|
-
|
|
|
387
|
-
|
|
|
388
|
-
|
|
|
389
|
-
|
|
|
397
|
+
| ***type***: `'default'` | For the options to follow this pattern you must set the type to `'default'`
|
|
398
|
+
| ***prettyPrint***?: LoggerBundleParamsPretty | Here you can configure `prettyStream`, choosing to disable it if necessary and also provide your `pin.PrettyOptions`
|
|
399
|
+
| ***streams***?: LoggerBundleParamsStreams | Here you can configure `streams`, choosing to disable it if necessary and also provide your own transporter
|
|
400
|
+
| ***timestamp***?: LoggerBundleParamsTimestamp | To configure how the timestamp will be formatted or even disable it, use these settings
|
|
401
|
+
|
|
402
|
+
### Related Params
|
|
403
|
+
|
|
404
|
+
- **LoggerBundleParamsPretty**<br/>
|
|
390
405
|
|
|
391
|
-
|
|
406
|
+
| Param | Description
|
|
407
|
+
| :--- | :----:
|
|
408
|
+
| ***mode***?: LoggerBundleParamsLogggerMode | Here you can choose the mode that `prettyStream` will display the logs, the default value is `LoggerBundleParamsLogggerMode.LOG_BUNDLE`, so the bundle will be logged.
|
|
409
|
+
| ***disabled***?: boolean | If you want to disable the `prettyStream` you can pass `false` in this option `(remembering that, as it will be disabled the 'options' will not have any effects)`
|
|
410
|
+
| ***options***?: pino.PrettyOptions | Here you can pass some options provided by `pin`, like `{colorize: true}`
|
|
392
411
|
|
|
412
|
+
- **LoggerBundleParamsStreams**<br/>
|
|
413
|
+
|
|
393
414
|
| Param | Description
|
|
394
415
|
| :--- | :----:
|
|
395
|
-
|
|
|
396
|
-
|
|
|
416
|
+
| ***mode***?: LoggerBundleParamsLogggerMode | Here you can choose the mode that `streams` will display the logs, the default value is `LoggerBundleParamsLogggerMode.LOG_BUNDLE`, so the bundle will be logged.
|
|
417
|
+
| ***pinoStreams***?: pinoms.Streams | You can also tell which `streams` you want pinoms handles, you can find implementations of various transporters that can be used here https://github.com/pinojs/pino/blob/master/docs/transports.md#legacy
|
|
418
|
+
|
|
419
|
+
- **LoggerBundleParamsLogggerMode**<br/>
|
|
420
|
+
|
|
421
|
+
There are two types of modes used in the `prettyPrint` and `streams` settings, they are:
|
|
422
|
+
|
|
423
|
+
| Enum | Description
|
|
424
|
+
| :--- | :----:
|
|
425
|
+
| ***LoggerBundleParamsLogggerMode.LOG_BUNDLE*** | Indicates that the log will be sent to the destination as a bundle `(this is the default behavior of all destinations)`
|
|
426
|
+
| ***LoggerBundleParamsLogggerMode.LOG_LINE*** | Indicates that the log will be sent to the destination as log lines
|
|
397
427
|
|
|
398
428
|
- **pinoms.Streams**<br/>
|
|
399
429
|
|
|
400
|
-
Here
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
```ts
|
|
404
|
-
import datadog from 'pino-datadog';
|
|
405
|
-
|
|
406
|
-
// ...
|
|
407
|
-
NestLoggerModule.forRootAsync({
|
|
408
|
-
useFactory: async (config: ConfigService): Promise<NestLoggerParams> => {
|
|
409
|
-
const datadogStream = await datadog.createWriteStream({
|
|
410
|
-
apiKey: config.get('datadog.apiKey'),
|
|
411
|
-
service: config.get('datadog.serviceName'),
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
return {
|
|
415
|
-
// ...
|
|
416
|
-
pinoStream: {
|
|
417
|
-
type: 'default',
|
|
418
|
-
streams: [
|
|
419
|
-
{
|
|
420
|
-
stream: datadogStream,
|
|
421
|
-
},
|
|
422
|
-
],
|
|
423
|
-
},
|
|
424
|
-
};
|
|
425
|
-
},
|
|
426
|
-
inject: [ConfigService],
|
|
427
|
-
}),
|
|
430
|
+
Here you can set some streams to transport your logs, check these examples of how to use [Streams](#streams)
|
|
431
|
+
|
|
432
|
+
- **LoggerBundleParamsTimestamp**<br/>
|
|
428
433
|
|
|
429
|
-
|
|
434
|
+
| Param | Description
|
|
435
|
+
| :--- | :----:
|
|
436
|
+
| ***disabled***: boolean | If necessary, you can also disable the timestamp.
|
|
437
|
+
| ***format***: LoggerBundleParamsPinoTimestampFormat | You can also configure how the timestamp will be formatted in the logs informing a template and a timezone, the template is created with the help of `dayjs` to assemble the desired string you can use the symbols informed here [Day.js](https://day.js.org/docs/en/display/format)
|
|
430
438
|
|
|
431
|
-
- **
|
|
439
|
+
- **LoggerBundleParamsPinoTimestampFormat**<br/>
|
|
432
440
|
|
|
433
441
|
| Param | Description
|
|
434
442
|
| :--- | :----:
|
|
435
|
-
|
|
|
436
|
-
|
|
|
443
|
+
| ***template***: string | To format the timezone your way, use a string that follows the pattern informed here [dayjs-formar](https://day.js.org/docs/en/display/format), eg: `'DD/MM/YYYY - HH:mm:ss.SSS'`
|
|
444
|
+
| ***timezone***: string | Inform the timezone, you can find the valid timezones here [IANA database](https://www.iana.org/time-zones)
|
|
445
|
+
|
|
446
|
+
|
|
437
447
|
|
|
448
|
+
- **LoggerBundleParamsCustom**<br/>
|
|
449
|
+
But if you choose to use the custom configuration in `LoggerBundleParams`, using '`{ type: 'custom', ... }`' the options for these parameters will be provided
|
|
438
450
|
|
|
451
|
+
| Param | Description
|
|
452
|
+
| :--- | :----:
|
|
453
|
+
| ***type***: `'custom'` | For the options to follow this pattern you must set the type to `'custom'`
|
|
454
|
+
| ***bundleLogger***: pino.Logger | This logger will be used to log bundles only
|
|
455
|
+
| ***lineLogger***?: pino.Logger | This logger will be used to log only line logs (which are common logs)
|
|
439
456
|
|
|
440
|
-
- **
|
|
441
|
-
|
|
457
|
+
- **LoggerBundleParamsContextBundle**<br/>
|
|
458
|
+
Here you can configure bundle-related behaviors, such as the `strategy` used to dispatch the bundle to the loggers
|
|
442
459
|
|
|
443
460
|
| Param | Description
|
|
444
461
|
| :--- | :----:
|
|
445
|
-
|
|
|
446
|
-
|
|
462
|
+
| ***strategy***?: LoggerBundleParamsContextBundleStrategy | Strategy used to dispatch the bundle to the loggers
|
|
463
|
+
|
|
464
|
+
### Related Params
|
|
447
465
|
|
|
466
|
+
- **LoggerBundleParamsContextBundleStrategy**<br/>
|
|
467
|
+
Below are the settings available for these strategies
|
|
468
|
+
|
|
469
|
+
| Param | Description
|
|
470
|
+
| :--- | :----:
|
|
471
|
+
| ***level***?: LoggerBundleLevelStrategy | This strategy defines what will be the main level of the bundle, as the bundle will contain a tree of logs, it can contain several logs with several levels, so to define the main level, the configuration provided here is used to decide the best level, the default strategy is `LoggerBundleLevelStrategy.MAJOR_LEVEL`
|
|
472
|
+
|
|
473
|
+
### Streams
|
|
474
|
+
|
|
475
|
+
Probably at some point you may need to transport your logs, for example to some
|
|
476
|
+
observability service in the cloud, here is an example of how to configure this using the `streams` parameter to send the logs to Datadog service `(In this example, datadog transporter is used)`
|
|
477
|
+
> To find more transporters and how to install their dependencies, have a look at the pino repository in this section [Legacy](https://github.com/pinojs/pino/blob/master/docs/transports.md#legacy)
|
|
478
|
+
|
|
479
|
+
```ts
|
|
480
|
+
import datadog from 'pino-datadog';
|
|
481
|
+
|
|
482
|
+
// ...
|
|
483
|
+
LoggerBundleModule.forRootAsync({
|
|
484
|
+
useFactory: async (config: ConfigService): Promise<LoggerBundleParams> => {
|
|
485
|
+
const datadogStream = await datadog.createWriteStream({
|
|
486
|
+
apiKey: config.get('datadog.apiKey'),
|
|
487
|
+
service: config.get('datadog.serviceName'),
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
return {
|
|
491
|
+
// ...
|
|
492
|
+
pinoStream: {
|
|
493
|
+
type: 'default',
|
|
494
|
+
streams: [
|
|
495
|
+
{
|
|
496
|
+
stream: datadogStream,
|
|
497
|
+
},
|
|
498
|
+
],
|
|
499
|
+
},
|
|
500
|
+
};
|
|
501
|
+
},
|
|
502
|
+
inject: [ConfigService],
|
|
503
|
+
}),
|
|
504
|
+
|
|
505
|
+
```
|
|
448
506
|
|
|
449
507
|
### Custom Filter and Interceptor
|
|
450
508
|
|
|
@@ -514,13 +572,13 @@ import { GlobalInterceptor } from './example-http-interceptor.ts'
|
|
|
514
572
|
In case you need to call some asynchronous function and not block the execution with `await` this can create a point of failure for the `LoggerBundle`, this failure is not serious but it can create confusion when interpreting the logs, this happens because a request that originated this call can end before the async function finishes, so when the request is finished the `LoggerBundle` assembles a bundle and transports it, so the async call that can still be loose and calling logging in will not be packaged in the same bundle, these logs they would be lost. For this there is a function that creates an asynchronous `LoggerBundle` and transfers you the responsibility of transporting the log at the end of the asynchronous flow. An example of usage is shown below
|
|
515
573
|
|
|
516
574
|
```ts
|
|
517
|
-
import { AsyncLoggerService,
|
|
575
|
+
import { AsyncLoggerService, LoggerBundleService } from 'nest-logger-bundle';
|
|
518
576
|
|
|
519
577
|
@Injectable()
|
|
520
578
|
export class SampleUserService {
|
|
521
579
|
|
|
522
580
|
constructor(
|
|
523
|
-
private logService:
|
|
581
|
+
private logService: LoggerBundleService
|
|
524
582
|
) {
|
|
525
583
|
this.logService.setContextToken(SampleService.name)
|
|
526
584
|
}
|
|
@@ -553,8 +611,8 @@ export class SampleUserService {
|
|
|
553
611
|
|
|
554
612
|
## Stay in touch
|
|
555
613
|
|
|
556
|
-
- Author - [Pedro Henrique C.](https://
|
|
614
|
+
- Author - [Pedro Henrique C.](https://twitter.com/pedrohcdo)
|
|
557
615
|
|
|
558
616
|
## License
|
|
559
617
|
|
|
560
|
-
NestLoggerBundle is [
|
|
618
|
+
NestLoggerBundle is [Apache License 2.0](LICENSE).
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { ModuleRef } from '@nestjs/core';
|
|
2
2
|
import pino from 'pino';
|
|
3
|
-
import {
|
|
4
|
-
export declare class
|
|
3
|
+
import { LoggerBundleParams } from '../../nest-logger.params';
|
|
4
|
+
export declare class BundleAsyncLoggerContext {
|
|
5
5
|
private params;
|
|
6
6
|
private bundleLogger;
|
|
7
7
|
private moduleRef;
|
|
8
8
|
private detachedContext;
|
|
9
|
-
constructor(params:
|
|
9
|
+
constructor(params: LoggerBundleParams, bundleLogger: pino.Logger, moduleRef: ModuleRef);
|
|
10
10
|
getCurrent(): {
|
|
11
11
|
logger: import("pino").Logger<import("pino").LoggerOptions>;
|
|
12
12
|
reqId: any;
|
|
13
13
|
loggerBundle: any;
|
|
14
14
|
};
|
|
15
15
|
dispatchCurrentLoggerBundle(message: string): void;
|
|
16
|
-
dispatchCurrentLoggerBundle(
|
|
16
|
+
dispatchCurrentLoggerBundle(exception: unknown, message?: string): void;
|
|
17
17
|
hasContext(): boolean;
|
|
18
|
-
createDetachedContext(): Promise<
|
|
18
|
+
createDetachedContext(): Promise<BundleAsyncLoggerContext>;
|
|
19
19
|
}
|
|
@@ -14,9 +14,9 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
14
14
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
15
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
16
|
};
|
|
17
|
-
var
|
|
17
|
+
var BundleAsyncLoggerContext_1;
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.
|
|
19
|
+
exports.BundleAsyncLoggerContext = void 0;
|
|
20
20
|
const common_1 = require("@nestjs/common");
|
|
21
21
|
const core_1 = require("@nestjs/core");
|
|
22
22
|
const pino_1 = __importDefault(require("pino"));
|
|
@@ -24,7 +24,7 @@ const nest_logger_module_definition_1 = require("../../nest-logger.module-defini
|
|
|
24
24
|
const nest_logger_params_1 = require("../../nest-logger.params");
|
|
25
25
|
const logger_bundle_service_1 = require("../logger-bundle.service");
|
|
26
26
|
const async_logger_hook_1 = require("./async-logger.hook");
|
|
27
|
-
let
|
|
27
|
+
let BundleAsyncLoggerContext = BundleAsyncLoggerContext_1 = class BundleAsyncLoggerContext {
|
|
28
28
|
constructor(params, bundleLogger, moduleRef) {
|
|
29
29
|
this.params = params;
|
|
30
30
|
this.bundleLogger = bundleLogger;
|
|
@@ -36,36 +36,33 @@ let NestAsyncLoggerContext = NestAsyncLoggerContext_1 = class NestAsyncLoggerCon
|
|
|
36
36
|
if (!this.hasContext()) {
|
|
37
37
|
return null;
|
|
38
38
|
}
|
|
39
|
-
const fromStore = async_logger_hook_1.
|
|
39
|
+
const fromStore = async_logger_hook_1.BundleLoggerStorage.getStore();
|
|
40
40
|
return {
|
|
41
41
|
logger: fromStore.logger,
|
|
42
42
|
reqId: fromStore.reqId,
|
|
43
43
|
loggerBundle: fromStore.loggerContext,
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
|
-
dispatchCurrentLoggerBundle(
|
|
47
|
-
var _a, _b, _c;
|
|
46
|
+
dispatchCurrentLoggerBundle(exceptionOrMessage, message) {
|
|
48
47
|
if (!this.hasContext()) {
|
|
49
48
|
return;
|
|
50
49
|
}
|
|
51
50
|
const { logger, loggerBundle } = this.getCurrent();
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
childLogger.flush();
|
|
60
|
-
}
|
|
51
|
+
const { object, level } = loggerBundle.build();
|
|
52
|
+
const childLogger = logger.child(object);
|
|
53
|
+
if (message)
|
|
54
|
+
childLogger[level](exceptionOrMessage, message);
|
|
55
|
+
else
|
|
56
|
+
childLogger[level](exceptionOrMessage);
|
|
57
|
+
childLogger.flush();
|
|
61
58
|
loggerBundle.expireNow();
|
|
62
59
|
}
|
|
63
60
|
hasContext() {
|
|
64
|
-
return !!async_logger_hook_1.
|
|
61
|
+
return !!async_logger_hook_1.BundleLoggerStorage.getStore();
|
|
65
62
|
}
|
|
66
63
|
async createDetachedContext() {
|
|
67
|
-
const context = await this.moduleRef.create(
|
|
68
|
-
const detachedLoggerBundle = await this.moduleRef.create(logger_bundle_service_1.
|
|
64
|
+
const context = await this.moduleRef.create(BundleAsyncLoggerContext_1);
|
|
65
|
+
const detachedLoggerBundle = await this.moduleRef.create(logger_bundle_service_1.LoggerBundle);
|
|
69
66
|
let getFrom;
|
|
70
67
|
if (this.detachedContext) {
|
|
71
68
|
getFrom = this.detachedContext;
|
|
@@ -91,11 +88,11 @@ let NestAsyncLoggerContext = NestAsyncLoggerContext_1 = class NestAsyncLoggerCon
|
|
|
91
88
|
return context;
|
|
92
89
|
}
|
|
93
90
|
};
|
|
94
|
-
|
|
91
|
+
BundleAsyncLoggerContext = BundleAsyncLoggerContext_1 = __decorate([
|
|
95
92
|
(0, common_1.Injectable)({}),
|
|
96
93
|
__param(0, (0, common_1.Inject)(nest_logger_module_definition_1.MODULE_OPTIONS_TOKEN)),
|
|
97
94
|
__param(1, (0, common_1.Inject)(nest_logger_params_1.BUNDLE_LOGGER_PROVIDER_TOKEN)),
|
|
98
95
|
__metadata("design:paramtypes", [Object, Object, core_1.ModuleRef])
|
|
99
|
-
],
|
|
100
|
-
exports.
|
|
96
|
+
], BundleAsyncLoggerContext);
|
|
97
|
+
exports.BundleAsyncLoggerContext = BundleAsyncLoggerContext;
|
|
101
98
|
//# sourceMappingURL=async-logger-context.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-logger-context.service.js","sourceRoot":"","sources":["../../../../lib/src/bundle/context/async-logger-context.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,2CAAoD;AACpD,uCAAyC;AACzC,gDAAwB;AACxB,uFAA2E;AAC3E,
|
|
1
|
+
{"version":3,"file":"async-logger-context.service.js","sourceRoot":"","sources":["../../../../lib/src/bundle/context/async-logger-context.service.ts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