'Python module not producing logs after being imported

I am building a tool with the following file structure:

+- my_tool
|   +- logs_dir
|   +- my_package
|   |   +- __init__.py  # main __init__
|   |   +- classes
|   |   |   +- __init__.py  # empty __init__
|   |   |   +- my_class.py
|   |   +- util
|   |   |   +- __init__.py  # empty __init__
|   |   |   +- _logger.py
|   |   +- tests
|   |   |   +- __init.py__  # empty __init__
|   |   |   +- test_my_class.py
|   |   +- config
|   |   |   +- logger.json
+- my_script.py

The python scripts contain the following code:

main __init__.py

from my_package.util import _logger

my_class.py

import logging

class TheClass():

    def __init__(self):
        
        self.log = logging.getLogger(__name__)
        self.log.info('this is a log output!')
    
    def a_method(self):
        return 'a string that is returned...'

_logger.py

import logging
import logging.config
from pathlib import Path
import json

logging_config_path = Path(__file__).parent.parent / 'config/logger.json'
with open(logging_config_path, 'r', encoding='utf-8') as config_file:
    LOGGING_CONFIG = json.load(config_file)
    
log_path = Path(__file__).parent.parent.parent

log_file = LOGGING_CONFIG['handlers']['file']['filename']
LOGGING_CONFIG['handlers']['file']['filename'] = log_path.join_path(log_file)

logging.config.dictConfig(LOGGING_CONFIG)

test_my_class.py

import pytest
from my_package.classes.my_class import TheClass

def test_the_class():
    the_class = TheClass()
    assert the_class.a_method() == 'a string that is returned...'

my_script.py

from my_tool.my_package.classes.my_class import TheClass

the_class = TheClass()
print(the_class.a_method())

logger.json

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "standard": {
            "format": "%(asctime)s [%(levelname)s] %(name)|%(lineno)d:: %(message)s",
            "datefmt": "%Y-%m-%d %H:%M:%S"
        }
    },
    "handlers": {
        "default": {
            "formatter": "standard",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stdout"
        },
        "file": {
            "formatter": "standard",
            "class": "logging.FileHandler",
            "filename": "logs_dir/log.log"
        }
    },
    "loggers": {
        "": {
            "handlers": ["default"],
            "level": "WARNING",
            "propagate": false
        },
        "my_package": {
            "handlers": ["default", "file"],
            "level": "DEBUG", 
            "propagate": false
        }
    }
}

When I run test_my_class.py, I get logs in both the terminal and the log_dir directory. However, when I run my_script.py, no logs are produced at all.

I have tried changing "disable_existing_loggers" in the logger.json file but that does not change the behaviour.

How do I get the logs to output properly when I run my_script.py? Why is it currently not producing an output.



Solution 1:[1]

You do your logging setup in _logger.py.

When your run my_script.py the _logger.py is not executed. Because of that python's default settings for logging are relevant.

The default log-level is logging.WARNING. This means that logging.info() is lower then the default level not outputed somewhere.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 buhtz