简体   繁体   中英

Python Logging writing to terminal and log file

I have two files: script.py and functions.py . In functions.py , I have logger setup, and a set of functions (made up one below):

class ecosystem():
    def __init__(self, environment, mode):
        self.logger = logging.getLogger(__name__)
            if os.path.exists('log.log'):
                os.remove('log.log')

            handler= logging.FileHandler('log.log')
            if mode.lower()== 'info':
                handler.setLevel(logging.INFO)
                self.logger.setLevel(logging.INFO)
            elif mode.lower()== 'warning':
                handler.setLevel(logging.WARNING)
                self.logger.setLevel(logging.WARNING)
            elif mode.lower()== 'error':
                handler.setLevel(logging.ERROR)
                self.logger.setLevel(logging.ERROR)
            elif mode.lower()== 'critical':
                handler.setLevel(logging.CRITICAL)
                self.logger.setLevel(logging.CRITICAL)
            else:
                handler.setLevel(logging.DEBUG)
                self.logger.setLevel(logging.DEBUG)

            #Logging file format
            formatter = logging.Formatter('    %(levelname)s | %(asctime)s | %(message)s \n')
            handler.setFormatter(formatter)
            #Add the handler to the logger
            self.logger.addHandler(handler)

            self.logger.info('Logging starts here')

    def my_function():
        self.logger.debug('test log'))
        return True

I'm trying to call ecosystem.my_function from script.py , but when I do, the logger.debug message shows up in both the terminal window AND log.log . Any ideas why this might be happening?

If it helps, I also import other modules into functions.py , if those modules import logging as well, could that cause issues?

It looks like you're initializing the logger with log.log file inside the __init__ method of ecosystem class. This means that any code that creates an object of ecosystem will initialize the logger. Somewhere in your code, in one the files, you are creating that object and hence the logger is initialized and writes to the file.

Note that you do not need to call __init__ yourself as it is called on object creation. ie. after this call

my_obj = ecosystem()

log files will be written.

You're asking why both stderr and file is used after your new file handler is attached. This is because of the propagate attribute. By default propagate is True and this means your log will bubble up the hierarchy of loggers and each one will continue handling it. Since default root logger is at the top of the hierarchy, it will be handling your log too. Set propagate to False fix this:

self.logger.propagate = False

You might want to read up a bit on logging . Also, if you want to keep your sanity regarding logging, check how you can use dict to configure logging .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM