@nitra/consola
Version:
consola with filename
64 lines (52 loc) • 1.5 kB
JavaScript
/* global location, process */
import consola from 'consola/src/browser'
import BrowserReporter from 'consola/src/reporters/browser'
const options = {}
// Vite Debug mode
if (typeof __CONSOLA_LEVEL_DEBUG__ !== 'undefined') {
options.level = 4
} else if (typeof location !== 'undefined' && location.protocol === 'http:') {
options.level = 4
} else if (typeof process !== 'undefined' && (process.env.DEV || process.env.CONSOLA_LEVEL_DEBUG)) {
// Quasar Debug mode
options.level = 4
}
class NReporter extends BrowserReporter {
log(logObj) {
if (this.options.currentFile) {
logObj.tag = this.options.currentFile
}
super.log(logObj)
}
}
/**
* pass import.meta.url
* example: const consola = createLogger(import.meta.url)
*
* @param {String} url
* @return {Consola}
*/
export const createLogger = url => {
let currentFile
if (url) {
currentFile = url
// webpack 5.x or Vite
const srcPosition = currentFile.indexOf('/src/')
if (srcPosition > 0) {
currentFile = currentFile.slice(srcPosition + 5)
}
// webpack 4.x
const questPos = currentFile.indexOf('?')
if (questPos > 0) {
currentFile = currentFile.slice(0, questPos)
}
// For Vue sfc
const percentPos = currentFile.indexOf('%')
if (percentPos > 0) {
currentFile = currentFile.slice(0, percentPos)
}
}
options.reporters = [new NReporter({ currentFile })]
return consola.create(options)
}
export default consola.create(options)