@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.
Files changed (2) hide show
  1. package/README.md +67 -53
  2. 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 using [nestjs-pino](https://github.com/iamolegga/nestjs-pino) — structured JSON logging with **automatic HTTP request/response logging**.
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
- ## Installation
5
+ ## Features
6
6
 
7
- ```bash
8
- npm install @iamnnort/nestjs-logger pino-http
9
- # or
10
- yarn add @iamnnort/nestjs-logger pino-http
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
- For pretty-printed logs in development:
14
+ ## Installation
14
15
 
15
16
  ```bash
16
- npm install pino-pretty
17
+ npm install @iamnnort/nestjs-logger
17
18
  # or
18
- yarn add pino-pretty
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 { Logger } from '@iamnnort/nestjs-logger';
42
+ import { LoggerService } from '@iamnnort/nestjs-logger';
55
43
 
56
44
  async function bootstrap() {
57
- const app = await NestFactory.create(AppModule, { bufferLogs: true });
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(app.get(Logger));
51
+ app.useLogger(loggerService);
60
52
 
61
53
  await app.listen(3000);
62
54
  }
55
+
63
56
  bootstrap();
64
57
  ```
65
58
 
66
- Every HTTP request and response is logged automatically. Use `Logger` (NestJS-style) or inject `PinoLogger` for full Pino API and request-scoped fields (e.g. `PinoLogger.assign({ userId })`).
59
+ ### Log level
67
60
 
68
- ### Configuration options
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
- | Option | Description |
71
- |----------------|-----------------------------------------------------------------------------|
72
- | `pinoHttp` | [pino-http](https://github.com/pinojs/pino-http#api) options (level, transport, customAttributeKeys, genReqId, etc.) |
73
- | `forRoutes` | Routes where the HTTP logger runs (default: `['*']`) |
74
- | `exclude` | Routes to skip (e.g. health checks) |
75
- | `renameContext`| Rename the `context` key in logs (e.g. `'service'`) |
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 { Logger, PinoLogger } from '@iamnnort/nestjs-logger';
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
- private readonly logger: Logger, // NestJS LoggerService API
88
- private readonly pino: PinoLogger, // Full Pino API + request context
89
- ) {}
80
+ constructor(private readonly loggerService: LoggerService) {
81
+ this.loggerService.setContext(AppController.name);
82
+ }
90
83
 
91
84
  @Get()
92
85
  get() {
93
- this.logger.log('Handling GET');
94
- this.pino.assign({ userId: '123' }); // Added to all logs in this request
95
- return { ok: true };
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
- ## Example
98
+ ### Global exception filter
101
99
 
102
- Run the example app (build the library first):
100
+ The module registers a global exception filter automatically. It returns proper error responses for both HTTP exceptions and unhandled errors:
103
101
 
104
- ```bash
105
- yarn build && yarn --cwd example start
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
- Then send requests to see request/response logs:
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
- curl http://localhost:3000
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.0.0",
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
- "exports": {
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
  },