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

nestjs-pretty-logger

v0.3.1

Published

Elevate NestJS logging with stylish, file-redirected, and real-time capable logging based on consola. Compact, powerful, and easy to integrate

Downloads

283

Readme

NestJS Pretty Logger

1126201123

Introduction

"NestJS Pretty Logger" is a versatile and visually appealing global Logger module for NestJS applications. This module extends the functionality of the default NestJS logger by utilizing the consola library for enhanced aesthetics and includes features like file redirection and real-time logging capabilities.

Features

  • Aesthetic and Functional Logging: Integrates consola for an enhanced logging experience.
  • Log File Redirection: Ability to redirect stdout to files, allowing for organized log management.
  • App-Wide Logging Coverage: Capable of covering the entire application's console and stdout.write functionalities.
  • Real-Time Logging Support: Provides the onData() hook for real-time logging implementations, such as custom log recorders or WebSocket real-time log streaming.

Installation

To install, run the following command:

npm i nestjs-pretty-logger

Usage

Basic Setup

In your main.ts:

// main.ts
import { Logger } from 'nestjs-pretty-logger'

import { NestFactory } from '@nestjs/core'

import { AppModule } from './app.module'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  app.useLogger(app.get(Logger))
  await app.listen(3000)
}
bootstrap()

In your app.module.ts:

// app.module.ts
import { LoggerModule } from 'nestjs-pretty-logger'

import { Module } from '@nestjs/common'

import { AppController, AppService } from './app.controller'

@Module({
  imports: [LoggerModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Custom Logger Configuration

Configure and utilize advanced features:

// Custom Logger Setup
import path from 'path'
import { createLogger, Logger } from 'nestjs-pretty-logger'

import { NestFactory } from '@nestjs/core'

import { AppModule } from './app.module'

const customLogger = createLogger({
  writeToFile: {
    loggerDir: path.resolve(__dirname, './logs'),
  },
})

// Wrap all console and stdout.write with customLogger
customLogger.wrapAll()

// Implement onData hook for real-time logging or custom actions
customLogger.onData((data) => {
  // Your custom implementation here
})

// Only error log
customLogger.onStdErr((data) => {
  // Your custom implementation here
})

// Only non-error log
customLogger.onStdOut((data) => {
  // Your custom implementation here
})

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  Logger.setLoggerInstance(customLogger)
  app.useLogger(app.get(Logger))
  await app.listen(3000)
}
bootstrap()

Note: After invoking wrapAll(), avoid using console.log or similar stdout functions within onData() to prevent potential infinite loops.

Configuration Options

The createLogger method includes the FileReporterConfig interface for file redirection:

interface FileReporterConfig {
  loggerDir: string // Required: Log file directory
  stdoutFileFormat?: string // Optional: Default 'stdout_%d%.log'
  stderrFileFormat?: string // Optional: Default 'error.log'
  cron?: string // Optional: Default '0 0 * * *' for daily log rotation
}

This configuration is crucial for directing stdout to log files, with loggerDir being mandatory for specifying the log directory. The other parameters are optional, offering defaults for file formats and log rotation, which can be changed through the cron setting.

Contributions

Contributions to "NestJS Pretty Logger" are highly appreciated. Whether it's through pull requests or issue discussions, your feedback and contributions are valuable to the project.

License

2023 © Innei, MIT License.

Personal Site · GitHub @Innei