@infineit/winston-logger 1.0.25 → 1.0.27
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 +106 -91
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,9 +10,14 @@
|
|
|
10
10
|
|
|
11
11
|
## Introduction
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
`@infineit/winston-logger` is a lightweight project for implements a production-ready logger for Nest.js applications using Winston, Morgan and Prisma. This package simplifies the setup of logger and provides configurable options for instrumenting your application.
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- **Easy Integration**: Quickly integrate logger with winston for distributed log.
|
|
19
|
+
- **Configurable**: Provides options to customize logger configuration.
|
|
20
|
+
- **Lightweight**: Minimal dependencies and overhead.
|
|
16
21
|
|
|
17
22
|
That implements a production-ready system advanced or basic Microservices or Monoliths projects applying concepts like:
|
|
18
23
|
|
|
@@ -22,42 +27,88 @@ That implements a production-ready system advanced or basic Microservices or Mon
|
|
|
22
27
|
* Logging Rules
|
|
23
28
|
* Log formatters
|
|
24
29
|
|
|
25
|
-
##
|
|
30
|
+
## Installation
|
|
26
31
|
|
|
27
|
-
|
|
32
|
+
* You can install this package using npm:
|
|
33
|
+
```bash
|
|
34
|
+
npm install @infineit/winston-logger
|
|
35
|
+
```
|
|
28
36
|
|
|
29
|
-
|
|
37
|
+
Or using Yarn:
|
|
38
|
+
```bash
|
|
39
|
+
yarn add @infineit/winston-logger
|
|
40
|
+
```
|
|
30
41
|
|
|
31
|
-
|
|
32
|
-
* **api**: It contains the REST API developed with Nest.js.
|
|
33
|
-
* **libs**: It contains the project packages or Bounded Contexts on a DDD language.
|
|
34
|
-
* **shared**: It contains the code of the Shared Kernel.
|
|
35
|
-
* **src**: It contains the source code.
|
|
36
|
-
* **config**: It contains de config module.
|
|
37
|
-
* **context**: It contains the context module.
|
|
38
|
-
* **logger**: It contains the logger module.
|
|
42
|
+
### Prerequisites
|
|
39
43
|
|
|
40
|
-
|
|
44
|
+
- NestJS
|
|
45
|
+
- Winston
|
|
46
|
+
- morgon
|
|
47
|
+
- prisma
|
|
48
|
+
|
|
49
|
+
### Getting Started
|
|
50
|
+
|
|
51
|
+
Follow these steps to set up and run logger. If all steps are followed correctly, logger should start without any issues:
|
|
52
|
+
|
|
53
|
+
1. To use `@infineit/winston-logger` in your NestJS application, simply import it of your `main.ts` file:
|
|
41
54
|
|
|
42
|
-
|
|
55
|
+
```bash
|
|
56
|
+
import { NestjsLoggerServiceAdapter } from '@infineit/winston-logger';;
|
|
43
57
|
|
|
44
|
-
|
|
58
|
+
const app = await NestFactory.create(AppModule, {bufferLogs: true });
|
|
45
59
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* **Infrastructure**
|
|
60
|
+
app.useLogger(app.get(NestjsLoggerServiceAdapter));
|
|
61
|
+
```
|
|
49
62
|
|
|
50
|
-
|
|
63
|
+
2. import in app.module.ts:
|
|
51
64
|
|
|
52
|
-
|
|
65
|
+
```bash
|
|
66
|
+
import { ContextModule, LoggerModule } from '@infineit/winston-logger';
|
|
53
67
|
|
|
54
|
-
|
|
68
|
+
@Module({
|
|
69
|
+
imports: [
|
|
70
|
+
ContextModule,
|
|
71
|
+
LoggerModule.forRoot(PrismaService),
|
|
72
|
+
]
|
|
73
|
+
})
|
|
74
|
+
```
|
|
55
75
|
|
|
56
|
-
|
|
76
|
+
3. app.service.ts:
|
|
57
77
|
|
|
58
|
-
|
|
78
|
+
```bash
|
|
79
|
+
import Logger, { LoggerKey } from '@infineit/winston-logger';
|
|
59
80
|
|
|
60
|
-
|
|
81
|
+
constructor(@Inject(LoggerKey) private logger: Logger) {}
|
|
82
|
+
|
|
83
|
+
this.logger.info('I am an info message!', {
|
|
84
|
+
props: {
|
|
85
|
+
foo: 'bar',
|
|
86
|
+
baz: 'qux',
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**Reminder**: Make sure prisma is initialize and winstonlog modal is running before starting your NestJS application to avoid initialization errors.
|
|
92
|
+
|
|
93
|
+
## Configuration
|
|
94
|
+
|
|
95
|
+
You can configure the Logger using environment variables in your `.env` file:
|
|
96
|
+
|
|
97
|
+
- `NODE_ENV`: development / staging / testing / production.
|
|
98
|
+
- `LOGGER_ORGANIZATION`: The name of your organization as it will appear in log.
|
|
99
|
+
- `LOGGER_CONTEXT`: The name of your context as it will appear in log.
|
|
100
|
+
- `LOGGER_APP`: The name of your app as it will appear in log.
|
|
101
|
+
- `LOGGER_DATABASE_STORAGE`: True/False. Default True for production or testing, It will store log to database.
|
|
102
|
+
- `LOGGER_LOG_LEVEL`: Default log level warn,error,fatal for production or testing, It will log.
|
|
103
|
+
- `LOGGER_DURATION`: True/False. Default False, It will store request duration in database.
|
|
104
|
+
- `LOGGER_DURATION_LOG_LEVEL`: Default log level warn,error,fatal for production or testing, It will calculate duration for request.
|
|
105
|
+
- `LOGGER_CONSOLE_PRINT`: True/False. Default False for production or testing.
|
|
106
|
+
- `LOGGER_LOG_IN_FILE`: True/False. Default False for production or testing.
|
|
107
|
+
- `LOGGER_SLACK_INC_WEBHOOK_URL`: Slack url, eg. `https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX`.
|
|
108
|
+
|
|
109
|
+
## Project structure
|
|
110
|
+
|
|
111
|
+
## Key concepts
|
|
61
112
|
|
|
62
113
|
### NestJS Logger
|
|
63
114
|
|
|
@@ -77,78 +128,42 @@ To add custom data to all the logs, we use a wrapper `LoggerContextWrapper`.
|
|
|
77
128
|
|
|
78
129
|
That class is injected with a Transient scope. By that, we can get the caller class and add it to the logs.
|
|
79
130
|
|
|
80
|
-
##
|
|
81
|
-
|
|
82
|
-
* Install dependencies
|
|
83
|
-
```sh
|
|
84
|
-
yarn install
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
* Copy file .env.example a .env
|
|
88
|
-
```sh
|
|
89
|
-
cp .env.example .env
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
* Start REST API
|
|
93
|
-
```sh
|
|
94
|
-
yarn start:dev api
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
* Test REST API
|
|
98
|
-
```sh
|
|
99
|
-
curl -X GET http://localhost:3000
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
* Test REST API with correlation ID
|
|
103
|
-
```sh
|
|
104
|
-
curl -X GET http://localhost:3000 -H "x-correlation-id: 87815cc5-d0f2-41e5-a731-ac55bbb733e8"
|
|
105
|
-
```
|
|
106
|
-
## Publish on npm
|
|
131
|
+
## Prisma Table
|
|
107
132
|
|
|
108
|
-
* Build file through nestjs-logger/libs/shared/src
|
|
109
|
-
```sh
|
|
110
|
-
yarn build
|
|
111
133
|
```
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
134
|
+
model winstonlog {
|
|
135
|
+
id_log String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
|
136
|
+
level String @db.VarChar(80)
|
|
137
|
+
message String @db.Text
|
|
138
|
+
context String? @db.VarChar(255)
|
|
139
|
+
correlationId String? @db.Uuid
|
|
140
|
+
sourceClass String? @db.VarChar(255)
|
|
141
|
+
props Json?
|
|
142
|
+
organization String? @db.VarChar(40)
|
|
143
|
+
app String? @db.VarChar(40)
|
|
144
|
+
durationMs Decimal? @default(0) @db.Decimal(10, 4)
|
|
145
|
+
stack String? @db.Text
|
|
146
|
+
label String? @db.VarChar(40)
|
|
147
|
+
timestamp DateTime @default(now()) @db.Timestamptz(6)
|
|
148
|
+
|
|
149
|
+
@@schema("public")
|
|
150
|
+
}
|
|
116
151
|
```
|
|
152
|
+
## Inspiration
|
|
117
153
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
* https://github.com/jnm733/nestjs-logger/tree/main
|
|
121
|
-
|
|
122
|
-
* https://medium.com/@jose-luis-navarro/logging-on-nestjs-like-a-pro-with-correlation-ids-log-aggregation-winston-morgan-and-more-d03e3bb56772
|
|
123
|
-
|
|
124
|
-
* https://github.com/InnovA2/winston-pg/tree/main
|
|
154
|
+
This project is inspired by the excellent work from:
|
|
125
155
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
model winstonlog {
|
|
130
|
-
id_log String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
|
131
|
-
level String @db.VarChar(80)
|
|
132
|
-
message String @db.Text
|
|
133
|
-
context String? @db.VarChar(255)
|
|
134
|
-
correlationId String? @db.Uuid
|
|
135
|
-
sourceClass String? @db.VarChar(255)
|
|
136
|
-
props Json?
|
|
137
|
-
organization String? @db.VarChar(40)
|
|
138
|
-
app String? @db.VarChar(40)
|
|
139
|
-
durationMs Decimal? @default(0) @db.Decimal(10, 4)
|
|
140
|
-
stack String? @db.Text
|
|
141
|
-
label String? @db.VarChar(40)
|
|
142
|
-
timestamp DateTime @default(now()) @db.Timestamptz(6)
|
|
143
|
-
|
|
144
|
-
@@schema("public")
|
|
145
|
-
}
|
|
146
|
-
```
|
|
156
|
+
- [Medium](https://medium.com/@jose-luis-navarro/logging-on-nestjs-like-a-pro-with-correlation-ids-log-aggregation-winston-morgan-and-more-d03e3bb56772)
|
|
157
|
+
- [Logger](https://github.com/jnm733/nestjs-logger/tree/main)
|
|
158
|
+
- [winston-pg](https://github.com/InnovA2/winston-pg/tree/main)
|
|
147
159
|
|
|
148
|
-
##
|
|
160
|
+
## License
|
|
149
161
|
|
|
150
|
-
|
|
162
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
|
|
151
163
|
|
|
152
|
-
|
|
164
|
+
## Author
|
|
153
165
|
|
|
154
|
-
|
|
166
|
+
Dharmesh Patel 🇮🇳 <br>
|
|
167
|
+
- [GitHub](https://github.com/dharmesh-r-patel/nestjs-starter)
|
|
168
|
+
- [LinkedIn](https://www.linkedin.com/in/dharmeshbbay)
|
|
169
|
+
- [Instagram](https://www.instagram.com/dharmesh_numbertank)
|