Skip to content

Commit

Permalink
No new lines in Python's default logging module (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Aug 5, 2024
1 parent 1a5574b commit 97c4a09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion prediction_market_agent_tooling/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class LogConfig(BaseSettings):
LOG_LEVEL: LogLevel = LogLevel.DEBUG


class NoNewLineStreamHandler(logging.StreamHandler): # type: ignore # StreamHandler is not typed in the standard library.
def format(self, record: logging.LogRecord) -> str:
return super().format(record).replace("\n", " ")


GCP_LOG_LOGURU_FORMAT = (
"{level:<.1}{time:MMDD HH:mm:ss} {process} {name}:{line}] {message}"
)
Expand All @@ -40,25 +45,34 @@ class LogConfig(BaseSettings):


def patch_logger() -> None:
"""
Function to patch loggers according to the deployed environment.
Patches Loguru's logger, Python's default logger, warnings library and also monkey-patch print function as many libraries just use it.
"""
config = LogConfig()

if config.LOG_FORMAT == LogFormat.GCP:
format_loguru = GCP_LOG_LOGURU_FORMAT
format_logging = GCP_LOG_LOGGING_FORMAT
datefmt_logging = GCP_LOG_FORMAT_LOGGING_DATEFMT
print_logging = print_using_loguru_info
handlers = [NoNewLineStreamHandler()]

elif config.LOG_FORMAT == LogFormat.DEFAULT:
format_loguru, format_logging, datefmt_logging = None, None, None
print_logging = None
handlers = None

else:
raise ValueError(f"Unknown log format: {config.LOG_FORMAT}")

# Change built-in logging.
if format_logging is not None:
logging.basicConfig(
level=config.LOG_LEVEL.value, format=format_logging, datefmt=datefmt_logging
level=config.LOG_LEVEL.value,
format=format_logging,
datefmt=datefmt_logging,
handlers=handlers,
)

# Change loguru.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "prediction-market-agent-tooling"
version = "0.45.0"
version = "0.45.1"
description = "Tools to benchmark, deploy and monitor prediction market agents."
authors = ["Gnosis"]
readme = "README.md"
Expand Down

0 comments on commit 97c4a09

Please sign in to comment.