Skip to content

Commit

Permalink
fix(service): fixes logger methods to follow LoggerService interface
Browse files Browse the repository at this point in the history
`OgmaService` methods now adhere to NestJS\'s `LoggerService`
interface, but still _can_ accept an object as the second parameter.
This will allow `app.setLogger(OgmaService)` to work properly, but
still allow for extra metadata to be logged in services.
  • Loading branch information
jmcdo29 committed Oct 25, 2020
1 parent 54ca14e commit f1fd191
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .codebeatignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
benchmarks/*
tools/*
40 changes: 31 additions & 9 deletions packages/nestjs-module/src/ogma.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable, Optional } from '@nestjs/common';
import { Injectable, LoggerService, Optional } from '@nestjs/common';
import { Ogma } from '@ogma/logger';
import { InjectOgma, InjectOgmaContext } from './decorators';
import { OgmaServiceMeta } from './interfaces';
import { RequestContext } from './interfaces/request-context.interface';

@Injectable()
export class OgmaService {
export class OgmaService implements LoggerService {
private readonly context?: string;
private readonly ogma: Ogma;

Expand Down Expand Up @@ -45,7 +45,11 @@ export class OgmaService {
* @param context Optional context if you want to change what the original context was
* @param requestId Optional id of an request
*/
public info(message: any, meta?: OgmaServiceMeta): void {

public info(message: any, meta?: OgmaServiceMeta | string): void {
if (typeof meta === 'string') {
meta = { context: meta };
}
this.printMessage(message, 'info', meta);
}

Expand All @@ -54,7 +58,10 @@ export class OgmaService {
* @param message What to print to the Ogma instance
* @param context Optional context if you want to change what the original context was
*/
public error(message: any, meta?: OgmaServiceMeta): void {
public error(message: any, meta?: OgmaServiceMeta | string): void {
if (typeof meta === 'string') {
meta = { context: meta };
}
this.printMessage(message, 'error', meta);
}

Expand All @@ -64,7 +71,10 @@ export class OgmaService {
* @param context Optional context if you want to change what the original context was
* @param requestId Optional id of an request
*/
public warn(message: any, meta?: OgmaServiceMeta): void {
public warn(message: any, meta?: OgmaServiceMeta | string): void {
if (typeof meta === 'string') {
meta = { context: meta };
}
this.printMessage(message, 'warn', meta);
}

Expand All @@ -74,7 +84,10 @@ export class OgmaService {
* @param context Optional context if you want to change what the original context was
* @param requestId Optional id of an request
*/
public debug(message: any, meta?: OgmaServiceMeta): void {
public debug(message: any, meta?: OgmaServiceMeta | string): void {
if (typeof meta === 'string') {
meta = { context: meta };
}
this.printMessage(message, 'debug', meta);
}

Expand All @@ -84,7 +97,10 @@ export class OgmaService {
* @param context Optional context if you want to change what the original context was
* @param requestId Optional id of an request
*/
public fatal(message: any, meta?: OgmaServiceMeta): void {
public fatal(message: any, meta?: OgmaServiceMeta | string): void {
if (typeof meta === 'string') {
meta = { context: meta };
}
this.printMessage(message, 'fatal', meta);
}

Expand All @@ -94,7 +110,10 @@ export class OgmaService {
* @param context Optional context if you want to change what the original context was
* @param requestId Optional id of an request
*/
public silly(message: any, meta?: OgmaServiceMeta): void {
public silly(message: any, meta?: OgmaServiceMeta | string): void {
if (typeof meta === 'string') {
meta = { context: meta };
}
this.printMessage(message, 'silly', meta);
}

Expand All @@ -104,7 +123,10 @@ export class OgmaService {
* @param context Optional context if you want to change what the original context was
* @param requestId Optional id of an request
*/
public verbose(message: any, meta?: OgmaServiceMeta): void {
public verbose(message: any, meta?: OgmaServiceMeta | string): void {
if (typeof meta === 'string') {
meta = { context: meta };
}
this.printMessage(message, 'verbose', meta);
}

Expand Down

0 comments on commit f1fd191

Please sign in to comment.