(level: DebugLevel, scope: string | undefined, messages: any[])
| 45 | } |
| 46 | |
| 47 | function log(level: DebugLevel, scope: string | undefined, messages: any[]) { |
| 48 | const levelOrder: DebugLevel[] = ['trace', 'debug', 'info', 'warn', 'error']; |
| 49 | |
| 50 | if (levelOrder.indexOf(level) < levelOrder.indexOf(currentLevel)) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | const allMessages = messages.reduce((acc, current) => { |
| 55 | if (acc.endsWith('\n')) { |
| 56 | return acc + current; |
| 57 | } |
| 58 | |
| 59 | if (!acc) { |
| 60 | return current; |
| 61 | } |
| 62 | |
| 63 | return `${acc} ${current}`; |
| 64 | }, ''); |
| 65 | |
| 66 | if (!supportsColor) { |
| 67 | console.log(`[${level.toUpperCase()}]`, allMessages); |
| 68 | |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | const labelBackgroundColor = getColorForLevel(level); |
| 73 | const labelTextColor = level === 'warn' ? 'black' : 'white'; |
| 74 | |
| 75 | const labelStyles = getLabelStyles(labelBackgroundColor, labelTextColor); |
| 76 | const scopeStyles = getLabelStyles('#77828D', 'white'); |
| 77 | |
| 78 | const styles = [labelStyles]; |
| 79 | |
| 80 | if (typeof scope === 'string') { |
| 81 | styles.push('', scopeStyles); |
| 82 | } |
| 83 | |
| 84 | console.log(`%c${level.toUpperCase()}${scope ? `%c %c${scope}` : ''}`, ...styles, allMessages); |
| 85 | } |
| 86 | |
| 87 | function getLabelStyles(color: string, textColor: string) { |
| 88 | return `background-color: ${color}; color: white; border: 4px solid ${color}; color: ${textColor};`; |
no test coverage detected