Skip to content

Commit

Permalink
feat: write error and warns to process.stderr by default
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 9, 2018
1 parent ed294e4 commit 6565254
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
22 changes: 14 additions & 8 deletions src/reporters/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ export default class BasicReporter {
constructor (options) {
this.options = Object.assign({
stream: process.stdout,
errStream: process.stderr,
alignment: 'left',
showType: false
}, options)
}

write (data) {
this.options.stream.write(data)
write (data, isError = false) {
if (isError) {
this.options.errStream.write(data)
} else {
this.options.stream.write(data)
}
}

formatStack (stack) {
Expand Down Expand Up @@ -70,31 +75,32 @@ export default class BasicReporter {

log (logObj) {
const fields = this.getFields(logObj)
const { isError } = logObj

// Print date
this.write((`[${align(this.options.alignment, fields.date, 8)}] `))
this.write((`[${align(this.options.alignment, fields.date, 8)}] `), isError)

// Print type
if (fields.type.length) {
this.write((`[${align(this.options.alignment, fields.type.toUpperCase(), 7)}] `))
this.write((`[${align(this.options.alignment, fields.type.toUpperCase(), 7)}] `), isError)
}

// Print tag
if (fields.tag.length) {
this.write(`[${fields.tag}] `)
this.write(`[${fields.tag}] `, isError)
}

// Print message
if (fields.message.length) {
this.write(fields.message)
this.write(fields.message, isError)
}

// Print additional args
if (fields.args.length) {
this.write('\n' + (fields.args.join(' ')))
this.write('\n' + (fields.args.join(' ')), isError)
}

// Newline
this.write('\n')
this.write('\n', isError)
}
}
23 changes: 12 additions & 11 deletions src/reporters/fancy.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,53 @@ export default class FancyReporter extends BasicReporter {
return ' ' + parseStack(stack).join('↲\n ')
}

clear () {
this.options.stream.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H')
clear (isError) {
this.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H', isError)
}

log (logObj) {
const fields = this.getFields(logObj)
const { isError } = logObj

// Clear console
if (logObj.clear) {
this.clear()
this.clear(isError)
}

// Print type
const type = align(this.options.alignment, fields.type.toUpperCase(), 7)
if (logObj.badge) {
this.write('\n' + chalkBgColor(logObj.color).black(` ${type} `) + ' ')
this.write('\n' + chalkBgColor(logObj.color).black(` ${type} `) + ' ', isError)
} else if (fields.type !== 'log') {
const icon = logObj.icon || ICONS[fields.type] || ICONS.default
if (this.showType) {
this.write(chalkColor(logObj.color)(`${icon} ${type} `))
this.write(chalkColor(logObj.color)(`${icon} ${type} `), isError)
} else {
this.write(chalkColor(logObj.color)(`${icon} `))
this.write(chalkColor(logObj.color)(`${icon} `), isError)
}
}

// Print tag
if (fields.tag.length) {
this.write((fields.tag.replace(/:/g, '>') + '>').split('>').join(NS_SEPARATOR))
this.write((fields.tag.replace(/:/g, '>') + '>').split('>').join(NS_SEPARATOR), isError)
}

// Print message
if (fields.message.length) {
this.write(chalkColor(logObj.color)(fields.message))
this.write(chalkColor(logObj.color)(fields.message), isError)
}

// Badge additional line
if (logObj.badge) {
this.write('\n')
this.write('\n', isError)
}

// Print additional args
if (fields.args.length) {
this.write('\n' + chalkColor(logObj.additionalColor || 'grey')(fields.args.join(' ')))
this.write('\n' + chalkColor(logObj.additionalColor || 'grey')(fields.args.join(' ')), isError)
}

// Newline
this.write(logObj.badge ? '\n\n' : '\n')
this.write(logObj.badge ? '\n\n' : '\n', isError)
}
}
9 changes: 6 additions & 3 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
export default {
fatal: {
level: 0,
color: 'red'
color: 'red',
isError: true
},
error: {
level: 0,
color: 'red'
color: 'red',
isError: true
},
warn: {
level: 1,
color: 'yellow'
color: 'yellow',
isError: true
},
log: {
level: 2,
Expand Down

6 comments on commit 6565254

@pi0
Copy link
Member Author

@pi0 pi0 commented on 6565254 Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pimlie This was what you wanted?

@pi0
Copy link
Member Author

@pi0 pi0 commented on 6565254 Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: 3884e42

@pimlie
Copy link
Contributor

@pimlie pimlie commented on 6565254 Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly what I meant indeed. I am not sure how configurable you want everything to be, but we could add a separate mapping between streams and types (with a default config like you have now). Separation between info and errors is the most common use of this, but in the end it would be great if you could have every log level write to its own stream.

This is pseudo code, but then you could do something like this:

foreach (type in consola.types) {
  consola.setStreamForType(
    type,
    fs.createWriteStream('nuxt' + (type === 'log' ? '' : '.'+type) + '.log', { flags: 'w' })
  )
}

and have log files for every separate level:

/var/log/nuxt/nuxt.warn.log
/var/log/nuxt/nuxt.error.log
/var/log/nuxt/nuxt.info.log
/var/log/nuxt/nuxt.log

@pimlie
Copy link
Contributor

@pimlie pimlie commented on 6565254 Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, if you wish I can have a look at that tomorrow and make a real proposal. Also for the sprint thingie :)

@pi0
Copy link
Member Author

@pi0 pi0 commented on 6565254 Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pimlie For your idea (Which is really great) we would add a FileReporter which extends BasicReporter and then separate them like this. Also yes PR is the best idea ;)

@pi0
Copy link
Member Author

@pi0 pi0 commented on 6565254 Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also support not only per type but per tag splitting too. So we can have nuxt.log and default.log separated.

Please sign in to comment.