Search code examples
pythonpython-logging

I can't get logging.config to load my custom class


I have a custom formatter defined in my log_conf.py.

# log_conf.py
class Custom(jsonlogger.JsonFormatter):
    def add_fields(self, log_record, record, message_dict):
        super().add_fields(log_record, record, message_dict)
        log_record["new_field"] = log_record["levelname"]

logging.config.fileConfig(file_path)

def get_logger(name):
    return logging.getLogger(name)

In my logger.ini file I reference it like this:

....
[formatter_json]
class = log_conf.Custom

If I throw my log_conf.py and logger.ini in the same module that is using it this works:

from my_project.my_module import log_conf
log = log_conf.get_logger(__name__)

But I want to put it in a different folder like this:

my_project
    main.py
    my_module/
        stuff.py
    myconf/
        logger.ini
        log_conf.py

If I use this structure and do from my_project.myconf import log_conf I get ModuleNotFoundError: No module named 'log_conf'.

If I change the ini file to class = myconf.log_conf.Custom it still can't find it.

If I change it to class = my_project.myconf.log_conf.Custom I get AttributeError: cannot access submodule 'log_config' of module 'my_project.myconf' (most likely due to a circular import).

Why does it work when they are in the module's dir and throw circular import when using the full namespace of the Custom class for class=?


Solution

  • Realized the problem was having my custom class defined in the same file that was loading the config duh.

    If the file that is calling logging.config.fileConfig("logging_ini_file") includes the class the ini file is pointing to ie: class = myproj.mymodule.file_that_loaded_this_ini_with_fileConfig.CustomThing you get a circular import because fileConfig() is trying to re-import the file that called it.