@squareboat/nestjs-logger
v0.0.3
Published
A simplified winston logger for your NestJS Applications
Downloads
275
Readme
Nestjs Logger
A simplified winston logger for your Nestjs applications
Description
This package provides a NestJS logger wrapper around the Winston logger library. It allows you to easily configure and use Winston logger within your NestJS application
Table Of Content
Installation
To install the package, run
npm install @squareboat/nestjs-logger
OR
yarn add @squareboat/nestjs-logger
Getting Started
To register LoggerModule
with your app, import the module inside AppModule
.
Static Registration
To register the module statically you can do
import { Module } from '@nestjs/common'
import { LoggerModule } from '@squareboat/nestjs-logger'
import winston from 'winston'
@Module({
imports: [
LoggerModule.register({
isGlobal: true,
default: 'properties',
disableConsole: false,
loggers: {
properties: {
level: 'error',
transports: [new winston.transports.Console()],
},
},
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
Recommended Way
1. Create logger.ts file
Use ConfigModule
provided by NestJS to load configurations. To learn about ConfigModule, Click here.
import { registerAs } from '@nestjs/config'
import winston from 'winston'
import path from 'path'
const { combine, timestamp, printf } = winston.format
const myFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`
})
export default registerAs('logger', () => {
return {
default: 'payments',
disableConsole: process.env.APP_ENV == 'local' ? false : true,
loggers: {
payments: {
level: 'error',
format: combine(timestamp(), myFormat),
transports: [
new winston.transports.File({
filename: path.join(process.cwd(), 'log', '/payments.log'),
level: 'error',
}),
],
},
},
}
})
By setting disbaleConsole
to true you can restrict your console logs to be printed in production or other enviroments.
Note that you can define multiple transports for the same logger
2. Register Logger Module
import { Module } from '@nestjs/common'
import { LoggerModule } from '@squareboat/nestjs-logger'
@Module({
imports: [
LoggerModule.registerAsync({
isGlobal: true,
imports: [ConfigModule],
useFactory: (config: ConfigService) => config.get('logger'),
inject: [ConfigService],
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
Knowing about Transports
Transports are the mechanisms by which log messages are delivered to their intended destinations, such as the console, files, or remote servers such as CloudWatch.
To read about other transports that you can use refer to Winston docs.
- Console transport
The
Console
transport sends log messages to the console.
...
transports: [new winston.transports.Console()]
...
- File transport
The
File
transport writes log messages to a file.It is recommended to create your log files inside the root directory under the 'log' folder.
...
transports: [
new winston.transports.File({
filename: path.join(process.cwd(), 'log', '/payments.log')
})
]
...
Usage
First import Logger from @squareboat/nestjs-logger
import { Logger } from '@squareboat/nestjs-logger';
To use the logger named "payments" in your application and log an "info" level message, you can use the following code:
Logger('payments').info('Payment successful!!');
To know more about other logging levels refer here.
Available Commands
We have added some useful commands which we think will add value to your workflow.
node cli logger:rotate-log
You can use this command to create a zip of all .logs
file under your root log directory.
Contributing
To know about contributing to this package, read the guidelines here
About Us
We are a bunch of dreamers, designers, and futurists. We are high on collaboration, low on ego, and take our happy hours seriously. We'd love to hear more about your product. Let's talk and turn your great ideas into something even greater! We have something in store for everyone. ☎️ 📧 Connect with us!
We are hiring! Apply now at careers page
License
The MIT License. Please see License File for more information. Copyright © 2023 SquareBoat.
Made with ❤️ by Squareboat