From 34773ed2255b318e5517cbc1321fdb8f0ddbc3d0 Mon Sep 17 00:00:00 2001 From: Czar Echavez Date: Thu, 18 Apr 2024 16:38:42 +0100 Subject: [PATCH] #2470: implement PR suggestions --- docs/source/configuration/io_settings.rst | 47 ++++++++++--------- src/primaite/session/io.py | 8 ++-- src/primaite/simulator/__init__.py | 18 ++++--- src/primaite/simulator/system/core/sys_log.py | 10 ++-- .../configs/basic_switched_network.yaml | 2 +- .../test_io_settings.py | 2 +- .../_simulator/_system/core/test_sys_log.py | 43 ++++------------- 7 files changed, 53 insertions(+), 77 deletions(-) diff --git a/docs/source/configuration/io_settings.rst b/docs/source/configuration/io_settings.rst index 085074a1..c3bcdf7b 100644 --- a/docs/source/configuration/io_settings.rst +++ b/docs/source/configuration/io_settings.rst @@ -21,29 +21,6 @@ This section configures how PrimAITE saves data during simulation and training. log_level: INFO -``log_level`` -------------- - -Optional. Default value is ``INFO``. - -The level of logging that should be visible in the sys logs or the logs output to the terminal. - -Available options are: - -- ``OFF``: No logs -- ``DEBUG``: Debug level items and the items below -- ``INFO``: Info level items and the items below -- ``WARNING``: Warning level items and the items below -- ``ERROR``: Error level items and the items below -- ``CRITICAL``: Only critical level logs - -See also |logging_levels| - -.. |logging_levels| raw:: html - - Python logging levels - - ``save_logs`` ------------- @@ -79,3 +56,27 @@ If ``True``, then the pcap files which contain all network traffic during the si Optional. Default value is ``False``. If ``True``, then the log files which contain all node actions during the simulation will be saved. + + +``sys_log_level`` +------------- + +Optional. Default value is ``INFO``. + +The level of logging that should be visible in the sys logs or the logs output to the terminal. + +``save_sys_logs`` has to be set to ``True`` for this setting to be used. + +Available options are: + +- ``DEBUG``: Debug level items and the items below +- ``INFO``: Info level items and the items below +- ``WARNING``: Warning level items and the items below +- ``ERROR``: Error level items and the items below +- ``CRITICAL``: Only critical level logs + +See also |logging_levels| + +.. |logging_levels| raw:: html + + Python logging levels diff --git a/src/primaite/session/io.py b/src/primaite/session/io.py index ffc07e4e..6aff6f9f 100644 --- a/src/primaite/session/io.py +++ b/src/primaite/session/io.py @@ -35,7 +35,7 @@ class PrimaiteIO: """Whether to save system logs.""" write_sys_log_to_terminal: bool = False """Whether to write the sys log to the terminal.""" - log_level: LogLevel = LogLevel.INFO + sys_log_level: LogLevel = LogLevel.INFO """The level of log that should be included in the logfiles/logged into terminal.""" def __init__(self, settings: Optional[Settings] = None) -> None: @@ -52,7 +52,7 @@ class PrimaiteIO: SIM_OUTPUT.save_pcap_logs = self.settings.save_pcap_logs SIM_OUTPUT.save_sys_logs = self.settings.save_sys_logs SIM_OUTPUT.write_sys_log_to_terminal = self.settings.write_sys_log_to_terminal - SIM_OUTPUT.log_level = self.settings.log_level + SIM_OUTPUT.sys_log_level = self.settings.sys_log_level def generate_session_path(self, timestamp: Optional[datetime] = None) -> Path: """Create a folder for the session and return the path to it.""" @@ -100,8 +100,8 @@ class PrimaiteIO: """Create an instance of PrimaiteIO based on a configuration dict.""" config = config or {} - if config.get("log_level"): - config["log_level"] = LogLevel[config["log_level"].upper()] # convert to enum + if config.get("sys_log_level"): + config["sys_log_level"] = LogLevel[config["sys_log_level"].upper()] # convert to enum new = cls(settings=cls.Settings(**config)) diff --git a/src/primaite/simulator/__init__.py b/src/primaite/simulator/__init__.py index cfae2ab7..ddb098c6 100644 --- a/src/primaite/simulator/__init__.py +++ b/src/primaite/simulator/__init__.py @@ -1,6 +1,6 @@ """Warning: SIM_OUTPUT is a mutable global variable for the simulation output directory.""" from datetime import datetime -from enum import Enum +from enum import IntEnum from pathlib import Path from primaite import _PRIMAITE_ROOT @@ -8,20 +8,18 @@ from primaite import _PRIMAITE_ROOT __all__ = ["SIM_OUTPUT"] -class LogLevel(Enum): +class LogLevel(IntEnum): """Enum containing all the available log levels for PrimAITE simulation output.""" - OFF = 999 - """No logs will be output to terminal or log file.""" - DEBUG = 1 + DEBUG = 10 """Debug items will be output to terminal or log file.""" - INFO = 2 + INFO = 20 """Info items will be output to terminal or log file.""" - WARNING = 3 + WARNING = 30 """Warnings will be output to terminal or log file.""" - ERROR = 4 + ERROR = 40 """Errors will be output to terminal or log file.""" - CRITICAL = 5 + CRITICAL = 50 """Critical errors will be output to terminal or log file.""" @@ -33,7 +31,7 @@ class _SimOutput: self.save_pcap_logs: bool = False self.save_sys_logs: bool = False self.write_sys_log_to_terminal: bool = False - self.log_level: LogLevel = LogLevel.INFO # default log level is at INFO + self.sys_log_level: LogLevel = LogLevel.INFO # default log level is at INFO @property def path(self) -> Path: diff --git a/src/primaite/simulator/system/core/sys_log.py b/src/primaite/simulator/system/core/sys_log.py index 775f9b30..225bd4d8 100644 --- a/src/primaite/simulator/system/core/sys_log.py +++ b/src/primaite/simulator/system/core/sys_log.py @@ -99,7 +99,7 @@ class SysLog: :param msg: The message to be logged. :param to_terminal: If True, prints to the terminal too. """ - if SIM_OUTPUT.log_level.value > LogLevel.DEBUG.value: + if SIM_OUTPUT.sys_log_level > LogLevel.DEBUG: return if SIM_OUTPUT.save_sys_logs: @@ -113,7 +113,7 @@ class SysLog: :param msg: The message to be logged. :param to_terminal: If True, prints to the terminal too. """ - if SIM_OUTPUT.log_level.value > LogLevel.INFO.value: + if SIM_OUTPUT.sys_log_level > LogLevel.INFO: return if SIM_OUTPUT.save_sys_logs: @@ -127,7 +127,7 @@ class SysLog: :param msg: The message to be logged. :param to_terminal: If True, prints to the terminal too. """ - if SIM_OUTPUT.log_level.value > LogLevel.WARNING.value: + if SIM_OUTPUT.sys_log_level > LogLevel.WARNING: return if SIM_OUTPUT.save_sys_logs: @@ -141,7 +141,7 @@ class SysLog: :param msg: The message to be logged. :param to_terminal: If True, prints to the terminal too. """ - if SIM_OUTPUT.log_level.value > LogLevel.ERROR.value: + if SIM_OUTPUT.sys_log_level > LogLevel.ERROR: return if SIM_OUTPUT.save_sys_logs: @@ -155,7 +155,7 @@ class SysLog: :param msg: The message to be logged. :param to_terminal: If True, prints to the terminal too. """ - if LogLevel.CRITICAL.value < SIM_OUTPUT.log_level.value: + if LogLevel.CRITICAL < SIM_OUTPUT.sys_log_level: return if SIM_OUTPUT.save_sys_logs: diff --git a/tests/assets/configs/basic_switched_network.yaml b/tests/assets/configs/basic_switched_network.yaml index 4e45b008..aaddebd0 100644 --- a/tests/assets/configs/basic_switched_network.yaml +++ b/tests/assets/configs/basic_switched_network.yaml @@ -8,7 +8,7 @@ io_settings: save_step_metadata: false save_pcap_logs: true save_sys_logs: true - log_level: WARNING + sys_log_level: WARNING game: diff --git a/tests/integration_tests/configuration_file_parsing/test_io_settings.py b/tests/integration_tests/configuration_file_parsing/test_io_settings.py index 83df31ff..e66350cf 100644 --- a/tests/integration_tests/configuration_file_parsing/test_io_settings.py +++ b/tests/integration_tests/configuration_file_parsing/test_io_settings.py @@ -28,7 +28,7 @@ def test_io_settings(): assert env.io.settings is not None - assert env.io.settings.log_level is LogLevel.WARNING + assert env.io.settings.sys_log_level is LogLevel.WARNING assert env.io.settings.save_pcap_logs assert env.io.settings.save_sys_logs assert env.io.settings.save_step_metadata is False diff --git a/tests/unit_tests/_primaite/_simulator/_system/core/test_sys_log.py b/tests/unit_tests/_primaite/_simulator/_system/core/test_sys_log.py index 610aad1c..56b58d71 100644 --- a/tests/unit_tests/_primaite/_simulator/_system/core/test_sys_log.py +++ b/tests/unit_tests/_primaite/_simulator/_system/core/test_sys_log.py @@ -11,32 +11,9 @@ def syslog() -> SysLog: return SysLog(hostname="test") -def test_off_log_level(syslog, capsys): +def test_debug_sys_log_level(syslog, capsys): """Test that the debug log level logs debug syslogs and above.""" - SIM_OUTPUT.log_level = LogLevel.OFF - SIM_OUTPUT.write_sys_log_to_terminal = True - - test_string = str(uuid4()) - - syslog.debug(test_string) - syslog.info(test_string) - syslog.warning(test_string) - syslog.error(test_string) - syslog.critical(test_string) - - captured = "".join(capsys.readouterr()) - - assert test_string not in captured - assert "DEBUG" not in captured - assert "INFO" not in captured - assert "WARNING" not in captured - assert "ERROR" not in captured - assert "CRITICAL" not in captured - - -def test_debug_log_level(syslog, capsys): - """Test that the debug log level logs debug syslogs and above.""" - SIM_OUTPUT.log_level = LogLevel.DEBUG + SIM_OUTPUT.sys_log_level = LogLevel.DEBUG SIM_OUTPUT.write_sys_log_to_terminal = True test_string = str(uuid4()) @@ -57,9 +34,9 @@ def test_debug_log_level(syslog, capsys): assert "CRITICAL" in captured -def test_info_log_level(syslog, capsys): +def test_info_sys_log_level(syslog, capsys): """Test that the debug log level logs debug syslogs and above.""" - SIM_OUTPUT.log_level = LogLevel.INFO + SIM_OUTPUT.sys_log_level = LogLevel.INFO SIM_OUTPUT.write_sys_log_to_terminal = True test_string = str(uuid4()) @@ -80,9 +57,9 @@ def test_info_log_level(syslog, capsys): assert "CRITICAL" in captured -def test_warning_log_level(syslog, capsys): +def test_warning_sys_log_level(syslog, capsys): """Test that the debug log level logs debug syslogs and above.""" - SIM_OUTPUT.log_level = LogLevel.WARNING + SIM_OUTPUT.sys_log_level = LogLevel.WARNING SIM_OUTPUT.write_sys_log_to_terminal = True test_string = str(uuid4()) @@ -103,9 +80,9 @@ def test_warning_log_level(syslog, capsys): assert "CRITICAL" in captured -def test_error_log_level(syslog, capsys): +def test_error_sys_log_level(syslog, capsys): """Test that the debug log level logs debug syslogs and above.""" - SIM_OUTPUT.log_level = LogLevel.ERROR + SIM_OUTPUT.sys_log_level = LogLevel.ERROR SIM_OUTPUT.write_sys_log_to_terminal = True test_string = str(uuid4()) @@ -126,9 +103,9 @@ def test_error_log_level(syslog, capsys): assert "CRITICAL" in captured -def test_critical_log_level(syslog, capsys): +def test_critical_sys_log_level(syslog, capsys): """Test that the debug log level logs debug syslogs and above.""" - SIM_OUTPUT.log_level = LogLevel.CRITICAL + SIM_OUTPUT.sys_log_level = LogLevel.CRITICAL SIM_OUTPUT.write_sys_log_to_terminal = True test_string = str(uuid4())