@djeka07/nestjs-loki-logger
v1.1.1
Published
A logger that logs to Grafana Loki.
Downloads
367
Readme
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.