Skip to content

Commit

Permalink
disable/enable
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-usielski committed Jan 14, 2025
1 parent 817e41a commit d21f6d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 8 additions & 1 deletion moler/config/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -84,6 +87,7 @@
%% %%% %%% %% %%% %% %%% %%%%%%%%% %%% %%%%%%%
%% %%%%% %% %%% %% %%% %%% %%% %%%
%% %%% %% %%%%%%%%%%% %%%%%%%%% %%%%%%%%%% %%% %%%
disable log 1
"""


Expand Down Expand Up @@ -635,16 +639,19 @@ def configure_moler_threads_logger():
logging.getLogger("moler_threads").propagate = False


def change_debug_log(disable: bool) -> None:
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()
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

0 comments on commit d21f6d4

Please sign in to comment.