@djeka07/nestjs-loki-logger
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

NestJS Loki Logger

Introduction

A logger that logs to Grafana Loki.

Installation

yarn add @djeka07/nestjs-loki-logger

Usage

Importing module

import { LokiLoggerModule } from '@djeka07/nestjs-loki-logger';
@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 '@djeka07/nestjs-loki-logger';
@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 '@djeka07/nestjs-loki-logger';


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 '@djeka07/nestjs-loki-logger';
@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 '@djeka07/nestjs-loki-logger';

@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.

Readme

Keywords

Package Sidebar

Install

npm i @djeka07/nestjs-loki-logger

Weekly Downloads

109

Version

1.1.1

License

MIT

Unpacked Size

158 kB

Total Files

53

Last publish

Collaborators

  • djeka07