@f1stnpm3/totam-temporibus-eaque

1.0.0 • Public • Published

NestJS Loki Logger

Introduction

A logger that logs to Grafana Loki.

Installation

yarn add @f1stnpm3/totam-temporibus-eaque

Usage

Importing module

import { LokiLoggerModule } from '@f1stnpm3/totam-temporibus-eaque';
@Module({
  imports: [
    LokiLoggerModule.forRoot({
      app: 'app-name',
      host: 'host',
      userId: 'user id',
      password: 'password',
      environment: 'development' | 'production', // Optional, defaults to production
      logDev: false, // Optional, default to false
      minLogLevel: LogLevel.verbose, // Optional, defaults to LogLevel.verbose
    }),
  ],
  providers: [],
  exports: [],
})
export class AModule {}

Importing module Async

import { LokiLoggerModule } from '@f1stnpm3/totam-temporibus-eaque';
@Module({
  imports: [
    LokiLoggerModule.forRootAsync({
      useFactory: async () => {
        return {
          app: 'app-name',
          host: 'host',
          userId: 'user id',
          password: 'password',
          environment: 'development' | 'production', // Optional, defaults to production
          logDev: false, // Optional, default to false
          minLogLevel: LogLevel.verbose, // Optional, defaults to LogLevel.verbose
        };
      },
    }),
  ],
  providers: [],
  exports: [],
})
export class AModule {}

Use logger for nest logging

import { NestFactory } from '@nestjs/core';
import { MainModule } from './main.module';
import { LokiLoggerService } from '@f1stnpm3/totam-temporibus-eaque';


async function bootstrap() {
  const app = await NestFactory.create(MainModule, {
    bufferLogs: true,
  });
  app.useLogger(app.get(LokiLoggerService));
  await app.listen(3000, '0.0.0.0');
}
bootstrap();

Use request logging interceptor

import { LokiLoggerModule, LokiRequestLoggingInterceptor } from '@f1stnpm3/totam-temporibus-eaque';
@Module({
  imports: [
    LokiLoggerModule.forRootAsync({
      useFactory: async () => {
        return {
          app: 'app-name',
          host: 'host',
          userId: 'user id',
          password: 'password',
          environment: 'development' | 'production', // Optional, defaults to production
          logDev: false, // Optional, default to false
          minLogLevel: LogLevel.verbose, // Optional, defaults to LogLevel.verbose
        };
      },
    }),
  ],
  providers: [LokiRequestLoggerInterceptorProvider],
  exports: [],
})
export class AModule {}

Use the log service

import { LokiLoggerService } from '@f1stnpm3/totam-temporibus-eaque';

@Injectable()
export class AService {
  constructor(private readonly loggerService: LokiLoggerService) {
    this.loggerService.verbose('message', [{ optionalProps: 'optionalProps' }])
  }
}

Author

André Ekbom Github

License

Licensed under the MIT License - see the LICENSE file for details.

Package Sidebar

Install

npm i @f1stnpm3/totam-temporibus-eaque

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

11.4 kB

Total Files

8

Last publish

Collaborators

  • sinhatminh11