All Projects → nest-x → nestx-log4js

nest-x / nestx-log4js

Licence: MIT License
nestjs log4js-node module

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to nestx-log4js

necord
🤖 A module for creating Discord bots using NestJS, based on Discord.js
Stars: ✭ 77 (+266.67%)
Mutual labels:  nestjs
Nestjs-Typeorm-Auth
NestJS + Typeorm codebase containing a full authentification system with roles, sessions and email verification.
Stars: ✭ 37 (+76.19%)
Mutual labels:  nestjs
docker-nestjs-starter
NestJs app with Docker
Stars: ✭ 65 (+209.52%)
Mutual labels:  nestjs
nest-todo
🐱 使用 React.js + Nest.js 实现一个简单的 Todo App。
Stars: ✭ 205 (+876.19%)
Mutual labels:  nestjs
web07-boostCam
🎥캠 기능이 들어간 웹 메신저 서비스🎥
Stars: ✭ 16 (-23.81%)
Mutual labels:  nestjs
axios
Axios module for Nest framework (node.js) 🗂
Stars: ✭ 95 (+352.38%)
Mutual labels:  nestjs
Firstsight
前后端分离,服务端渲染的个人博客,基于 Nodejs、 Vue、 Nuxt、Nestjs、PostgreSQL、Apollo
Stars: ✭ 19 (-9.52%)
Mutual labels:  nestjs
nestjs-mailer
🌈 A simple implementation example with and without email-templates using mailer module for nest js built on top of nodemailer.
Stars: ✭ 82 (+290.48%)
Mutual labels:  nestjs
prisma-generator-nestjs-dto
Generates NestJS DTO classes from Prisma Schema
Stars: ✭ 124 (+490.48%)
Mutual labels:  nestjs
teanjs
🔥 TypeORM - Express - Angular 8 - NestJS Server Side Rendering (SSR) 😺
Stars: ✭ 62 (+195.24%)
Mutual labels:  nestjs
kanban-project-management
Web Application to manage software development projects.
Stars: ✭ 39 (+85.71%)
Mutual labels:  nestjs
bad-cards-game
Bad Cards Game
Stars: ✭ 23 (+9.52%)
Mutual labels:  nestjs
nestjs-file-streaming
NestJS File Streaming With MongoDB
Stars: ✭ 28 (+33.33%)
Mutual labels:  nestjs
postgres-nest-react-typescript-boilerplate
No description or website provided.
Stars: ✭ 93 (+342.86%)
Mutual labels:  nestjs
nestjs-course
NestJs In Practice Course (with MongoDB)
Stars: ✭ 70 (+233.33%)
Mutual labels:  nestjs
nestjs-asyncapi
Async API module for Nestjs that provides documentation generation using your existing code (similar to Nestjs swagger module)
Stars: ✭ 22 (+4.76%)
Mutual labels:  nestjs
shellops-api
Shellops API ( NestJS ). Assists in managing Docker on your server.
Stars: ✭ 17 (-19.05%)
Mutual labels:  nestjs
awesome-nestjs
A curated list of awesome things related to NestJS 😎
Stars: ✭ 5,771 (+27380.95%)
Mutual labels:  nestjs
server-next
😎 The next generation of RESTful API service and more for Mix Space, powered by @nestjs.
Stars: ✭ 43 (+104.76%)
Mutual labels:  nestjs
nestjs-toolbox
The repository contains a suite of components and modules for Nest.js
Stars: ✭ 166 (+690.48%)
Mutual labels:  nestjs

nestx-log4js

Github Workflow Status Codecov Semantic-Release

Provide log4js module as NestJS module

This is root of @nestx-log4js monorepo.

To Get Started, please read @nestx-log4js/core Documentation

Core Module Documentation

log4js as NestJS Module.


Features

  • Provide log4js wrapper as NestJS Global Module
  • Provide Log4jsLogger instance for replacement logger usage

Installation

yarn add @nestx-log4js/core

Usage

Just want to use log4js?

