Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple messages sent, need to remove the logging handlers. #24

Open
coolkau opened this issue Nov 7, 2022 · 1 comment
Open

Multiple messages sent, need to remove the logging handlers. #24

coolkau opened this issue Nov 7, 2022 · 1 comment

Comments

@coolkau
Copy link

coolkau commented Nov 7, 2022

Every time timeloop is started it adds a new logging handlers causing issues e.g. when using flask. Messages are sent multiple times. This could be fixed by releasing the handlers in the stop method by adding:

        for h in self.logger.handlers:
            self.logger.removeHandler(h)

Example:

tl = Timeloop()
tl = Timeloop()
tl = Timeloop()
tl = Timeloop()
tl = Timeloop()

every message is sent 5 times:


[2022-11-07 15:00:12,195] [timeloop] [INFO] Starting Timeloop..
[2022-11-07 15:00:12,195] [timeloop] [INFO] Starting Timeloop..
[2022-11-07 15:00:12,195] [timeloop] [INFO] Starting Timeloop..
[2022-11-07 15:00:12,195] [timeloop] [INFO] Starting Timeloop..
[2022-11-07 15:00:12,195] [timeloop] [INFO] Starting Timeloop..
[2022-11-07 15:00:12,195] [timeloop] [INFO] Starting Timeloop..

Note that in this example the timeloop will still print multiple messages after my fix, as only the handler of the last instance is removed. I guess to make it cleanly the timeloop should be a "with as", the logging handler should only be attached once the process is started, use of the destructor... or check for existing handlers before creating a new one.

@coolkau
Copy link
Author

coolkau commented Nov 7, 2022

Disclaimer: I am not an expert so my suggestion might be wrong but this fixed it for me:

Move the logger out of __init__ into a separate function and call those functions in start() and stop():

    def add_logger(self):
        logger = logging.getLogger('timeloop')
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.INFO)
        formatter = logging.Formatter('[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s')
        ch.setFormatter(formatter)
        logger.addHandler(ch)
        logger.setLevel(logging.INFO)
        self.logger = logger

    def remove_logger(self):
        for h in self.logger.handlers:
            self.logger.removeHandler(h)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant