@infineit/winston-logger 1.0.25 → 1.0.26

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 +118 -88
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -10,9 +10,14 @@
10
10
 
11
11
  ## Introduction
12
12
 
13
- This project implements a production-ready logger for Nest.js applications using Winston and Morgan on a clean architecture (Hexagonal Architecture).
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
- You can read a detailed explanation of the project on the following article: https://medium.com/p/d03e3bb56772/edit
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,98 @@ That implements a production-ready system advanced or basic Microservices or Mon
22
27
  * Logging Rules
23
28
  * Log formatters
24
29
 
25
- ## Project structure
30
+ ## Installation
26
31
 
27
- The project is structured in a Nest.js monorepo.
32
+ * You can install this package using npm:
33
+ ```bash
34
+ npm install @infineit/winston-logger
35
+ ```
28
36
 
29
- The folder structure is as follows:
37
+ Or using Yarn:
38
+ ```bash
39
+ yarn add @infineit/winston-logger
40
+ ```
30
41
 
31
- * **apps**: It contains the project's executable applications, in this case, the REST API.
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.
39
42
 
40
- ## Key concepts
43
+ ## Usage
44
+
45
+ ### Basic Setup
46
+
47
+ To use `@infineit/winston-logger` in your NestJS application, simply import it of your `main.ts` file:
48
+
49
+ ```sh
50
+ import { NestjsLoggerServiceAdapter } from '@infineit/winston-logger';;
51
+ ```
52
+
53
+ ### Prerequisites
54
+
55
+ - NestJS
56
+ - Winston
57
+ - morgon
58
+ - prisma
59
+
60
+
61
+ ### Getting Started
62
+
63
+ Follow these steps to set up and run logger. If all steps are followed correctly, logger should start without any issues:
64
+
65
+ 1. use logger after import in main.ts:
66
+
67
+ ```bash
68
+ const app = await NestFactory.create(AppModule, {bufferLogs: true });
69
+
70
+ app.useLogger(app.get(NestjsLoggerServiceAdapter));
71
+ ```
72
+
73
+ 2. import in app.module.ts:
74
+
75
+ ```bash
76
+ import { ContextModule, LoggerModule } from '@infineit/winston-logger';
77
+
78
+ @Module({
79
+ imports: [
80
+ ContextModule,
81
+ LoggerModule.forRoot(PrismaService),
82
+ ]
83
+ })
84
+ ```
85
+
86
+ 3. app.service.ts:
41
87
 
42
- ### Architecture
88
+ ```bash
89
+ import Logger, { LoggerKey } from '@infineit/winston-logger';
43
90
 
44
- The project is structured on a Hexagonal Architecture, so it has the following layers:
91
+ constructor(@Inject(LoggerKey) private logger: Logger) {}
45
92
 
46
- * **Application**
47
- * **Domain**
48
- * **Infrastructure**
93
+ this.logger.info('I am an info message!', {
94
+ props: {
95
+ foo: 'bar',
96
+ baz: 'qux',
97
+ },
98
+ });
99
+ ```
49
100
 
50
- ### Logging Libraries
101
+ **Reminder**: Make sure prisma is initialize and winstonlog modal is running before starting your NestJS application to avoid initialization errors.
51
102
 
52
- We use Winston to manage the logs and Morgan to log the HTTP requests. All that is managed by the `LoggerModule`.
103
+ ## Configuration
53
104
 
54
- To manage Winston transports, we use the `WinstonLoggerTransportsKey` DI token that is defined on the `LoggerModule`.
105
+ You can configure the Logger using environment variables in your `.env` file:
55
106
 
