Skip to content

Commit

Permalink
Add MDC to the ButlerLogRecord definition
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Jul 19, 2021
1 parent 948c5eb commit 365f2ce
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion python/lsst/daf/butler/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

import logging
import datetime
from typing import List, Union, Optional, ClassVar, Iterable, Iterator
from collections import defaultdict
from typing import List, Union, Optional, ClassVar, Iterable, Iterator, Dict

from logging import LogRecord, StreamHandler
from pydantic import BaseModel, ValidationError
Expand Down Expand Up @@ -126,6 +127,7 @@ class ButlerLogRecord(BaseModel):
funcName: Optional[str]
process: int
processName: str
MDC: Dict[str, str]

class Config:
allow_mutation = False
Expand All @@ -147,6 +149,14 @@ def from_record(cls, record: LogRecord) -> ButlerLogRecord:

record_dict["message"] = record.getMessage()

# MDC
if hasattr(record, "MDC"):
# mypy does not understand hasattr.
record_dict["MDC"] = dict(record.MDC) # type: ignore
else:
# Use an empty MDC to prevent format failures.
record_dict["MDC"] = defaultdict(str)

# Always use UTC because in distributed systems we can't be sure
# what timezone localtime is and it's easier to compare logs if
# every system is using the same time.
Expand Down

0 comments on commit 365f2ce

Please sign in to comment.