Skip to content

Commit

Permalink
fix: handle the case where the first argument is an object (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang authored Aug 8, 2022
1 parent 35815a2 commit 09832f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ class Logger {
}

_writeLogOutput(level, consoleArgs) {
const fields = consoleArgs[0];
if (typeof fields === 'object') {
if (fields && consoleArgs.length === 1 && fields.err && fields.err instanceof Error) {
consoleArgs = [fields.err.message];
} else {
consoleArgs.shift();
}
}

if (this._debug) {
const str = new Date().toISOString().substring(11, 23) + ' ';

Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ describe('hexo-log', () => {
consoleWarnSpy.calledOnce.should.be.false;
consoleErrorSpy.calledTwice.should.be.false;
});

it('should not display error as object', () => {
const consoleWarnSpy = sinon.spy();
const consoleErrorSpy = sinon.spy();

loggerModule.__set__('console.warn', consoleWarnSpy);
loggerModule.__set__('console.error', consoleErrorSpy);

loggerModule.__with__(fakeProcess)(() => {
const log = loggerModule();
log.warn({err: new Error('test')});
log.error({err: new Error('test')}, 'test: %s', 'test');
});

consoleWarnSpy.args[0][0].should.eql('test');
consoleErrorSpy.args[0][0].should.eql('test: %s');
});
});

describe('hexo-log example', () => {
Expand Down

0 comments on commit 09832f1

Please sign in to comment.