Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved trace method #336

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/lib/logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ describe('NGXLogger', () => {
[NGXLogger],
(logger: NGXLogger) => {
const logSpy = spyOn(<any>logger, '_log');

logger.trace('message');

expect(logSpy).toHaveBeenCalledWith(NgxLoggerLevel.TRACE, 'message', []);
const messageString = 'message';

logger.trace(messageString);
const _testStackError = new Error();
_testStackError.name = messageString;

expect(logSpy).toHaveBeenCalled();
expect(logSpy.calls.argsFor(0)[0]).toBe(NgxLoggerLevel.TRACE);
// We can't test the exact stack trace so we only make sure the message is included
expect(logSpy.calls.argsFor(0)[1]).toContain(messageString);
expect(logSpy.calls.argsFor(0)[2]).toEqual([]);
}
));
});
Expand Down
7 changes: 6 additions & 1 deletion src/lib/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export class NGXLogger {
}

public trace(message?: any | (() => any), ...additional: any[]): void {
this._log(NgxLoggerLevel.TRACE, message, additional);
// We manually set the stack trace using a dummy Error.
// This is done instead of using console.trace in _log method to ensure the stack trace is kept at the highest level possible.
// If the dummy error has no stack, we default to printing only the message and additional parameters.
const _traceDummyError = new Error();
_traceDummyError.name = message;
this._log(NgxLoggerLevel.TRACE, _traceDummyError.stack || message, additional);
}

public debug(message?: any | (() => any), ...additional: any[]): void {
Expand Down