Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log output keeps line numbers #4

Open
ricardojlrufino opened this issue Feb 24, 2019 · 4 comments
Open

Log output keeps line numbers #4

ricardojlrufino opened this issue Feb 24, 2019 · 4 comments

Comments

@ricardojlrufino
Copy link

logs don't keeps line numbers

@machsix
Copy link

machsix commented Dec 18, 2019

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

@jonathan3692bf
Copy link

Do we have a solution for non webpack users?

@J-Rojas
Copy link

J-Rojas commented Jan 20, 2021

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

@itamayo
Copy link

itamayo commented Jun 16, 2021

If someone helps. I made a modification in the LogLevel code to add the formatted suffix:

https://github.com/itamayo/loglevel
PR -> pimterry/loglevel#168

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants