Skip to content

Commit

Permalink
Merge pull request #1896 from blacklanternsecurity/test-cleanup
Browse files Browse the repository at this point in the history
Clean up log output
  • Loading branch information
TheTechromancer authored Oct 29, 2024
2 parents d83dba4 + 1837f0a commit de740f6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
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

0 comments on commit de740f6

Please sign in to comment.