Since logger is a special service in NestJS, we suppose import Log4jsModule and manual call app.useLogger(app.get(Log4jsLogger))

app.module.ts

import { Module } from '@nestjs/common';
import { Log4jsModule } from '@nestx-log4js/core';


@Module({
  imports: [
    Log4jsModule.forRoot()
  ]
})
export class AppModule {}

bootstrap.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; 
import { Log4jsLogger } from '@nestx-log4js/core';

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

For more details, you can refer unit tests in source code

Initial Log4jsModule with AsyncOptions (Production Usage)

You might want to use different appender (e.g. file/dateFile appender) in real production usage and initial in your ConfigService/LoggerOnlyConfigService

app.module.ts

import { Module } from '@nestjs/common';
import { Log4jsModule } from '@nestx-log4js/core';

// import your ConfigService/LoggerConfigService

@Module({
  imports: [
    Log4js.forRootAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => config.getLog4jsOptions() // config.getLog4jsOptions should return valid Log4jsOptions
    })
  ]
})
export class AppModule {}

Bundled Layout & Appenders

When using Log4jsModule.forRoot() and no spec any appenders and layouts,

It will use default below layouts:

export const LOG4JS_DEFAULT_LAYOUT = {
  type: 'pattern',
  // log4js default pattern %d{yyyy-MM-dd HH:mm:ss:SSS} [%thread] %-5level %logger{36} - %msg%n
  // we use process id instead thread id
  pattern: '%[%d{yyyy-MM-dd hh:mm:ss:SSS} %p --- [%15.15x{name}]%] %40.40f{3}  : %m',
  tokens: {
    name: (logEvent) => {
      return (logEvent.context && logEvent.context['name']) || '-';
    }
  }
};

export const LOG4JS_NO_COLOUR_DEFAULT_LAYOUT = {
  type: 'pattern',
  // log4js default pattern %d{yyyy-MM-dd HH:mm:ss:SSS} [%thread] %-5level %logger{36} - %msg%n
  // we use process id instead thread id
  pattern: '%d{yyyy-MM-dd hh:mm:ss:SSS} %p --- [%15.15x{name}] %40.40f{3}  : %m',
  tokens: {
    name: (logEvent) => {
      return (logEvent.context && logEvent.context['name']) || '-';
    }
  }
};



export const LOG4JS_DEFAULT_CONFIG: Configuration = {
  appenders: {
    stdout: {
      type: 'stdout',
      layout: LOG4JS_DEFAULT_LAYOUT
    },
    file: {
      type: 'file',
      filename: './logs/application.log',
      maxLogSize: 20 * 1024 * 1024, // maxLogSize use bytes ad unit
      backups: 10,     // default use 5 so 1KB file size total rotating
      layout: LOG4JS_NO_COLOUR_DEFAULT_LAYOUT
    }
  },
  categories: {
    default: {
      enableCallStack: true,
      appenders: ['stdout', 'file'],
      level: 'debug'
    }
  }
};

It will use default below layouts (from spring boot default log pattern without process id)

You can refer to SpringBoot logging features


2020-11-14 15:47:24:486 INFO --- [         NestJS]               core/src/log4js.classes.ts  : log using nestjs as category
2020-11-14 15:47:24:486 INFO --- [              -]               core/src/log4js.classes.ts  : log using none as category
2020-11-14 15:47:24:490 INFO --- [         NestJS]               core/src/log4js.classes.ts  : log using nestjs as category
2020-11-14 15:47:24:490 WARN --- [              -]      src/__tests__/log4js.module.test.ts  : log using none as category

Tips: You are using grok pattern via filebeat/other sidecar log agent? You can use below grok pattern: %{TIMESTAMP_ISO8601:server_time}\s*%{LOGLEVEL:level}\s*---\s*[\s*%{NOTSPACE:context}]\s*%{NOTSPACE:file_path}\s*:\s*%{GREEDYDATA:content}

It will split to friendly format (you can update alias yourself via grok document)

  • server_time
  • level
  • context
  • file_path
  • content
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].