Gathering detailed insights and metrics for @infineit/winston-logger
Gathering detailed insights and metrics for @infineit/winston-logger
Gathering detailed insights and metrics for @infineit/winston-logger
Gathering detailed insights and metrics for @infineit/winston-logger
npm install @infineit/winston-logger
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A Nest.js production-ready logger implementation.
@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.
That implements a production-ready system advanced or basic Microservices or Monoliths projects applying concepts like:
You can install this package using npm:
1npm install @infineit/winston-logger
Or using Yarn:
1yarn add @infineit/winston-logger
Follow these steps to set up and run logger. If all steps are followed correctly, logger should start without any issues:
To use @infineit/winston-logger
in your NestJS application, simply import it of your main.ts
file:
1 import { NestjsLoggerServiceAdapter } from '@infineit/winston-logger';; 2 3 const app = await NestFactory.create(AppModule, {bufferLogs: true }); 4 5 app.useLogger(app.get(NestjsLoggerServiceAdapter));
import in app.module.ts:
1 import { ContextModule, LoggerModule } from '@infineit/winston-logger'; 2 3 @Module({ 4 imports: [ 5 ContextModule, 6 LoggerModule.forRoot(PrismaService), 7 ] 8 })
app.service.ts:
1 import Logger, { LoggerKey } from '@infineit/winston-logger'; 2 3 constructor(@Inject(LoggerKey) private logger: Logger) {} 4 5 this.logger.info('I am an info message!', { 6 props: { 7 foo: 'bar', 8 baz: 'qux', 9 }, 10 });
Reminder: Make sure prisma is initialize and winstonlog modal is running before starting your NestJS application to avoid initialization errors.
You can configure the Logger using environment variables in your .env
file:
NODE_ENV
: development / staging / testing / production.LOGGER_ORGANIZATION
: The name of your organization as it will appear in log.LOGGER_CONTEXT
: The name of your context as it will appear in log.LOGGER_APP
: The name of your app as it will appear in log.LOGGER_DATABASE_STORAGE
: True/False. Default True for production or testing, It will store log to database.LOGGER_LOG_LEVEL
: Default log level warn,error,fatal for production or testing, It will log.LOGGER_DURATION
: True/False. Default False, It will store request duration in database.LOGGER_DURATION_LOG_LEVEL
: Default log level warn,error,fatal for production or testing, It will calculate duration for request.LOGGER_CONSOLE_PRINT
: True/False. Default False for production or testing.LOGGER_LOG_IN_FILE
: True/False. Default False for production or testing.LOGGER_SLACK_INC_WEBHOOK_URL
: Slack url, eg. https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX
.NestJS uses a custom logger for bootstrap and internal logging. To use our logger, we need to create an adapter that implements the NestJS LoggerService
interface. That is implemented in the NestjsLoggerServiceAdapter
class.
We pass that adapter to the NestJS app on the main.ts
file.
To manage correlation IDs, we use nestjs-cls
library that implements a Local Storage. With that, we can isolate and share data on a request lifecycle.
The system reads the x-correlation-id
HTTP header and stores it on the Local Storage. If the header is not present, the system generates a new UUID and stores it on the Local Storage.
To add custom data to all the logs, we use a wrapper LoggerContextWrapper
.
That class is injected with a Transient scope. By that, we can get the caller class and add it to the logs.
model winstonlog {
id_log String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
level String @db.VarChar(80)
message String @db.Text
context String? @db.VarChar(255)
correlationId String? @db.Uuid
sourceClass String? @db.VarChar(255)
props Json?
organization String? @db.VarChar(40)
app String? @db.VarChar(40)
durationMs Decimal? @default(0) @db.Decimal(10, 4)
stack String? @db.Text
label String? @db.VarChar(40)
timestamp DateTime @default(now()) @db.Timestamptz(6)
@@schema("public")
}
This project is inspired by the excellent work from:
This project is licensed under the MIT License. See the LICENSE file for more details.
Dharmesh Patel 🇮🇳
No vulnerabilities found.
No security vulnerabilities found.