Code Monkey home page Code Monkey logo

nestjs-custom-extendedlogger's Introduction

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

Install with npm or yarn

npm i @hoplin/nestjs-logger

yarn add @hoplin/nestjs-logger

NestJS custom logger

Extended NestJS Logger, Based on NestJS common logger. Log messages also follow Nest embedded style

  • Support logfile
  • Able to use logger with Dependency Injection
  • Compatible with existing logging module
  • Global request flow

Start logger

To use this logger, you need to import LoggerModule to app.module.ts via .forRoot() method.

import { LoggerModule } from '@hoplin/nestjs-logger';

@Module({
  imports: [
    LoggerModule.forRoot({
      applicationName: 'Logger Test',
      logfileDirectory: `${__dirname}/../`,
      saveAsFile: true,
      levelNTimestamp: {
        logLevels: ['log'],
        timestamp: true,
      },
    }),
  ],

...

forRoot() require some options. You can give two types of options, and each options refer to description under below

  • applicationName - string
    • Name of application to be printed in the log message
  • saveAsFile (optional) - boolean
    • true if you want to save log as logfile. If it's true, option logfileDirectory is not optional
  • logfileDirectory (optional) - string
    • Directory where logfile will be saved.
  • levelNTimestamp
    • logLevels(optional) - LogLevel[]
      • log level array. This work as same as NestJS Logger
    • timestamp(optional) - boolean
      • Check if logger print timestamp. timestamp mean, between current and previous log message.

For log level please refer json underbelow

{
  verbose: 0,
  debug: 1,
  log: 2,
  warn: 3,
  error: 4,
};

after initialize logger with .forRoot(), You can import LoggerModule from other Moduels throgh forFeature() and make DI to Injectable object

import { LoggerModule } from '@hoplin/nestjs-logger';

// example.module.ts
@Module({
  imports: [LoggerModule.forFeature()]

...
import { Logger } from '@hoplin/nestjs-logger';

// example.service.ts
@Injectable()
export class ExampleService {
  constructor(private readonly logger: Logger) {}

Global request / response flow interceptor

If you want to log to console about every request, use FlowInterceptor. You can both register globally or you can use it with @UseInterceptor() decorator, which NestJS provide

// main.ts
import { FlowInterceptor } from '@hoplin/nestjs-logger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalInterceptors(new FlowInterceptor());
  ...

Example Log Message

  • Enable Flow Interceptor globally

img

nestjs-custom-extendedlogger's People

Contributors

j-hoplin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.