UNPKG

1.19 kBJavaScriptView Raw
1export default class BrowserReporter {
2 constructor (options) {
3 this.options = Object.assign({}, options)
4
5 this.defaultColor = '#7f8c8d' // Gray
6 this.levelColorMap = {
7 0: '#c0392b', // Red
8 1: '#f39c12', // Yellow
9 3: '#00BCD4' // Cyan
10 }
11 this.typeColorMap = {
12 success: '#2ecc71' // Green
13 }
14 }
15
16 log (logObj) {
17 const consoleLogFn = logObj.level < 1
18 // eslint-disable-next-line no-console
19 ? (console.__error || console.error)
20 // eslint-disable-next-line no-console
21 : logObj.level === 1 && console.warn ? (console.__warn || console.warn) : (console.__log || console.log)
22
23 // Type
24 const type = logObj.type !== 'log' ? logObj.type : ''
25
26 // Tag
27 const tag = logObj.tag ? logObj.tag : ''
28
29 // Styles
30
31 const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor
32 const style = `
33 background: ${color};
34 border-radius: 0.5em;
35 color: white;
36 font-weight: bold;
37 padding: 2px 0.5em;
38 `
39
40 // Log to the console
41 consoleLogFn(
42 '%c' + [tag, type].filter(Boolean).join(':'),
43 style,
44 ...logObj.args
45 )
46 }
47}