Skip to content

Commit

Permalink
implement logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jiewpeng committed Sep 1, 2022
1 parent ef67576 commit a7e71d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
15 changes: 15 additions & 0 deletions bentoml/_internal/configuration/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ def _is_ip_address(addr: str) -> bool:
},
},
},
"logging": {
"formatting": {
"format": str,
"datefmt": str,
"trace_id_format": str,
"span_id_format": str,
}
},
}
)

Expand Down Expand Up @@ -535,5 +543,12 @@ def duration_buckets(
)
return DEFAULT_BUCKET

@providers.SingletonFactory
@staticmethod
def logging_formats(
cfg: dict[str, t.Any] = Provide[config.logging.formatting],
) -> dict[str, str]:
return cfg


BentoMLContainer = _BentoMLContainerClass()
7 changes: 7 additions & 0 deletions bentoml/_internal/configuration/default_configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ tracing:
otlp:
protocol: Null
url: Null

logging:
formatting:
format: "%(asctime)s %(levelname_bracketed)s %(component)s %(message)s%(trace_msg)s"
datefmt: "%Y-%m-%dT%H:%M:%S%z"
trace_id_format: 032x
span_id_format: 016x
17 changes: 11 additions & 6 deletions bentoml/_internal/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .context import component_context
from .configuration import get_debug_mode
from .configuration import get_quiet_mode
from .configuration.containers import BentoMLContainer

default_factory = logging.getLogRecordFactory()

Expand Down Expand Up @@ -45,10 +46,11 @@ def filter(self, record: logging.LogRecord) -> bool:
"root": {"level": logging.WARNING},
}

TRACED_LOG_FORMAT = (
"%(asctime)s %(levelname_bracketed)s %(component)s %(message)s%(trace_msg)s"
)
DATE_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
LOGGING_FORMATS = BentoMLContainer.logging_formats.get()
TRACED_LOG_FORMAT = LOGGING_FORMATS["format"]
DATE_FORMAT = LOGGING_FORMATS["datefmt"]
TRACE_ID_FORMAT = LOGGING_FORMATS["trace_id_format"]
SPAN_ID_FORMAT = LOGGING_FORMATS["span_id_format"]

SERVER_LOGGING_CONFIG: dict[str, t.Any] = {
"version": 1,
Expand Down Expand Up @@ -111,10 +113,13 @@ def trace_record_factory(*args: t.Any, **kwargs: t.Any):
record = default_factory(*args, **kwargs)
record.levelname_bracketed = f"[{record.levelname}]" # type: ignore (adding fields to record)
record.component = f"[{_component_name()}]" # type: ignore (adding fields to record)
if trace_context.trace_id == 0:
trace_id = trace_context.trace_id
if trace_id in (0, None):
record.trace_msg = "" # type: ignore (adding fields to record)
else:
record.trace_msg = f" (trace={trace_context.trace_id},span={trace_context.span_id},sampled={trace_context.sampled})" # type: ignore (adding fields to record)
trace_id = format(trace_id, TRACE_ID_FORMAT)
span_id = format(trace_context.span_id, SPAN_ID_FORMAT)
record.trace_msg = f" (trace={trace_id},span={span_id},sampled={trace_context.sampled})" # type: ignore (adding fields to record)
record.request_id = trace_context.request_id # type: ignore (adding fields to record)

return record
Expand Down

0 comments on commit a7e71d0

Please sign in to comment.