Skip to content

Commit

Permalink
fix(service): update printError to include context by default
Browse files Browse the repository at this point in the history
Currently, the context of an error printed with `printError` does not get logged, so it looks like
the error could be from anywhere.
  • Loading branch information
jmcdo29 committed Feb 19, 2020
1 parent cab7495 commit 31b7819
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
27 changes: 16 additions & 11 deletions src/ogma.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Test } from '@nestjs/testing';
import { LogLevel, Ogma, OgmaOptions } from 'ogma';
import { Ogma, OgmaOptions } from 'ogma';
import { OGMA_CONTEXT, OGMA_INSTANCE } from './ogma.constants';
import { OgmaService } from './ogma.service';

Expand All @@ -16,20 +16,20 @@ describe('OgmaService', () => {
'should work with context %s',
(context: string | null) => {
describe.each([
{ logLevel: 'ALL' as keyof typeof LogLevel, color: true },
{ logLevel: 'ALL' as const, color: true },
{ color: false, stream: mockStream },
])('should work with options %o', (options: Partial<OgmaOptions>) => {
describe.each(['Custom_Context', undefined])(
'calling with custom context %s',
(customContext: string | undefined) => {
describe.each([
'info' as keyof OgmaService,
'error' as keyof OgmaService,
'warn' as keyof OgmaService,
'debug' as keyof OgmaService,
'verbose' as keyof OgmaService,
'silly' as keyof OgmaService,
'fatal' as keyof OgmaService,
'info' as const,
'error' as const,
'warn' as const,
'debug' as const,
'verbose' as const,
'silly' as const,
'fatal' as const,
])('should call %s method', (level: keyof OgmaService) => {
let ogmaSpy: jest.SpyInstance;
beforeEach(async () => {
Expand All @@ -44,7 +44,7 @@ describe('OgmaService', () => {
...(ogma as any).options,
...options,
};
ogmaSpy = jest.spyOn(ogma, level as any);
ogmaSpy = jest.spyOn(ogma, level);
return ogma;
},
},
Expand All @@ -67,9 +67,14 @@ describe('OgmaService', () => {
},
);
it('should be able to call "callError"', () => {
const error = new Error('This is my error');
ogmaSpy = jest.spyOn(Ogma.prototype, 'printError');
service.printError(new Error('This is my error'));
service.printError(error, customContext);
expect(ogmaSpy).toBeCalledTimes(1);
expect(ogmaSpy).toBeCalledWith(
error,
customContext ?? context ?? '',
);
});
});
},
Expand Down
3 changes: 2 additions & 1 deletion src/ogma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export class OgmaService implements LoggerService {
*/
public printError(error: Error, context?: string): void {
this.printMessage('', 'error', context);
this.ogma.printError(error);
context = context ?? this.context;
this.ogma.printError(error, context);
}

private printMessage(
Expand Down

0 comments on commit 31b7819

Please sign in to comment.