1
1
## Description
2
2
3
- Winston and Nest-Winston opionated configuration for NestJS. This package depends on @gedai/core and @nestjs/config , be sure to install and setup those for everything to work as expected.
3
+ Opionated common configuration for NestJS services . This package depends on @gedai/core and @nestjs/config . Be sure to install and setup those for everything to work as expected.
4
4
5
- This package uses the following environment variables during its setup:
5
+ ## Configuration:
6
+
7
+ This package uses a few environment variables for configuration. The folowing lists the available values for each configuration variable.
6
8
7
9
```
8
10
NODE_ENV="{development,testing,staging,production}"
9
- APP_NAME ="myApp"
11
+ SERVICE_NAME ="myApp"
10
12
LOG_LEVEL="{debug,info,warn,error}"
13
+ LOG_FORMAT="{pretty,json}"
14
+ INSPECT_HTTP_TRAFFIC="{all,none,inbound,outbound}"
11
15
```
12
16
13
17
- development and testing will output colorized format
14
18
- staging and production will output json format
15
19
16
- ## Installation
20
+ ## Getting Started
21
+
22
+ ### Step 1: Installation
17
23
18
24
``` bash
19
- $ npm install @gedai/core @gedai/logger @nestjs/config
25
+ $ npm install @gedai/core @gedai/common @nestjs/config
20
26
```
21
27
22
- ## Running the app
28
+ ### Step 2: The Setup
23
29
24
- ### Step 1:
30
+ Import the required modules
31
+
32
+ ``` typescript
33
+ // app.module.ts
34
+ import { ContextModule } from ' @gedai/core' ;
35
+ import { Module } from ' @nestjs/common' ;
36
+ import { ConfigModule } from ' @nestjs/config' ;
37
+ import { randomUUID } from ' crypto' ;
38
+ import { AppController } from ' ./app.controller' ;
39
+ import { AppService } from ' ./app.service' ;
40
+
41
+ @Module ({
42
+ imports: [
43
+ // <<-- Setup Config Module Here -->>
44
+ ConfigModule .forRoot ({ isGlobal: true }),
45
+ // <<-- Setup Context Module Here -->>
46
+ ContextModule .forRoot ({}),
47
+ ],
48
+ controllers: [AppController ],
49
+ providers: [AppService ],
50
+ })
51
+ export class AppModule {}
52
+ ```
25
53
26
54
Apply global wide configuration in main.ts
27
55
28
56
``` typescript
29
57
// app.module.ts
30
- import { configureLogger } from ' @gedai/logger' ;
58
+ import { configureContextWrappers } from ' @gedai/core' ;
59
+ import { configureLogger } from ' @gedai/common' ;
31
60
import { NestFactory } from ' @nestjs/core' ;
32
61
import { AppModule } from ' ./app.module' ;
33
62
34
63
async function bootstrap() {
35
- const app = await NestFactory .create (AppModule );
36
- configureLogger (app );
64
+ const app = await NestFactory .create (AppModule )
65
+ // <<-- Setup Context Wrappers Here -->>
66
+ .then (configureContextWrappers ())
67
+ // <<-- Setup Logger Here -->>
68
+ .then (configureLogger ());
37
69
await app .listen (3000 );
38
70
}
39
71
bootstrap ();
40
72
```
41
73
42
- ## Step 2:
74
+ ### Step 3: Do Magic!
43
75
44
76
You can now use the Nest's default logger with preconfigured setup.
45
77
@@ -50,22 +82,32 @@ import { Injectable } from '@nestjs/common';
50
82
51
83
@Injectable ()
52
84
export class AppService {
85
+ // <<-- Create a scoped instance of nest's Logger -->>
53
86
private logger = new Logger (this .constructor .name );
54
87
55
88
constructor (private readonly context : ContextService ) {}
56
89
57
90
getHello(): string {
58
- const message = ' Hello, World!' ;
59
- this .logger .log (message );
60
- // outputs: [nest-app] Info 3/16/2024, 3:16:59 PM [AppService] Hello, World! - {"traceId":"20889865-510c-433c-ac0a-a04f27b89d35"}
61
- /* Optionally:
62
- * this.logger.log({ message, ...additionalDataForLogs });
63
- */
91
+ // <<-- Whenever you use the logger, it will be tracked with the contextId-->>
92
+ this .logger .log (' Hello, Jack!' );
64
93
return message ;
65
94
}
66
95
}
67
96
```
68
97
98
+ ## Features
99
+
100
+ - Logging
101
+ - Anonymized keys on logged objects
102
+ - Inbound HTTP traffic inspection
103
+ - Outbound HTTP traffic inspection
104
+ - Compression
105
+ - Helmet
106
+ - Validation
107
+ - CORS
108
+ - API Versioning
109
+ - Route Prefixing
110
+
69
111
## License
70
112
71
113
Gedai is [ MIT licensed] ( LICENSE ) .
0 commit comments