Socket
Socket
Sign inDemoInstall

nestjs-pretty-logger

Package Overview
Dependencies
19
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nestjs-pretty-logger

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


Version published
Weekly downloads
495
decreased by-24.43%
Maintainers
1
Install size
4.70 MB
Created
Weekly downloads
 

Changelog

Source

0.2.2 (2024-02-29)

Features

  • error to stdout (5def54f)

Readme

Source

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

FAQs

Last updated on 29 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc