Skip to content

Commit

Permalink
Add optional project_name field to JSON log output
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Oct 18, 2023
1 parent 04932c8 commit b5c53e8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/codemodder/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def parse_args(argv, codemod_registry):
choices=[OutputFormat.HUMAN, OutputFormat.JSON],
help="the format for the log output",
)
parser.add_argument(
"--project-name",
help="optional descriptive name for the project used in log output",
)
parser.add_argument(
"--path-exclude",
action=CsvListAction,
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, argv.log_format)
configure_logger(argv.verbose, argv.log_format, argv.project_name)

log_section("startup")
logger.info("codemodder: python/%s", __VERSION__)
Expand Down
16 changes: 14 additions & 2 deletions src/codemodder/logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from enum import Enum
import logging
import sys
from typing import Optional

from pythonjsonlogger import jsonlogger

Expand All @@ -21,13 +22,21 @@ def __str__(self):


class CodemodderJsonFormatter(jsonlogger.JsonFormatter):
project_name: Optional[str]

def __init__(self, *args, project_name: Optional[str] = None, **kwargs):
self.project_name = project_name
super().__init__(*args, **kwargs)

Check warning on line 29 in src/codemodder/logging.py

View check run for this annotation

Codecov / codecov/patch

src/codemodder/logging.py#L28-L29

Added lines #L28 - L29 were not covered by tests

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.lower()
log_record["file"] = record.filename
log_record["line"] = record.lineno
if self.project_name:
log_record["project-name"] = self.project_name

Check warning on line 39 in src/codemodder/logging.py

View check run for this annotation

Codecov / codecov/patch

src/codemodder/logging.py#L32-L39

Added lines #L32 - L39 were not covered by tests


def log_section(section_name: str):
Expand All @@ -46,7 +55,9 @@ 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, log_format: OutputFormat):
def configure_logger(
verbose: bool, log_format: OutputFormat, project_name: Optional[str] = None
):
"""
Configure the logger based on the verbosity level.
"""
Expand All @@ -64,7 +75,8 @@ def configure_logger(verbose: bool, log_format: OutputFormat):
handlers.append(stderr_handler)
case OutputFormat.JSON:
formatter = CodemodderJsonFormatter(

Check warning on line 77 in src/codemodder/logging.py

View check run for this annotation

Codecov / codecov/patch

src/codemodder/logging.py#L76-L77

Added lines #L76 - L77 were not covered by tests
"%(asctime) %(level) %(message) %(file) %(line)"
"%(asctime) %(level) %(message) %(file) %(line)",
project_name=project_name,
)
stdout_handler.setFormatter(formatter)

Check warning on line 81 in src/codemodder/logging.py

View check run for this annotation

Codecov / codecov/patch

src/codemodder/logging.py#L81

Added line #L81 was not covered by tests

Expand Down

0 comments on commit b5c53e8

Please sign in to comment.