sergees
sergees10mo ago

Choose level for std logger

I have some loggers configured using the setup function from std/log. This way levels are hard-coded. I would like to be able to pass a --debug option to my script to activate the DEBUG level, otherwise use INFO and above. Is it possible to achieve this using std/log? If not, what are other libs you can recommend instead?
9 Replies
marvinh.
marvinh.10mo ago
Detect the CLI flag and set the appropriate log level on the Logger instance (or your custom once). Only log levels equal to the one on the Logger instance or higher are logged
sergees
sergeesOP10mo ago
There a function getLogger which return logger instance. It only accepts name as a parameter, no way to pass level there. The level doesn’t seem to have a setter, so this code won’t work:
const logger = getLogger('my_logger');
logger.level = 'DEBUG';
const logger = getLogger('my_logger');
logger.level = 'DEBUG';
Ah okay levelName seems to work
cknight
cknight10mo ago
Sounds like you've got a working solution. I've written an alternative logging framework which can do this through the use of environment variables or command line options. https://github.com/onjara/optic
Leokuma
Leokuma10mo ago
Much better than std's logger IMO
NDH
NDH10mo ago
Perhaps a pr to upgrade std before std 1.0 ?
cknight
cknight10mo ago
Thanks! That's kind of you, however, my logger isn't really a good fit for std I think. For starters the fluent interface would be out of place there.
NDH
NDH10mo ago
Just a thought. Love your work!
sergees
sergeesOP10mo ago
Nice! One thing I’m missing in std logger is the trace level
iuioiua
iuioiua10mo ago
Suggestions for features for std/log are more than welcome! If you guys have any, feel free to open a GitHub issue.