Skip to content

Commit

Permalink
Implement JSON logging with --log-format=json
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Oct 19, 2023
1 parent 142b327 commit f9c8b9b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies = [
"isort~=5.12.0",
"libcst~=1.1.0",
"pylint~=3.0.0",
"python-json-logger~=2.0.0",
"PyYAML~=6.0.0",
"semgrep~=1.45.0",
"wrapt~=1.15.0",
Expand Down
2 changes: 1 addition & 1 deletion src/codemodder/codemodder.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def run(original_args) -> int:
)
return 1

configure_logger(argv.verbose)
configure_logger(argv.verbose, argv.log_format)

log_section("startup")
logger.info("codemodder: python/%s", __VERSION__)
Expand Down
32 changes: 26 additions & 6 deletions src/codemodder/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
import sys

from pythonjsonlogger import jsonlogger

logger = logging.getLogger("codemodder")


Expand All @@ -18,6 +20,16 @@ def __str__(self):
return self.value.lower()


class CodemodderJsonFormatter(jsonlogger.JsonFormatter):
def add_fields(self, log_record, record, message_dict):
super().add_fields(log_record, record, message_dict)
log_record["timestamp"] = log_record.pop("asctime")
log_record.move_to_end("timestamp", last=False)
log_record["level"] = record.levelname.upper()
log_record["file"] = record.filename
log_record["line"] = record.lineno


def log_section(section_name: str):
"""
Log a section header.
Expand All @@ -34,22 +46,30 @@ def log_list(level: int, header: str, items: list, predicate=None):
logger.log(level, " - %s", predicate(item) if predicate else item)


def configure_logger(verbose: bool):
def configure_logger(verbose: bool, log_format: OutputFormat):
"""
Configure the logger based on the verbosity level.
"""
log_level = logging.DEBUG if verbose else logging.INFO

# TODO: this should all be conditional on the output format
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(log_level)
stdout_handler.addFilter(lambda record: record.levelno <= logging.WARNING)
handlers = [stdout_handler]

stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(logging.ERROR)
match log_format:
case OutputFormat.HUMAN:
stdout_handler.addFilter(lambda record: record.levelno <= logging.WARNING)
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(logging.ERROR)
handlers.append(stderr_handler)
case OutputFormat.JSON:
formatter = CodemodderJsonFormatter(
"%(asctime) %(level) %(message) %(file) %(line)"
)
stdout_handler.setFormatter(formatter)

logging.basicConfig(
format="%(message)s",
level=log_level,
handlers=[stdout_handler, stderr_handler],
handlers=handlers,
)

0 comments on commit f9c8b9b

Please sign in to comment.