0

I have two python scripts in the same directory. I try to catch logging messages from both of them:

#script.py
import requests
import logging

logger = logging.getLogger(__name__)

class Downloader:
    
    def __init__(self, url):
        self.url = url
        
    def download(self):
        logger.debug(f'Downloading {self.url}')
        req = requests.get(self.url, timeout=1)
        return req
#main.py
import logging
from script import Downloader

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)

logger.debug('create object')

d = Downloader('https://www.google.com')
res = d.download()

Basically I want to get rid of the debug-messages from the requests-module, so using logging.basicConfig() is not an option. But the way I do it, I do not get the debug-message from the imported script. Apparently because in script.py __name__ is not main.script.

How can I achieve this without hard coding anything to a string?

1 Answer 1

1

In a different file (e.g. logger.py):

import logging

def setup_logger(name, logfile, formatter, stream_handler=False, level=logging.DEBUG):
    """Function to create loggers."""

    file_handler   = logging.FileHandler(logfile)
    stdout_handler = logging.StreamHandler()

    file_handler.setFormatter(formatter)
    stdout_handler.setFormatter(formatter)

    logger = logging.getLogger(name)

    if not logger.handlers:
        logger.setLevel(level)
        logger.addHandler(file_handler)
        if stream_handler:
            logger.addHandler(stdout_handler)

    return logger

# Example formatter
formatter = logging.Formatter('%(asctime)s - %(levelname)s -> %(message)s\n')

# Generate the log object
log = setup_logger('logger_name', 'path_to_logfile', formatter)

Import this log object from your other modules to use it: from logger import log

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.