npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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.

  1. Console transport The Console transport sends log messages to the console.
...
    transports: [new winston.transports.Console()]
...
  1. 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