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.
In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.
They are located in the logging. config module. Their use is optional — you can configure the logging module using these functions or by making calls to the main API (defined in logging itself) and defining handlers which are declared either in logging or logging.
Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.
The default level is WARNING , which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise. Events that are tracked can be handled in different ways. The simplest way of handling tracked events is to print them to the console.
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
in __init__.py
file,
# Create the Logger
loggers = logging.getLogger(__name__)
the logger name is <python_logging>
.
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
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