ui
This commit is contained in:
+72
@@ -0,0 +1,72 @@
|
||||
import util from 'util'
|
||||
import { parseStack } from '../utils/error'
|
||||
import { writeStream } from '../utils/stream'
|
||||
import { formatDate } from '../utils/date'
|
||||
|
||||
const DEFAULTS = {
|
||||
dateFormat: 'HH:mm:ss',
|
||||
formatOptions: {
|
||||
date: true,
|
||||
colors: false,
|
||||
compact: true
|
||||
}
|
||||
}
|
||||
|
||||
const bracket = x => x ? `[${x}]` : ''
|
||||
|
||||
export default class BasicReporter {
|
||||
constructor (options) {
|
||||
this.options = Object.assign({}, DEFAULTS, options)
|
||||
}
|
||||
|
||||
formatStack (stack) {
|
||||
return ' ' + parseStack(stack).join('\n ')
|
||||
}
|
||||
|
||||
formatArgs (args) {
|
||||
const _args = args.map(arg => {
|
||||
if (arg && typeof arg.stack === 'string') {
|
||||
return arg.message + '\n' + this.formatStack(arg.stack)
|
||||
}
|
||||
return arg
|
||||
})
|
||||
|
||||
// Only supported with Node >= 10
|
||||
// https://nodejs.org/api/util.html#util_util_inspect_object_options
|
||||
if (typeof util.formatWithOptions === 'function') {
|
||||
return util.formatWithOptions(this.options.formatOptions, ..._args)
|
||||
} else {
|
||||
return util.format(..._args)
|
||||
}
|
||||
}
|
||||
|
||||
formatDate (date) {
|
||||
return this.options.formatOptions.date ? formatDate(this.options.dateFormat, date) : ''
|
||||
}
|
||||
|
||||
filterAndJoin (arr) {
|
||||
return arr.filter(x => x).join(' ')
|
||||
}
|
||||
|
||||
formatLogObj (logObj) {
|
||||
const message = this.formatArgs(logObj.args)
|
||||
|
||||
return this.filterAndJoin([
|
||||
bracket(logObj.type),
|
||||
bracket(logObj.tag),
|
||||
message
|
||||
])
|
||||
}
|
||||
|
||||
log (logObj, { async, stdout, stderr } = {}) {
|
||||
const line = this.formatLogObj(logObj, {
|
||||
width: stdout.columns || 0
|
||||
})
|
||||
|
||||
return writeStream(
|
||||
line + '\n',
|
||||
logObj.level < 2 ? stderr : stdout,
|
||||
async ? 'async' : 'default'
|
||||
)
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
export default class BrowserReporter {
|
||||
constructor (options) {
|
||||
this.options = Object.assign({}, options)
|
||||
|
||||
this.defaultColor = '#7f8c8d' // Gray
|
||||
this.levelColorMap = {
|
||||
0: '#c0392b', // Red
|
||||
1: '#f39c12', // Yellow
|
||||
3: '#00BCD4' // Cyan
|
||||
}
|
||||
this.typeColorMap = {
|
||||
success: '#2ecc71' // Green
|
||||
}
|
||||
}
|
||||
|
||||
log (logObj) {
|
||||
const consoleLogFn = logObj.level < 1
|
||||
// eslint-disable-next-line no-console
|
||||
? (console.__error || console.error)
|
||||
// eslint-disable-next-line no-console
|
||||
: logObj.level === 1 && console.warn ? (console.__warn || console.warn) : (console.__log || console.log)
|
||||
|
||||
// Type
|
||||
const type = logObj.type !== 'log' ? logObj.type : ''
|
||||
|
||||
// Tag
|
||||
const tag = logObj.tag ? logObj.tag : ''
|
||||
|
||||
// Styles
|
||||
const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor
|
||||
const style = `
|
||||
background: ${color};
|
||||
border-radius: 0.5em;
|
||||
color: white;
|
||||
font-weight: bold;
|
||||
padding: 2px 0.5em;
|
||||
`
|
||||
|
||||
const badge = `%c${[tag, type].filter(Boolean).join(':')}`
|
||||
|
||||
// Log to the console
|
||||
if (typeof logObj.args[0] === 'string') {
|
||||
consoleLogFn(
|
||||
`${badge}%c ${logObj.args[0]}`,
|
||||
style,
|
||||
// Empty string as style resets to default console style
|
||||
'',
|
||||
...logObj.args.slice(1)
|
||||
)
|
||||
} else {
|
||||
consoleLogFn(badge, style, ...logObj.args)
|
||||
}
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
import stringWidth from 'string-width'
|
||||
import figures from 'figures'
|
||||
import chalk from 'chalk'
|
||||
import BasicReporter from './basic'
|
||||
import { parseStack } from '../utils/error'
|
||||
import { chalkColor, chalkBgColor } from '../utils/chalk'
|
||||
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from '../utils/fancy'
|
||||
|
||||
const DEFAULTS = {
|
||||
secondaryColor: 'grey',
|
||||
formatOptions: {
|
||||
date: true,
|
||||
colors: true,
|
||||
compact: false
|
||||
}
|
||||
}
|
||||
|
||||
const TYPE_ICONS = {
|
||||
info: figures('ℹ'),
|
||||
success: figures('✔'),
|
||||
debug: figures('›'),
|
||||
trace: figures('›'),
|
||||
log: ''
|
||||
}
|
||||
|
||||
export default class FancyReporter extends BasicReporter {
|
||||
constructor (options) {
|
||||
super(Object.assign({}, DEFAULTS, options))
|
||||
}
|
||||
|
||||
formatStack (stack) {
|
||||
const grey = chalkColor('grey')
|
||||
const cyan = chalkColor('cyan')
|
||||
|
||||
return '\n' + parseStack(stack)
|
||||
.map(line => ' ' + line
|
||||
.replace(/^at +/, m => grey(m))
|
||||
.replace(/\((.+)\)/, (_, m) => `(${cyan(m)})`)
|
||||
)
|
||||
.join('\n')
|
||||
}
|
||||
|
||||
formatType (logObj, isBadge) {
|
||||
const typeColor = TYPE_COLOR_MAP[logObj.type] ||
|
||||
LEVEL_COLOR_MAP[logObj.level] ||
|
||||
this.options.secondaryColor
|
||||
|
||||
if (isBadge) {
|
||||
return chalkBgColor(typeColor).black(` ${logObj.type.toUpperCase()} `)
|
||||
}
|
||||
|
||||
const _type = typeof TYPE_ICONS[logObj.type] === 'string' ? TYPE_ICONS[logObj.type] : (logObj.icon || logObj.type)
|
||||
return _type ? chalkColor(typeColor)(_type) : ''
|
||||
}
|
||||
|
||||
formatLogObj (logObj, { width }) {
|
||||
const [message, ...additional] = this.formatArgs(logObj.args).split('\n')
|
||||
|
||||
const isBadge = typeof logObj.badge !== 'undefined' ? Boolean(logObj.badge) : logObj.level < 2
|
||||
|
||||
const secondaryColor = chalkColor(this.options.secondaryColor)
|
||||
|
||||
const date = this.formatDate(logObj.date)
|
||||
const coloredDate = date && secondaryColor(date)
|
||||
|
||||
const type = this.formatType(logObj, isBadge)
|
||||
|
||||
const tag = logObj.tag ? secondaryColor(logObj.tag) : ''
|
||||
|
||||
const formattedMessage = message.replace(/`([^`]+)`/g, (_, m) => chalk.cyan(m))
|
||||
|
||||
let line
|
||||
const left = this.filterAndJoin([type, formattedMessage])
|
||||
const right = this.filterAndJoin([tag, coloredDate])
|
||||
const space = width - stringWidth(left) - stringWidth(right) - 2
|
||||
|
||||
if (space > 0 && width >= 80) {
|
||||
line = left + ' '.repeat(space) + right
|
||||
} else {
|
||||
line = left
|
||||
}
|
||||
|
||||
line += additional.length ? '\n' + additional.join('\n') : ''
|
||||
|
||||
return isBadge ? '\n' + line + '\n' : line
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
export { default as BasicReporter } from './basic'
|
||||
export { default as BrowserReporter } from './browser'
|
||||
export { default as FancyReporter } from './fancy'
|
||||
export { default as JSONReporter } from './json'
|
||||
export { default as WinstonReporter } from './winston'
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
export default class JSONReporter {
|
||||
constructor ({ stream } = {}) {
|
||||
this.stream = stream || process.stdout
|
||||
}
|
||||
|
||||
log (logObj) {
|
||||
this.stream.write(JSON.stringify(logObj) + '\n')
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
// This reporter is compatible with Winston 3
|
||||
// https://github.com/winstonjs/winston
|
||||
|
||||
// eslint-disable-next-line
|
||||
const _require = typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : require // bypass webpack
|
||||
|
||||
export default class WinstonReporter {
|
||||
constructor (logger) {
|
||||
if (logger && logger.log) {
|
||||
this.logger = logger
|
||||
} else {
|
||||
const winston = _require('winston')
|
||||
|
||||
this.logger = winston.createLogger(Object.assign({
|
||||
level: 'info',
|
||||
format: winston.format.simple(),
|
||||
transports: [
|
||||
new winston.transports.Console()
|
||||
]
|
||||
}, logger))
|
||||
}
|
||||
}
|
||||
|
||||
log (logObj) {
|
||||
const args = [].concat(logObj.args)
|
||||
const arg0 = args.shift()
|
||||
|
||||
this.logger.log({
|
||||
level: levels[logObj.level] || 'info',
|
||||
label: logObj.tag,
|
||||
message: arg0,
|
||||
args: args,
|
||||
timestamp: logObj.date.getTime() / 1000
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const levels = {
|
||||
0: 'error',
|
||||
1: 'warn',
|
||||
2: 'info',
|
||||
3: 'verbose',
|
||||
4: 'debug',
|
||||
5: 'silly'
|
||||
}
|
||||
Reference in New Issue
Block a user