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

Logging #1936

Merged
merged 32 commits into from
Jun 19, 2024
Merged

Logging #1936

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4857bd9
WIP: Simplify handlers
JimMadge Jun 13, 2024
dbe3624
Restore markup
JimMadge Jun 13, 2024
5489f09
Remove output file argument
JimMadge Jun 14, 2024
f446535
Add verbose option
JimMadge Jun 14, 2024
4ef0b38
Add show level option
JimMadge Jun 14, 2024
70c9852
Move prompts
JimMadge Jun 14, 2024
314f599
Move tabulate
JimMadge Jun 14, 2024
038cb43
WIP: log errors on exceptions
JimMadge Jun 14, 2024
8ae968f
Fix circular dependency
JimMadge Jun 14, 2024
063957c
Sort imports
JimMadge Jun 14, 2024
b0cd4b8
Correct type hint
JimMadge Jun 14, 2024
7c06bcc
Adjust log levels and printing
JimMadge Jun 14, 2024
d07fee4
Add log directory
JimMadge Jun 14, 2024
6f2e94e
Use tz aware datetime
JimMadge Jun 17, 2024
3a0fd58
Fix help tests
JimMadge Jun 17, 2024
2b22024
Add version test
JimMadge Jun 17, 2024
2515dc9
Fix diff rendering
JimMadge Jun 17, 2024
895c61c
Change log level of context switch
JimMadge Jun 17, 2024
79c0db3
Fix mocks
JimMadge Jun 17, 2024
7e67ce0
Sort imports
JimMadge Jun 17, 2024
6475e05
Add fixture for tmp logging dir
JimMadge Jun 17, 2024
edcfaaf
Add test for stripping rich formatting
JimMadge Jun 17, 2024
c913587
Restructure logger, don't use singleton
JimMadge Jun 18, 2024
34cd05b
Fix tests
JimMadge Jun 18, 2024
be803ba
Add get_logger test
JimMadge Jun 18, 2024
e2044af
Fix mypy errors
JimMadge Jun 18, 2024
61c17e3
Add set_console_level test
JimMadge Jun 18, 2024
fc55d93
Use package logger instead of root
JimMadge Jun 18, 2024
ec1ac3f
Add show_console_level test
JimMadge Jun 18, 2024
5419f61
Add stdout tests
JimMadge Jun 18, 2024
395e006
Run lint:fmt
JimMadge Jun 18, 2024
b67f993
Improve docstrings and comments
JimMadge Jun 19, 2024
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
Prev Previous commit
Next Next commit
Add set_console_level test
  • Loading branch information
JimMadge committed Jun 18, 2024
commit 61c17e39989bc0b7fc76e56f6dab9c2288ecf333
52 changes: 26 additions & 26 deletions data_safe_haven/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@
from data_safe_haven.directories import log_dir


class PlainFileHandler(logging.FileHandler):
"""
Logging handler that cleans messages before sending them to a log file.
"""

def __init__(self, *args: Any, **kwargs: Any):
"""Constructor"""
super().__init__(*args, **kwargs)

@staticmethod
def strip_formatting(input_string: str) -> str:
"""Strip console markup formatting from a string"""
text = Text.from_markup(input_string)
text.spans = []
return str(text)

def emit(self, record: logging.LogRecord) -> None:
"""Emit a record without formatting"""
record.msg = self.strip_formatting(record.msg)
super().emit(record)


def get_logger() -> logging.Logger:
return logging.getLogger(None)


def init_logging() -> None:
# Configure root handler
logger = get_logger()
Expand Down Expand Up @@ -54,32 +80,6 @@ def logfile_name() -> str:
return f"{datetime.now(UTC).date()}.log"


def get_logger() -> logging.Logger:
return logging.getLogger(None)


class PlainFileHandler(logging.FileHandler):
"""
Logging handler that cleans messages before sending them to a log file.
"""

def __init__(self, *args: Any, **kwargs: Any):
"""Constructor"""
super().__init__(*args, **kwargs)

@staticmethod
def strip_formatting(input_string: str) -> str:
"""Strip console markup formatting from a string"""
text = Text.from_markup(input_string)
text.spans = []
return str(text)

def emit(self, record: logging.LogRecord) -> None:
"""Emit a record without formatting"""
record.msg = self.strip_formatting(record.msg)
super().emit(record)


def set_console_level(level: int | str) -> None:
get_logger().console_handler.setLevel(level) # type: ignore [attr-defined]

Expand Down
10 changes: 9 additions & 1 deletion tests/logging/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from rich.logging import RichHandler

from data_safe_haven.logging.logger import PlainFileHandler, get_logger, logfile_name
from data_safe_haven.logging.logger import PlainFileHandler, get_logger, logfile_name, set_console_level


class TestPlainFileHandler:
Expand Down Expand Up @@ -40,3 +40,11 @@ def test_constructor(self, log_directory):
log_file = Path(logger.file_handler.baseFilename)
logger.info("hello")
assert log_file.is_file()


class TestSetConsoleLevel:
def test_set_console_level(self):
logger = get_logger()
assert logger.console_handler.level == logging.INFO
set_console_level(logging.DEBUG)
assert logger.console_handler.level == logging.DEBUG
Loading