Log output keeps line numbers
logs don't keeps line numbers
If you use webpack or browserify, the easiest way to keep line numbers and also add prefix is to use string-replace-loader to add prefix.
An example for webpack that adds [Super-preloader] prefix is as the following:
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: "babel-loader"
},
{
loader: "string-replace-loader",
options: {
search: "(logger\\.(?:trace|debug|info|warn|error))\\((.*)\\)",
replace: (match, p1, p2) => {
// All string must be wrapped in double quote
const inBracket = [false, false];
const splitLoc = [];
for (let i = 0; i < p2.length; i++) {
if (p2[i] === '"' && (i == 0 || p2[i - 1] !== "\\")) {
inBracket[0] = !inBracket[0];
} else if (p2[i] === "`") {
inBracket[1] = !inBracket[1];
}
if (p2[i] === "," && !inBracket[0] && !inBracket[1]) {
splitLoc.push(i);
}
}
const args = [];
for (let i = 0; i < splitLoc.length; i++) {
const iBegin = i == 0 ? 0 : splitLoc[i - 1] + 1;
args.push(p2.slice(iBegin, splitLoc[i]));
}
if (splitLoc.length === 0) {
args.push(p2);
} else {
args.push(p2.slice(splitLoc[splitLoc.length - 1] + 1));
}
return `${p1}("[Super-preloader]", ${args.join(",")})`;
},
flags: "g"
}
}
]
}
]
},
Do we have a solution for non webpack users?
One of the features of loglevel is to keep the original line numbers. This plugin breaks this feature.
If someone helps. I made a modification in the LogLevel code to add the formatted suffix:
https://github.com/itamayo/loglevel PR -> https://github.com/pimterry/loglevel/pull/168