@iamnnort/nestjs-logger 2.0.0 → 2.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 +67 -53
- package/package.json +3 -8
package/README.md
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
# @iamnnort/nestjs-logger
|
|
2
2
|
|
|
3
|
-
Logger module for NestJS
|
|
3
|
+
Logger module for NestJS based on [nestjs-pino](https://github.com/iamolegga/nestjs-pino) — structured request logging, clean output, CloudWatch-friendly.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Features
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
- Automatic HTTP request/response logging via pino-http
|
|
8
|
+
- Clean single-line request logs: `INFO: [Http] GET /v1/visits 200 (3ms)`
|
|
9
|
+
- Context-aware logging: `INFO: [AppController] User logged in`
|
|
10
|
+
- Global exception filter with proper error responses
|
|
11
|
+
- No colors, no timestamps, no PID — optimized for AWS CloudWatch
|
|
12
|
+
- Configurable log level
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
## Installation
|
|
14
15
|
|
|
15
16
|
```bash
|
|
16
|
-
npm install
|
|
17
|
+
npm install @iamnnort/nestjs-logger
|
|
17
18
|
# or
|
|
18
|
-
yarn add
|
|
19
|
+
yarn add @iamnnort/nestjs-logger
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
## Usage
|
|
@@ -23,25 +24,11 @@ yarn add pino-pretty
|
|
|
23
24
|
**app.module.ts**
|
|
24
25
|
|
|
25
26
|
```ts
|
|
26
|
-
import { RequestMethod } from '@nestjs/common';
|
|
27
27
|
import { Module } from '@nestjs/common';
|
|
28
28
|
import { LoggerModule } from '@iamnnort/nestjs-logger';
|
|
29
29
|
|
|
30
30
|
@Module({
|
|
31
|
-
imports: [
|
|
32
|
-
LoggerModule.forRoot({
|
|
33
|
-
pinoHttp: {
|
|
34
|
-
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
|
|
35
|
-
transport:
|
|
36
|
-
process.env.NODE_ENV !== 'production'
|
|
37
|
-
? { target: 'pino-pretty', options: { colorize: true } }
|
|
38
|
-
: undefined,
|
|
39
|
-
genReqId: (req) => req.headers['x-request-id'] ?? crypto.randomUUID(),
|
|
40
|
-
},
|
|
41
|
-
forRoutes: ['*'],
|
|
42
|
-
exclude: [{ method: RequestMethod.ALL, path: 'health' }],
|
|
43
|
-
}),
|
|
44
|
-
],
|
|
31
|
+
imports: [LoggerModule.forRoot()],
|
|
45
32
|
})
|
|
46
33
|
export class AppModule {}
|
|
47
34
|
```
|
|
@@ -50,66 +37,93 @@ export class AppModule {}
|
|
|
50
37
|
|
|
51
38
|
```ts
|
|
52
39
|
import { NestFactory } from '@nestjs/core';
|
|
40
|
+
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
53
41
|
import { AppModule } from './app.module';
|
|
54
|
-
import {
|
|
42
|
+
import { LoggerService } from '@iamnnort/nestjs-logger';
|
|
55
43
|
|
|
56
44
|
async function bootstrap() {
|
|
57
|
-
const app = await NestFactory.create(AppModule, {
|
|
45
|
+
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
|
46
|
+
bufferLogs: true,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const loggerService = await app.resolve(LoggerService);
|
|
58
50
|
|
|
59
|
-
app.useLogger(
|
|
51
|
+
app.useLogger(loggerService);
|
|
60
52
|
|
|
61
53
|
await app.listen(3000);
|
|
62
54
|
}
|
|
55
|
+
|
|
63
56
|
bootstrap();
|
|
64
57
|
```
|
|
65
58
|
|
|
66
|
-
|
|
59
|
+
### Log level
|
|
67
60
|
|
|
68
|
-
|
|
61
|
+
Set the minimum level with `LoggerModule.forRoot({ level: 'debug' })`. Levels (most to least verbose): `trace`, `debug`, `info`, `warn`, `error`, `fatal`. Default is `info`.
|
|
69
62
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
| `assignResponse` | Include assigned fields in response logs |
|
|
63
|
+
```ts
|
|
64
|
+
@Module({
|
|
65
|
+
imports: [LoggerModule.forRoot({ level: 'debug' })],
|
|
66
|
+
})
|
|
67
|
+
export class AppModule {}
|
|
68
|
+
```
|
|
77
69
|
|
|
78
70
|
### Using the logger in your code
|
|
79
71
|
|
|
72
|
+
Inject `LoggerService` and set the context to get `[ClassName]` prefix in all log messages:
|
|
73
|
+
|
|
80
74
|
```ts
|
|
81
|
-
import { Controller } from '@nestjs/common';
|
|
82
|
-
import {
|
|
75
|
+
import { Controller, Get, Post } from '@nestjs/common';
|
|
76
|
+
import { LoggerService } from '@iamnnort/nestjs-logger';
|
|
83
77
|
|
|
84
78
|
@Controller()
|
|
85
79
|
export class AppController {
|
|
86
|
-
constructor(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
) {}
|
|
80
|
+
constructor(private readonly loggerService: LoggerService) {
|
|
81
|
+
this.loggerService.setContext(AppController.name);
|
|
82
|
+
}
|
|
90
83
|
|
|
91
84
|
@Get()
|
|
92
85
|
get() {
|
|
93
|
-
this.
|
|
94
|
-
|
|
95
|
-
|
|
86
|
+
this.loggerService.log('Handling GET request');
|
|
87
|
+
return { success: true };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@Post()
|
|
91
|
+
post() {
|
|
92
|
+
this.loggerService.error('Something failed');
|
|
93
|
+
return { success: false };
|
|
96
94
|
}
|
|
97
95
|
}
|
|
98
96
|
```
|
|
99
97
|
|
|
100
|
-
|
|
98
|
+
### Global exception filter
|
|
101
99
|
|
|
102
|
-
|
|
100
|
+
The module registers a global exception filter automatically. It returns proper error responses for both HTTP exceptions and unhandled errors:
|
|
103
101
|
|
|
104
|
-
```
|
|
105
|
-
|
|
102
|
+
```json
|
|
103
|
+
// BadRequestException('Example error')
|
|
104
|
+
{ "message": "Example error", "error": "Bad Request", "statusCode": 400 }
|
|
105
|
+
|
|
106
|
+
// throw new Error('Something went wrong.')
|
|
107
|
+
{ "message": "Something went wrong.", "error": "Internal Server Error", "statusCode": 500 }
|
|
106
108
|
```
|
|
107
109
|
|
|
108
|
-
|
|
110
|
+
## Output
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
INFO: [NestFactory] Application is starting...
|
|
114
|
+
INFO: [NestApplication] Application started.
|
|
115
|
+
INFO: [Http] GET / 200 (3ms)
|
|
116
|
+
INFO: [Http] POST / 200 (1ms)
|
|
117
|
+
INFO: [Http] POST /http-error 400 (2ms)
|
|
118
|
+
ERROR: [AppController] User error.
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Example
|
|
122
|
+
|
|
123
|
+
An example app lives in [`example/`](example/). To run it:
|
|
109
124
|
|
|
110
125
|
```bash
|
|
111
|
-
|
|
112
|
-
curl -X POST http://localhost:3000 -H "Content-Type: application/json" -d '{"foo":"bar"}'
|
|
126
|
+
yarn start
|
|
113
127
|
```
|
|
114
128
|
|
|
115
129
|
## License
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iamnnort/nestjs-logger",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Logger module for NestJS - Simple - Informative - Pretty",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,13 +17,8 @@
|
|
|
17
17
|
"files": [
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"types": "./dist/index.d.ts",
|
|
23
|
-
"import": "./dist/index.mjs",
|
|
24
|
-
"require": "./dist/index.js"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
27
22
|
"engines": {
|
|
28
23
|
"node": ">=22.0.0"
|
|
29
24
|
},
|