1

I've been reading up on logging, and I think I understand what is happening but I do not know how to correct it. I have a file where I want to set up my logger, and then I want my application to use that configured logger, here is my logger.py file:

import logging
import sys

logger = logging.getLogger('mylogger')
logger.setLevel(logging.INFO)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter(
    '{levelname} {asctime} {name} {lineno}: {message}', style='{')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

Now, I have another file that handles certain functionality and it needs to use this logger. Let's call it some_class.py So what I have done is:

import logging

logger = logging.getLogger('mylogger.someClass')


class SomeClass:
    def __init__(self):
        pass

    def do_something(self):
        logger.critical('A critical message')

Now in a runner.py file I do:

import logger

from some_class import SomeClass

SomeClass().do_something()

My output is:

CRITICAL 2019-10-04 07:38:38,579 mylogger 25: critical message
CRITICAL 2019-10-04 07:38:38,592 mylogger.SomeClass 12: A critical message

Now the problem is that I only want to show the class-level logger's message. I have also tried moving the logger inside the class as a class variable and then referring to self.logger instead but it yields the same result. Is there a way to disable the root logger or how would I only get one log message in this case?

Dave
  • 329
  • 1
  • 5
  • 16

0 Answers0