(
level: LogLevel = 'info',
options: LoggerOptions = {},
)
| 66 | } |
| 67 | |
| 68 | export function createLogger( |
| 69 | level: LogLevel = 'info', |
| 70 | options: LoggerOptions = {}, |
| 71 | ): Logger { |
| 72 | if (options.customLogger) { |
| 73 | return options.customLogger |
| 74 | } |
| 75 | |
| 76 | const loggedErrors = new WeakSet<Error | RollupError>() |
| 77 | const { |
| 78 | prefix = '[vite]', |
| 79 | allowClearScreen = true, |
| 80 | console = globalThis.console, |
| 81 | } = options |
| 82 | const thresh = LogLevels[level] |
| 83 | const canClearScreen = |
| 84 | allowClearScreen && process.stdout.isTTY && !process.env.CI |
| 85 | const clear = canClearScreen ? clearScreen : () => {} |
| 86 | |
| 87 | function format(type: LogType, msg: string, options: LogErrorOptions = {}) { |
| 88 | if (options.timestamp) { |
| 89 | let tag = '' |
| 90 | if (type === 'info') { |
| 91 | tag = colors.cyan(colors.bold(prefix)) |
| 92 | } else if (type === 'warn') { |
| 93 | tag = colors.yellow(colors.bold(prefix)) |
| 94 | } else { |
| 95 | tag = colors.red(colors.bold(prefix)) |
| 96 | } |
| 97 | const environment = options.environment ? options.environment + ' ' : '' |
| 98 | return `${colors.dim(getTimeFormatter().format(new Date()))} ${tag} ${environment}${msg}` |
| 99 | } else { |
| 100 | return msg |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function output(type: LogType, msg: string, options: LogErrorOptions = {}) { |
| 105 | if (thresh >= LogLevels[type]) { |
| 106 | const method = type === 'info' ? 'log' : type |
| 107 | |
| 108 | if (options.error) { |
| 109 | loggedErrors.add(options.error) |
| 110 | } |
| 111 | if (canClearScreen) { |
| 112 | if (type === lastType && msg === lastMsg) { |
| 113 | sameCount++ |
| 114 | clear() |
| 115 | console[method]( |
| 116 | format(type, msg, options), |
| 117 | colors.yellow(`(x${sameCount + 1})`), |
| 118 | ) |
| 119 | } else { |
| 120 | sameCount = 0 |
| 121 | lastMsg = msg |
| 122 | lastType = type |
| 123 | if (options.clear) { |
| 124 | clear() |
| 125 | } |
no outgoing calls
no test coverage detected