Use logging configuration file to configure logging level (like easyloggingcpp).
yes, really useful functionality !
I agree. In systems where many apps/processes use spdlog, it is a need to globally configure the logging level from one global file, without recompiling code.
Would it be a good idea to have a more clever spdlog::get(…) function as well?
Something that if the requested logger is not found, a null_sink logger instance is returned instead of a nullptr. It could even create such a null logger if it does not already exists. Thus, if a logger is not defined in the configuration file, then there is no logging. If it is, then logging appears.
I am thinking of implementing that in one of my projects that requires logging in some libraries, some of the time. I do not want to force anyone using said library to have to define their own loggers. I am kinda assuming that a null logger will not impact performance drastically…
Its a bit dangerous behaviour. If the code has bug and requests a non existing logger, the nullptr will cause it to crash immediatly, which is much beter than letting the program as usual with no logging..
If the code has bug and requests a non existing logger, the
nullptrwill cause it to crash immediately, which is much better than letting the program as usual with no logging..
In some cases yes.
However, please consider the case where I am writing a library. I do not want to force the user of said library to have to define a spdlog for it, especially since they might not want to log anything. So, being able to define either the correct logger or a null one or a console one at high level would be useful.
I might have a dozen of said libraries I need to define loggers for. I might only be interested in logging a few for a particular case so would only define those. The rest would be null and thus silent.
Or am I think about this in the wrong way?
maybe add a ser_logger(..) to the library API? (or just enable_logging() if you do not want to expose spdlog from the lib)
Maybe add a
ser_logger(..)to the library API? (or justenable_logging()if you do not want to expose spdlog from the lib).
This still means that I have to define a null logger and use my own get(…) function which will be the same piece of code in many libraries.
Any timeline on this enhancement? I just integrated my project with spdlog (great stuff overall!) but I had not realized that there was no file config option..... bummer.
@gegles I have not been able to work on this for now. I have a tiny function that replaces the get(…) method for my own use. I have no idea when I will have some time to work on this, but I am not an official dev.
Is this in progress?
For me it's essential because we have multiple levels (CRAZY, TRACE, DEBUG, INFO, ERROR) and categories, then turn everything up and down depending on how loaded the system is and what problems we're having. Having to restart server programs to get a change in logging is not ideal - I would need to write my own wrapper code/macros to avoid it. I have code that watches a config file already, so if I can just tell the loggers to change behaviour that would work.
Viz, LOG_TRACE(...) becomes if(config.log_level >= LOG_LEVEL_TRACE) my_logger->info(...) if I can't just say my_logger->set_level(config.log_level); but macros get ugly very quickly, especially because gcc stops reporting errors after a couple of levels of nesting (with zlog I have a conditional define in my logging header file that directly turns DIGILOG_... into printf(...) to get those warnings/errors).
I may have to do this anyway to get categories, because zlog also has good support for auto cat = logger.new_category("some string") ... zlog(cat,...) which go into one or more sinks. I can again fake that with macros but I suspect I can also do it better with multiple loggers going into one sink. As long as I can reset those loggers at runtime, that is.
I have not been able to work on this for now. This might change in the future but is way off. In any case, I am just a user of this not one of the main developers.
Thanks. I was just hoping, you know :)
Maybe this project can help?
@gabime Yes, it could. ☺ Thank you for sharing it.
If you try it, please share your expience with it. It might be useful for many users.
@gabime are you still curious for experience? I tried it and it generally works nice. Its super convenient to setup all the logging for a project from a config file like this. The only thing which I have on it is that it has so many dependencies and requires a C++14 compiler. Its not entirely clear for me why this is necessary for a functionality like this. It would be a huge plus for spdlog to support a feature like this out of the box without that many dependencies and with C++11.
@p0fi I am the author of spdlog_setup and thanks for your feedback!
You might be interested to note that I have actually made amendments to the project from v0.2.0 onwards such that it is now exception based, removing many of the external dependencies, and requiring only include/spdlog and include/spdlog_setup side by side. It should now also only require C++11.
Also thanks to @gabime for the excellent and well-maintained logging library!
i don't have time to give a try know, but thank you very much for sharing!
@guangie88 thats awesome to hear! I'll definitely give it a try again.