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

Clean up log output #1896

Merged
merged 12 commits into from
Oct 29, 2024
2 changes: 1 addition & 1 deletion .github/workflows/distro_tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Linux Distro Tests
name: Tests (Linux Distros)
on:
pull_request:

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: tests
name: Tests (Python Versions)
on:
push:
branches:
Expand Down
29 changes: 29 additions & 0 deletions bbot/core/config/logger.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys
import atexit
import logging
from copy import copy
import multiprocessing
import logging.handlers
from pathlib import Path
from contextlib import suppress

from ..helpers.misc import mkdir, error_and_exit
from ...logger import colorize, loglevel_mapping
Expand Down Expand Up @@ -70,9 +72,36 @@ def __init__(self, core):
# Start the QueueListener
self.listener = logging.handlers.QueueListener(self.queue, *self.log_handlers.values())
self.listener.start()
atexit.register(self.cleanup_logging)

self.log_level = logging.INFO

def cleanup_logging(self):
# Close the queue handler
with suppress(Exception):
self.queue_handler.close()

# Clean root logger
root_logger = logging.getLogger()
for handler in list(root_logger.handlers):
with suppress(Exception):
root_logger.removeHandler(handler)
with suppress(Exception):
handler.close()

# Clean all other loggers
for logger in logging.Logger.manager.loggerDict.values():
if hasattr(logger, "handlers"): # Logger, not PlaceHolder
for handler in list(logger.handlers):
with suppress(Exception):
logger.removeHandler(handler)
with suppress(Exception):
handler.close()

# Stop queue listener
with suppress(Exception):
self.listener.stop()

def setup_queue_handler(self, logging_queue=None, log_level=logging.DEBUG):
if logging_queue is None:
logging_queue = self.queue
Expand Down
7 changes: 7 additions & 0 deletions bbot/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def assert_all_responses_were_requested() -> bool:
return False


@pytest.fixture(autouse=True)
def silence_live_logging():
for handler in logging.getLogger().handlers:
if type(handler).__name__ == "_LiveLoggingStreamHandler":
handler.setLevel(logging.CRITICAL)


@pytest.fixture
def bbot_httpserver():
server = HTTPServer(host="127.0.0.1", port=8888, threaded=True)
Expand Down
Loading