Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it bad practice to call dictConfig more than once?

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?

like image 682
ShmulikA Avatar asked Nov 26 '17 11:11

ShmulikA


1 Answers

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!

like image 151
taras Avatar answered Nov 04 '22 00:11

taras