diff --git a/lib/log.js b/lib/log.js index f0d3c97..fd11c36 100644 --- a/lib/log.js +++ b/lib/log.js @@ -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) + ' '; diff --git a/test/index.js b/test/index.js index 64eae0f..e682a10 100644 --- a/test/index.js +++ b/test/index.js @@ -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', () => {