Python - Question on logging in __init__.py

Viewed 11492

I have the following python package structure.

python_logging
    python_logging
        __init__.py
        first_class.py
        second_class.py
    run.py

Here is the code in __init__.py

init.py

import logging
import logging.config


# Create the Logger
loggers = logging.getLogger(__name__)
loggers.setLevel(logging.DEBUG)

# Create the Handler for logging data to a file
logger_handler = logging.FileHandler(filename='C:\Python\Log\stest.txt')
logger_handler.setLevel(logging.DEBUG)

# Create a Formatter for formatting the log messages
logger_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')

# Add the Formatter to the Handler
logger_handler.setFormatter(logger_formatter)

# Add the Handler to the Logger
loggers.addHandler(logger_handler)
loggers.info('Completed configuring logger()!') 

Here is code for first_class.py

import logging

class FirstClass(object):
    def __init__(self):
        self.current_number = 0
        self.logger = logging.getLogger(__name__)


    def increment_number(self):
        self.current_number += 1
        self.logger.warning('Incrementing number!')
        self.logger.info('Still incrementing number!!')

Here is code for second_class.py

class SecondClass(object):
    def __init__(self):
        self.enabled = False
        self.logger = logging.getLogger(__name__)

    def enable_system(self):
        self.enabled = True
        self.logger.warning('Enabling system!')
        self.logger.info('Still enabling system!!')

Here is code for run.py

from LogModule.first_class import FirstClass
from LogModule.second_class import SecondClass

number = FirstClass()
number.increment_number()
system = SecondClass()
system.enable_system()

This is the output in the log file

LogModule - INFO - Completed configuring logger()!
LogModule.first_class - WARNING - Incrementing number!
LogModule.first_class - INFO - Still incrementing number!!
LogModule.second_class - WARNING - Enabling system!
LogModule.second_class - INFO - Still enabling system!!

Question : How did number.increment_number() and system.enable_system() write to a log file when the file handler was initialized in init.py ? also both the classes have different getloggers . Can anyone explain , it will be helpful.

1 Answers

Every logger has a parent - in Python you can think that all loggers construct into a single "tree". If you add handlers into one logger, its children loggers will also share these handlers. This means you have no need to create handlers for each logger - otherwise setting handlers on every logger will be repetitive and boring.

Back to your sample, your package structure,

python_logging
    python_logging
        __init__.py
        first_class.py
        second_class.py
    run.py
  1. in __init__.py file,

    # Create the Logger
    loggers = logging.getLogger(__name__)
    

    the logger name is <python_logging>.

  2. in first_class.py

    self.logger = logging.getLogger(__name__)
    

    the logger name is <python_logging>.<first_class>.

So the logger in first_class.py is the child of the logger in __init__.py

Related