Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Improve error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Oct 10, 2017
1 parent eb41ce0 commit dd94a10
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
55 changes: 41 additions & 14 deletions bin/cli/log.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,60 @@
'use strict'

const indentString = require('indent-string')
const PrettyError = require('pretty-error')
const getTimestamp = require('time-stamp')
const logSymbols = require('log-symbols')
const cleanStack = require('clean-stack')

const logUpdate = require('log-update')
const chalk = require('chalk')
const ora = require('ora')

const createSpinner = () => ora({ color: 'gray' })
const getCurrentTimestamp = () => getTimestamp('HH:mm:ss')
const createSpinner = () => ora({ color: 'gray' })
const pe = new PrettyError()

pe.appendStyle({
// this is a simple selector to the element that says 'Error'
'pretty-error > header > title > kind': {
background: 'none',
marginRight: 1
},

'pretty-error > header > colon': {
display: 'none'
},

'pretty-error > header > message': {
color: 'grey'
},

'pretty-error > trace > item > header > pointer > file': {
color: 'white'
},

'pretty-error > trace > item > header > pointer > colon': {
color: 'white'
},

'pretty-error > trace > item > header > pointer > line': {
color: 'white'
},

'pretty-error > trace > item > header > what': {
color: 'grey'
}
})

const OFFSET = ' '

module.exports = {
error (err) {
const symbol = chalk.blue(logSymbols.error)
const timestamp = chalk.gray(getCurrentTimestamp())

const [message, ...stackTraces] = chalk
.gray(
cleanStack(err.stack)
.replace('Error: ', chalk.red('error '))
.replace('Cannot', 'cannot')
)
.split('\n')

const stackTrace = stackTraces.map(msg => `${OFFSET}${msg}`).join('')
const logMessage = `${OFFSET} ${symbol} ${timestamp} ${message}`
console.log(`${logMessage}\n${stackTrace}\n`)
const stack = cleanStack(err.stack)
const cleanErr = Object.assign({}, err, { stack })
const prettyErr = pe.render(cleanErr).replace('Error', symbol)
console.log(indentString(prettyErr, OFFSET.length))
},
restart ({ filename, forcing }) {
const symbol = chalk.blue(logSymbols.info)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"log-update": "~2.1.0",
"meow": "~3.7.0",
"ora": "~1.3.0",
"pretty-error": "~2.1.1",
"time-stamp": "~2.0.0",
"update-notifier": "~2.2.0"
},
Expand Down

0 comments on commit dd94a10

Please sign in to comment.