Node JS - Turn on/off logs based on type or levels

462 Views Asked by At

Is there a way by which I can turn on/off certain logs, based on its type/levels.

For eg: I have defined 3 levels: ALL, WARNING, CRITICAL

And I have my Log class, where I will set this. Say I set Level: 'ALL' So this will log everything, wherever I have logged messages.

Now, when I set Level: 'WARNING' This will only log messages, which are of warning type.

Can I do this with Bunyan ? Or any other module?

Please help !!

2

There are 2 best solutions below

0
On

try using winston module for logging .this is good for logging and has log rotation and other features

0
On

One work around would be to use Bunyan's DTrace facilities.Keep the log level higher and if you need to inspect low level log like debug you can run Dtrace command Examples Trace all log messages coming from any Bunyan module on the system

dtrace -x strsize=4k -qn 'bunyan*:::log-*{printf("%d: %s: %s", pid, probefunc, copyinstr(arg0))}'

Trace all log messages coming from the "wuzzle" component:

dtrace -x strsize=4k -qn 'bunyan*:::log-*/strstr(this->str = copyinstr(arg0), "\"component\":\"wuzzle\"") != NULL/{printf("%s", this->str)}'

you need to manually install the "dtrace-provider" lib separately via npm install dtrace-provider Check out the documentation here