18 from typing
import Optional, Literal
19 from logging
import Formatter, LogRecord
23 from .severity
import LogSeverity
24 from .utils
import colorMessage
27 FormatStyle = Literal[
"%",
"{",
"$"]
30 class CTXFormatter(Formatter):
34 fmt: Optional[str] =
None,
35 datefmt: Optional[str] =
None,
36 style: FormatStyle =
"%",
37 validate: bool =
True,
39 jsonOutput: bool =
True
42 super().__init__(fmt, datefmt, style, validate)
45 self.jsonOutput = jsonOutput
47 def format(self, record: LogRecord) -> str:
49 record.levelname = record.levelname.lower().capitalize()
51 severity = LogSeverity.fromLevel(record.levelno)
52 formatted = super().format(record)
55 formatted = colorMessage(severity, formatted)
59 "severity": severity.value,
60 "message": formatted +
"\n"