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

Feature request: severity #12

Open
ourarash opened this issue Jun 28, 2019 · 1 comment
Open

Feature request: severity #12

ourarash opened this issue Jun 28, 2019 · 1 comment

Comments

@ourarash
Copy link
Contributor

ourarash commented Jun 28, 2019

I was able to hack a severity level so that a message only prints if its severity is above the min severity level. This is useful when for example you run the program in a verbose vs non-verbose mode.

Here is what worked for me:

const bullet = require("string.bullet");
const { cyan, yellow, red, dim, blue } = require("ansicolor");

var log = require("ololog").configure({
  locate: false,
  infoSeverity: 5,
  tag: (
    lines,
    {
      level = "",
      minSeverity=10,
      severity = 11,
      levelColor = {
        info: cyan,
        warn: yellow,
        error: red.bright.inverse,
        debug: blue
      }
    }
  ) => {
    const levelStr =
      level && (levelColor[level] || (s => s))(level.toUpperCase());
    if (severity >= minSeverity) {
      return bullet(levelStr.padStart(6) + "  ", lines);
    } else return [];
  }
});

log("This prints!");

log
.configure({tag:{severity:12}})
.info("This will print, severity 12!");

log
.configure({tag:{severity:1}})
.info("This will print, severity 1!");

log
.configure({tag:{severity:15}})
.info("This will print, severity 15!");

log = log
.configure({tag:{minSeverity:1000}}); // change severity

log
.configure({tag:{severity:12}})
.info("This will not print, severity 12!");

log
.configure({tag:{severity:1}})
.info("This will not print, severity 1!");

log
.configure({tag:{severity:15}})
.info("This will not print, severity 15!");

However, this is ugly. Ideally, it should be like this:

var log = require("ololog").configure({
  locate: false,
  infoMinSeverity: 5,
  warnMinSeverity: 6,
  debugMinSeverity: 8,
  errorMinSeverity: 10,
  minSeverity: 15, //without tag
});

log.severity(5).info('This is my message');
log.configure({infoMinSeverity:0}).info('This is my message');
@xpl
Copy link
Owner

xpl commented Jun 29, 2019

@ourarash Have you looked into this: https://github.com/xpl/ololog#adding-custom-helper-methods ?

Try that code:

log = require ('ololog')
        .configure (/* YOUR CONFIG HERE */)
        .methods ({
            severity (n) { return this.configure ({ tag: { severity: n } }) })
        })

This will allow you to do this:

log.severity(5).info('This is my message')

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

2 participants