56
- ### Decoupling Logging library
107
+ - `NODE_ENV`: development / staging / testing / production.
108
+ - `LOGGER_ORGANIZATION`: The name of your organization as it will appear in log.
109
+ - `LOGGER_CONTEXT`: The name of your context as it will appear in log.
110
+ - `LOGGER_APP`: The name of your app as it will appear in log.
111
+ - `LOGGER_DATABASE_STORAGE`: True/False. Default True for production or testing, It will store log to database.
112
+ - `LOGGER_LOG_LEVEL`: Default log level warn,error,fatal for production or testing, It will log.
113
+ - `LOGGER_DURATION`: True/False. Default False, It will store request duration in database.
114
+ - `LOGGER_DURATION_LOG_LEVEL`: Default log level warn,error,fatal for production or testing, It will calculate duration for request.
115
+ - `LOGGER_CONSOLE_PRINT`: True/False. Default False for production or testing.
116
+ - `LOGGER_LOG_IN_FILE`: True/False. Default False for production or testing.
117
+ - `LOGGER_SLACK_INC_WEBHOOK_URL`: Slack url, eg. https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX.
57
118
 
58
- To decouple the Logging library from our domain, we have created a `Logger` interface that is implemented by the `WinstonLogger` class.
119
+ ## Project structure
59
120
 
60
- If we want to change the logging library, we only have to implement the `Logger` interface and update the dependecy on the `LoggerModule`.
121
+ ## Key concepts
61
122
 
62
123
  ### NestJS Logger
63
124
 
@@ -77,78 +138,47 @@ To add custom data to all the logs, we use a wrapper `LoggerContextWrapper`.
77
138
 
78
139
  That class is injected with a Transient scope. By that, we can get the caller class and add it to the logs.
79
140
 
80
- ## Installation
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
141
+ ## Prisma Table
107
142
 
108
- * Build file through nestjs-logger/libs/shared/src
109
- ```sh
110
- yarn build
111
143
  ```
112
-
113
- * got to nestjs-logger/dist/libs/nestjs
114
- ```sh
115
- npm publish --access public
144
+ model winstonlog {
145
+ id_log String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
146
+ level String @db.VarChar(80)
147
+ message String @db.Text
148
+ context String? @db.VarChar(255)
149
+ correlationId String? @db.Uuid
150
+ sourceClass String? @db.VarChar(255)
151
+ props Json?
152
+ organization String? @db.VarChar(40)
153
+ app String? @db.VarChar(40)
154
+ durationMs Decimal? @default(0) @db.Decimal(10, 4)
155
+ stack String? @db.Text
156
+ label String? @db.VarChar(40)
157
+ timestamp DateTime @default(now()) @db.Timestamptz(6)
158
+
159
+ @@schema("public")
160
+ }
116
161
  ```
162
+ ## Inspiration
117
163
 
118
- ## Inspire by
164
+ This project is inspired by the excellent work from:
119
165
 
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
166
+ - [Medium](https://medium.com/@jose-luis-navarro/logging-on-nestjs-like-a-pro-with-correlation-ids-log-aggregation-winston-morgan-and-more-d03e3bb56772)
167
+ - [Logger GitHub Repository](https://github.com/SigNoz/signoz)
168
+ - [winston-pg GitHub Repository](https://github.com/InnovA2/winston-pg/tree/main)
123
169
 
124
- * https://github.com/InnovA2/winston-pg/tree/main
125
170
 
126
- ## Prisma Table
171
+ ## Support
127
172
 
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
- ```
173
+ NestJS is an MIT-licensed open-source project that thrives with support from the community. If you’d like to contribute or sponsor, please [learn more here](https://docs.nestjs.com/support).
147
174
 
148
- ## To do
175
+ ## License
149
176
 
150
- We will continue working on this project to add new features
177
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
151
178
 
152
- PRs are welcome!
179
+ ## Author
153
180
 
154
- - [ ] Add testing.
181
+ Dharmesh Patel 🇮🇳 <br>
182
+ - [GitHub](https://github.com/dharmesh-r-patel/nestjs-starter)
183
+ - [LinkedIn](https://www.linkedin.com/in/dharmeshbbay)
184
+ - [Instagram](https://www.instagram.com/dharmesh_numbertank)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infineit/winston-logger",
3
- "version": "1.0.25",
3
+ "version": "1.0.26",
4
4
  "description": "Enterprise-level logger integration package",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",