Skip to content
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
23 changes: 18 additions & 5 deletions packages/testing/src/execution_testing/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,27 @@ def get_logger(name: str) -> EESTLogger:

class UTCFormatter(logging.Formatter):
"""
Log formatter that formats UTC timestamps with milliseconds and +00:00
suffix.
Log formatter that formats UTC timestamps without milliseconds.
"""

def formatTime(self, record: LogRecord, datefmt: str | None = None) -> str: # noqa: D102,N802
# camelcase required
del datefmt

dt = datetime.fromtimestamp(record.created, tz=timezone.utc)
return dt.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + "+00:00"
return dt.strftime("%Y-%m-%d %H:%M:%S")

def format(self, record: LogRecord) -> str:
"""Format with relative pathname from current working directory."""
# Make pathname relative to cwd
try:
record.pathname = "./" + str(
Path(record.pathname).relative_to(Path.cwd())
) # noqa: E501
except ValueError:
# If path is not relative to cwd, keep as is
pass
return super().format(record)


class ColorFormatter(UTCFormatter):
Expand Down Expand Up @@ -150,7 +161,7 @@ def from_cli(cls, value: str) -> int:

valid = ", ".join(logging._nameToLevel.keys())
raise ValueError(
f"Invalid log level '{value}'. Expected one of: {valid} or a number."
f"Invalid log level '{value}'. Expected: {valid} or a number."
)


Expand All @@ -163,7 +174,9 @@ def configure_logging(
log_level: int | str = "INFO",
log_file: Optional[str | Path] = None,
log_to_stdout: bool = True,
log_format: str = "%(asctime)s [%(levelname)s] %(name)s: %(message)s",
log_format: str = (
"%(asctime)s [%(levelname)s] %(pathname)s:%(lineno)d: %(message)s"
),
use_color: Optional[bool] = None,
) -> Optional[logging.FileHandler]:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import io
import logging
import re
import tempfile
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -103,9 +102,12 @@ def test_utc_formatter(self) -> None:
)

formatted = formatter.format(record)
assert re.match(
r"2021-01-01 00:00:00\.\d{3}\+00:00: Test message", formatted
)

# logs contain
# timestamp
assert "2021-01-01 00:00:00" in formatted
# message
assert "Test message" in formatted

def test_color_formatter(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that ColorFormatter adds color codes to the log level."""
Expand Down
Loading