Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ubie/nslog

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ubie/nslog

A structured logger for NestJS

  • 1.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
920
decreased by-22.56%
Maintainers
0
Weekly downloads
 
Created
Source

nslog

nslog is a structured logger for NestJS. In production, you can output logs in JSON format, making it compatible with services like Cloud Logging. In local development, you can output logs in a color-coded format in the terminal for easy viewing.

Example

JSON format

json

Text format

text

Installation

npm install --save @ubie/nslog

Usage

import { NestFactory } from "@nestjs/core";
import { StructuredLogger } from "@ubie/nslog";

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { bufferLogs: true });
  app.useLogger(new StructuredLogger({ logLevel: "debug", format: "json" }));

  await app.listen(3000);
}
import { Logger, Injectable } from "@nestjs/common";

@Injectable()
class MyService {
  private readonly logger = new Logger(MyService.name);

  doSomething() {
    this.logger.log("Doing something...");
  }
}

see also: https://docs.nestjs.com/techniques/logger

Additional common parameter

If you have common parameters that you want to output in all logs, you can override the printMessage.

import { Injectable } from "@nestjs/common";
import {
  PrintMessageArgs,
  StructuredLogger,
  StructuredLoggerOptions,
} from "./structured-logger";

@Injectable()
class MyLogger extends StructuredLogger {
  protected printMessage(args: PrintMessageArgs): void {
    args.params.push({ requestId: getRequestId() });
    super.printMessage(args);
  }
}

Error logging

Passing an Error object to logger.error is handled specially.

const new Error("something went wrong");
logger.error(err);

This will be output as follows.

{
  "severity": "ERROR",
  "message": "something went wrong",
  "stack_trace": "Error: something went wrong\n    at Object.<anonymous> (/path/to/app.js:1:13)\n    at Module._compile (node:internal/modules/cjs/loader:1376:14)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)\n    at Module.load (node:internal/modules/cjs/loader:1207:32)\n    at Module._load (node:internal/modules/cjs/loader:1023:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)\n    at node:internal/main/run_main_module:28:49"
}

License

MIT License.

FAQs

Package last updated on 12 Jul 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc