Python logging - multiple modules

2024/10/15 22:22:33

I'm working on a small python project that has the following structure -

project -- logs-- project__init.py__classA.pyclassB.pyutils.py-- main.py

I've set up the logging configuration in __init.py__ under project as follows:

import logging
from logging import StreamHandler
from logging.handlers import RotatingFileHandler# Create the Logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)# Create the Handler for logging data to a file
logger_handler = RotatingFileHandler('logs\\mylog.log', maxBytes=1024, backupCount=5)
logger_handler.setLevel(logging.INFO)#Create the Handler for logging data to console.
console_handler = StreamHandler()
console_handler.setLevel(logging.INFO)# 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)
console_handler.setFormatter(logger_formatter)# Add the Handler to the Logger
logger.addHandler(logger_handler)
logger.addHandler(console_handler)

Setting up things this way seems to set up the root logger at the package level, and not at the entire project level. As a result, logged statements inside main.py don't appear in the log file, whereas all log statements in the two classes classA and classB as well as utils.py are routed to the console and the log file as expected.

How do I set up the logging so that I'm able to configure it once and use it across the project? I tried moving the logging configuration statements to main.py, but it didn't seem to work.

def setupLogging():# Create the Loggerlogger = logging.getLogger(__name__)logger.setLevel(logging.INFO)..etc..def main():   setupLogging()if __name__ == "__main__":main()
Answer

It is correct to configure logging only once for the whole project, as you first tried, not each package separately.

What you did wrong is you configured the logger for the current module only:

logger = logging.getLogger(__name__)

Instead of that, you want to configure the root logger:

root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)# ...root_logger.addHandler(logger_handler)
root_logger.addHandler(console_handler)

The configuration done for the root logger applies to every logger which does not explicitly override it.

Then you should use the specific logger when actually logging:

logger = logging.getLogger(__name__)logger.warning("I am warning you about %s", something)
https://en.xdnf.cn/q/69233.html

Related Q&A

Can you search backwards from an offset using a Python regular expression?

Given a string, and a character offset within that string, can I search backwards using a Python regular expression?The actual problem Im trying to solve is to get a matching phrase at a particular of…

Django AttributeError: Form object has no attribute _errors

Im overriding the init method in my form andthis is now returning an error TransactionForm object has no attribute _errors. I would expect this to work because Ive included super in my init, however pe…

Add new keys to a dictionary while incrementing existing values

I am processing a CSV file and counting the unique values of column 4. So far I have coded this three ways. One uses "if key in dictionary", the second traps the KeyError and the third uses &…

ImportError: cannot import name aiplatform from google.cloud (unknown location)

I was wondering where that error comes from. The package has to be installed additionally to google.cloud

What does : TypeError: cannot concatenate str and list objects mean?

What does this error mean?TypeError: cannot concatenate str and list objectsHeres part of the code:for j in (90.,52.62263.,26.5651.,10.8123.):if j == 90.:z = (0.)elif j == 52.62263.:z = (0., 72., 144.…

How do I create a fixed-length, mutable array of Python objects in Cython?

I need to have an array of python objects to be used in creating a trie datastructure. I need a structure that will be fixed-length like a tuple and mutable like a list. I dont want to use a list bec…

How to install atari-py in Windows 10? [duplicate]

This question already has answers here:OpenAI Gym Atari on Windows(5 answers)Closed 3 years ago.I tried to install lib pack atari-py, and can not find any clear information, most of them wrote that it …

Using pyplot to create grids of plots

I am new to python and having some difficulties with plotting using pyplot. My goal is to plot a grid of plots in-line (%pylab inline) in Juypter Notebook.I programmed a function plot_CV which plots cr…

matplotlib: deliberately block code execution pending a GUI event

Is there some way that I can get matplotlib to block code execution pending a matplotlib.backend_bases.Event?Ive been working on some classes for interactively drawing lines and polygons inside matplo…

Connecting Keras models / replacing input but keeping layers

This questions is similar to Keras replacing input layer. I have a classifier network and an autoencoder network and I want to use the output of the autoencoder (i.e. encoding + decoding, as a preproce…