Skip to content

Commit

Permalink
fix: support Error as logObject
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 5, 2018
1 parent d48fd0f commit 134ff54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/consola.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import defaultTypes from './types'
import { assignToLogObj } from './utils'

export default class Consola {
constructor (options = {}) {
Expand Down Expand Up @@ -67,15 +68,13 @@ export default class Consola {
logObj.message = [arg1, arg2].concat(args).join(' ')
} else {
// [str] [obj?]
if (typeof arg2 === 'object') {
Object.assign(logObj, arg2)
}
assignToLogObj(logObj, arg2)
logObj.message = arg1
}
} else {
// [obj]
if (typeof arg1 === 'object') {
Object.assign(logObj, arg1)
assignToLogObj(logObj, arg1)
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const VALID_LOGOBJ_KEYS = [
'message', 'date', 'scope',
'clear', 'badge', 'additional', 'stack', 'additionalStyle', 'icon'
]

export function assignToLogObj (logObj, obj) {
for (const key of VALID_LOGOBJ_KEYS) {
const val = obj[key]
if (typeof val !== 'undefined') {
logObj[key] = val
}
}
}

0 comments on commit 134ff54

Please sign in to comment.