#951 - Can not view and change the log level from the cli.

- Fixed write transaction issue in transactions_to_file.py
This commit is contained in:
Chris McCarthy
2023-06-09 22:23:45 +01:00
parent 29d1566789
commit 3c8a8188fb
4 changed files with 53 additions and 15 deletions

View File

@@ -14,6 +14,7 @@ from platformdirs import PlatformDirs
_PLATFORM_DIRS: Final[PlatformDirs] = PlatformDirs(appname="primaite")
"""An instance of `PlatformDirs` set with appname='primaite'."""
def _get_primaite_config():
config_path = _PLATFORM_DIRS.user_config_path / "primaite_config.yaml"
if not config_path.exists():
@@ -24,16 +25,20 @@ def _get_primaite_config():
)
with open(config_path, "r") as file:
primaite_config = yaml.safe_load(file)
log_level_map = {
"NOTSET": logging.NOTSET,
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARN": logging.WARN,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL
}
primaite_config["log_level"] = log_level_map[primaite_config["log_level"]]
return primaite_config
_PRIMAITE_CONFIG = _get_primaite_config()
# PrimAITE config items
_LOG_LEVEL: int = _PRIMAITE_CONFIG["log_level"]
_LOGGER_FORMAT: str = _PRIMAITE_CONFIG["logger_format"]
_USER_DIRS: Final[Path] = Path.home() / "primaite"
"""The users home space for PrimAITE which is located at: ~/primaite."""
@@ -86,10 +91,10 @@ _FILE_HANDLER: Final[RotatingFileHandler] = RotatingFileHandler(
backupCount=9, # Max 100MB of logs
encoding="utf8",
)
_STREAM_HANDLER.setLevel(_LOG_LEVEL)
_FILE_HANDLER.setLevel(_LOG_LEVEL)
_STREAM_HANDLER.setLevel(_PRIMAITE_CONFIG["log_level"])
_FILE_HANDLER.setLevel(_PRIMAITE_CONFIG["log_level"])
_LOG_FORMAT_STR: Final[str] = _LOGGER_FORMAT
_LOG_FORMAT_STR: Final[str] = _PRIMAITE_CONFIG["logger_format"]
_STREAM_HANDLER.setFormatter(logging.Formatter(_LOG_FORMAT_STR))
_FILE_HANDLER.setFormatter(logging.Formatter(_LOG_FORMAT_STR))
@@ -108,7 +113,7 @@ def getLogger(name: str) -> Logger:
logging config.
"""
logger = logging.getLogger(name)
logger.setLevel(_LOG_LEVEL)
logger.setLevel(_PRIMAITE_CONFIG["log_level"])
return logger

View File

@@ -1,11 +1,15 @@
# Crown Copyright (C) Dstl 2022. DEFCON 703. Shared in confidence.
"""Provides a CLI using Typer as an entry point."""
import logging
import os
import shutil
from pathlib import Path
from enum import Enum
from typing import Optional
import pkg_resources
import typer
import yaml
from platformdirs import PlatformDirs
from typing_extensions import Annotated
@@ -49,6 +53,36 @@ def logs(last_n: Annotated[int, typer.Option("-n")]):
print(re.sub(r"\n*", "", line))
_LogLevel = Enum("LogLevel", {k: k for k in logging._levelToName.values()}) # noqa
@app.command()
def log_level(level: Annotated[Optional[_LogLevel], typer.Argument()] = None):
"""
View or set the PrimAITE Log Level.
To View, simply call: primaite log-level
To set, call: primaite log-level <desired log level>
For example, to set the to debug, call: primaite log-level DEBUG
"""
app_dirs = PlatformDirs(appname="primaite")
app_dirs.user_config_path.mkdir(exist_ok=True, parents=True)
user_config_path = app_dirs.user_config_path / "primaite_config.yaml"
if user_config_path.exists():
with open(user_config_path, "r") as file:
primaite_config = yaml.safe_load(file)
if level:
primaite_config["log_level"] = level.value
with open(user_config_path, "w") as file:
yaml.dump(primaite_config, file)
else:
level = primaite_config["log_level"]
print(f"PrimAITE Log Level: {level}")
@app.command()
def notebooks():
"""Start Jupyter Lab in the users PrimAITE notebooks directory."""

View File

@@ -1,5 +1,5 @@
# The main PrimAITE application config file
# Logging
log_level: 'INFO'
log_level: INFO
logger_format: '%(asctime)s::%(levelname)s::%(name)s::%(lineno)s::%(message)s'

View File

@@ -16,11 +16,10 @@ def turn_action_space_to_array(_action_space):
Args:
_action_space: The action space.
"""
return_array = []
for x in range(len(_action_space)):
return_array.append(str(_action_space[x]))
return return_array
if isinstance(_action_space, list):
return [str(i) for i in _action_space]
else:
return [str(_action_space)]
def turn_obs_space_to_array(_obs_space, _obs_assets, _obs_features):