loglevel-plugin-prefix icon indicating copy to clipboard operation
loglevel-plugin-prefix copied to clipboard

Log output keeps line numbers

Open ricardojlrufino opened this issue 6 years ago • 4 comments

logs don't keeps line numbers

ricardojlrufino avatar Feb 24 '19 20:02 ricardojlrufino

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"
            }
          }
        ]
      }
    ]
  },

machsix avatar Dec 18 '19 20:12 machsix

Do we have a solution for non webpack users?

j-pollack avatar Jul 22 '20 10:07 j-pollack

One of the features of loglevel is to keep the original line numbers. This plugin breaks this feature.

J-Rojas avatar Jan 20 '21 09:01 J-Rojas

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

itamayo avatar Jun 16 '21 12:06 itamayo