Skip to content

Commit

Permalink
fix: stack missing in context logger (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Feb 6, 2021
1 parent e329333 commit 7b27145
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/logger/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ export const displayCommonMessage = format(
);
}

// error(new Error(''), {label: 1}) 的情况
if (info.message['stack'] && info.message['message']) {
const err = new Error(info.message['message']);
err.name = info.message['name'];
err.stack = info.message['stack'];
info.originError = err;
info.stack = info.message['stack'];
info.message = err.stack;
info[MESSAGE] = info[MESSAGE] || info.message;
}

// 处理数组,Map,Set 的 message
if (
Array.isArray(info.message) ||
Expand Down
3 changes: 3 additions & 0 deletions packages/logger/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,13 @@ describe('/test/index.test.ts', () => {
await removeFileOrDir(join(process.cwd(), 'common-error.log'));
const consoleLogger = createConsoleLogger('consoleLogger');
consoleLogger.error('test console error');
console.log('---');
const err = new Error('custom error');
err.name = 'MyCustomError';
consoleLogger.error(err);
consoleLogger.error(err, { label: 123});
consoleLogger.error('before:', err);
console.log('---');
consoleLogger.info('启动耗时 %d ms', 111);
consoleLogger.info('%j', {a: 1});
consoleLogger.debug('1', '2', '3');
Expand Down

0 comments on commit 7b27145

Please sign in to comment.