spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

Use logging configuration file to configure logging level (like easyloggingcpp).

Open pauldotknopf opened this issue 10 years ago • 19 comments

pauldotknopf avatar Jan 10 '16 12:01 pauldotknopf

yes, really useful functionality !

mipac avatar Jan 13 '16 14:01 mipac

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.

batduder avatar Nov 28 '16 15:11 batduder

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…

kierun avatar Dec 22 '16 14:12 kierun

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..

gabime avatar Dec 22 '16 14:12 gabime

If the code has bug and requests a non existing logger, the nullptr will 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?

kierun avatar Dec 22 '16 14:12 kierun

maybe add a ser_logger(..) to the library API? (or just enable_logging() if you do not want to expose spdlog from the lib)

gabime avatar Dec 22 '16 16:12 gabime

Maybe add a ser_logger(..) to the library API? (or just enable_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.

kierun avatar Jan 03 '17 11:01 kierun

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 avatar Feb 14 '17 15:02 gegles

@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.

kierun avatar Feb 14 '17 15:02 kierun

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.

DigiTester avatar Oct 10 '17 00:10 DigiTester

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.

kierun avatar Oct 10 '17 07:10 kierun

Thanks. I was just hoping, you know :)

DigiTester avatar Oct 11 '17 22:10 DigiTester

Maybe this project can help?

gabime avatar Oct 12 '17 04:10 gabime

@gabime Yes, it could. ☺ Thank you for sharing it.

kierun avatar Oct 12 '17 07:10 kierun

If you try it, please share your expience with it. It might be useful for many users.

gabime avatar Oct 12 '17 10:10 gabime

@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 avatar Jan 12 '18 20:01 p0fi

@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!

guangie88 avatar Feb 11 '18 13:02 guangie88

i don't have time to give a try know, but thank you very much for sharing!

mipac avatar Feb 12 '18 19:02 mipac

@guangie88 thats awesome to hear! I'll definitely give it a try again.

p0fi avatar Feb 27 '18 21:02 p0fi