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

Disable debug logging #560

Merged
merged 11 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: [3.7, 3.8, 3.9, "3.10", 3.11, 3.12, 3.13]
python-version: [3.8, 3.9, "3.10", 3.11, 3.12, 3.13]
exclude:
- os: macos-latest
python-version: 3.7
- os: macos-latest
python-version: 3.8
- os: macos-latest
Expand Down
70 changes: 52 additions & 18 deletions moler/config/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

__author__ = "Grzegorz Latuszek, Marcin Usielski, Michal Ernst"
__copyright__ = "Copyright (C) 2018-2024, Nokia"
__copyright__ = "Copyright (C) 2018-2025, Nokia"
__email__ = (
"[email protected], [email protected], [email protected]"
)
Expand All @@ -16,8 +16,11 @@
import platform
import re
import sys
import traceback
from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler
from importlib_metadata import version, PackageNotFoundError, distributions
from pprint import pformat


from moler.util import tracked_thread
from moler.util.compressed_rotating_file_handler import CompressedRotatingFileHandler
Expand Down Expand Up @@ -219,13 +222,15 @@ def configure_debug_level(level=None):
if level:
level_name = level
else:
level_name = os.getenv("MOLER_DEBUG_LEVEL", "not_found").upper()
level_name = os.getenv("MOLER_DEBUG_LEVEL", None)
if level_name:
level_name = level_name.upper()

allowed = {"TRACE": TRACE, "DEBUG": logging.DEBUG}

if level_name in allowed:
debug_level = allowed[level_name]
else:
elif level_name:
debug_level = logging.INFO


Expand Down Expand Up @@ -570,21 +575,7 @@ def configure_moler_main_logger():
)
if want_log_console("moler"):
_add_stdout_file_handler(logger_name="moler", formatter=main_formatter, log_level=logging.INFO)

if want_debug_details():
debug_log_format = "%(asctime)s.%(msecs)03d %(levelname)-12s %(name)-30s %(threadName)22s %(filename)30s:#%(lineno)3s %(funcName)25s() %(transfer_direction)s|%(message)s"
debug_formatter = MultilineWithDirectionFormatter(fmt=debug_log_format, datefmt=date_format)
_add_new_file_handler(
logger_name="moler",
log_file="moler.debug.log",
log_level=debug_level,
# entries from different components go to single file, so we need to
# differentiate them by logger name: "%(name)s"
# do we need "%(threadName)-30s" ???
formatter=debug_formatter
)
if want_log_console("moler.debug"):
_add_stdout_file_handler(logger_name="moler", formatter=debug_formatter, log_level=debug_level)
_add_debug_handler()

logger.info(moler_logo)
msg = f"Using specific packages version:\nPython: {platform.python_version()}\nmoler: {_get_moler_version()}"
Expand All @@ -598,6 +589,28 @@ def configure_moler_main_logger():
_main_logger = logger


def _get_debug_formatter():
debug_log_format = "%(asctime)s.%(msecs)03d %(levelname)-12s %(name)-30s %(threadName)22s %(filename)30s:#%(lineno)3s %(funcName)25s() %(transfer_direction)s|%(message)s"
debug_formatter = MultilineWithDirectionFormatter(fmt=debug_log_format, datefmt=date_format)
return debug_formatter


def _add_debug_handler():
if want_debug_details():
debug_formatter = _get_debug_formatter()
_add_new_file_handler(
logger_name="moler",
log_file="moler.debug.log",
log_level=debug_level,
# entries from different components go to single file, so we need to
# differentiate them by logger name: "%(name)s"
# do we need "%(threadName)-30s" ???
formatter=debug_formatter
)
if want_log_console("moler.debug"):
_add_stdout_file_handler(logger_name="moler", formatter=debug_formatter, log_level=debug_level)


def configure_moler_threads_logger():
"""Configure threads logger of Moler"""
# warning or above go to logfile
Expand Down Expand Up @@ -625,6 +638,27 @@ def configure_moler_threads_logger():
logging.getLogger("moler_threads").propagate = False


def switch_debug_log_visibility(disable: bool) -> None:
"""
Change disable debug log.

:param disable: True to disable debug log, False to enable.
:return: None
"""

logger = logging.getLogger("moler")
file_handlers = [handler for handler in logger.handlers if isinstance(handler, logging.FileHandler) and handler.baseFilename.endswith("moler.debug.log")]
if disable:
mg = pformat(traceback.format_list(traceback.extract_stack(limit=7))[::-1])
logger.info(msg=f"Debug log is disabled. Requested by: {mg}\n(...)")
for handler in file_handlers:
logger.removeHandler(handler)
handler.close()
else:
if len(file_handlers) == 0:
_add_debug_handler()


def _list_libraries(logger):
"""
List installed Python libraries to log file.
Expand Down
22 changes: 20 additions & 2 deletions moler/util/moler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

__author__ = "Grzegorz Latuszek, Marcin Usielski, Michal Ernst, Tomasz Krol"
__copyright__ = "Copyright (C) 2018-2022, Nokia"
__copyright__ = "Copyright (C) 2018-2025, Nokia"
__email__ = "[email protected], [email protected], [email protected], [email protected]"

import gc
Expand All @@ -18,7 +18,7 @@
from functools import partial, wraps
from types import FunctionType, MethodType

from moler.config.loggers import get_error_log_stack
from moler.config.loggers import get_error_log_stack, switch_debug_log_visibility
from moler.connection_observer import ConnectionObserver
from moler.exceptions import ExecutionException, MolerException

Expand Down Expand Up @@ -135,6 +135,24 @@ def raise_background_exceptions(cls, decorated="function", check_steps_end=False
else:
return partial(cls._decorate, check_steps_end=check_steps_end)

@classmethod
def disable_debug_log(cls):
"""
Disable debug log.
:return: None
"""
switch_debug_log_visibility(disable=True)

@classmethod
def enable_debug_log(cls):
"""
Enable debug log.
:return: None
"""
switch_debug_log_visibility(disable=False)

# No public methods and fields below:

_was_error = False
Expand Down
Loading