I am using logging.config.dictConfig()
to setup logging into files using FileHandler
and RotatingFileHandler
. is it bad practice to call dictConfig()
more than once? if so why?
I think the question is more broad than just going down to calling a method twice.
1) Is it a bad practice to call dictConfig
more than once? - No, it is not. You can use it happily as long as you understand what happens.
2) Can it be considered a bad practice or lead to unexpected results in some context? - Yes, for sure.
If you check the source code of dictConfig
you will not find anything that could possibly harm running code. All configuration is wrapped in a lock so calling method is safe.
However, what you should be aware is what happens to existing loggers in _handle_existing_loggers
.
By default all existing loggers are going to be disabled and this is important, read it again, disabled, not removed. They still exist but do not any work anymore and, for instance, in they might prevent message propagation to parent loggers. (more reading here, actual code: python source)
You can test what happens with loggers using this Gist (while I do not agree with a gist description).
Overall, my advice would be - try to configure all the loggers at once. There are cases when you need to add loggers on the fly, which is not bad, just make sure you know what happens or just pass False
as disable_existing_loggers
parameter if you want to extend loggers config.
Cheers and safe coding!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With