mirror of
https://github.com/kristoferssolo/bunyan-formatter.git
synced 2025-10-21 17:20:35 +00:00
fix(formatter): time format
This commit is contained in:
parent
63809fca71
commit
8a8aa5064e
@ -1,12 +1,14 @@
|
||||
import json
|
||||
import logging
|
||||
import socket
|
||||
import time
|
||||
from logging import Formatter, LogRecord
|
||||
from pathlib import Path
|
||||
from typing import ClassVar, Optional
|
||||
|
||||
|
||||
class BunyanFormatter(Formatter):
|
||||
LEVEL_MAP = {
|
||||
LEVEL_MAP: ClassVar[dict[int, int]] = {
|
||||
logging.NOTSET: 10,
|
||||
logging.DEBUG: 20,
|
||||
logging.INFO: 30,
|
||||
@ -38,10 +40,19 @@ class BunyanFormatter(Formatter):
|
||||
"levelname": record.levelname,
|
||||
"hostname": hostname,
|
||||
"pid": record.process,
|
||||
"time": self.formatTime(record, "%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||
"time": self.formatTime(record),
|
||||
"target": record.name,
|
||||
"line": record.lineno,
|
||||
"file": str(relative_path),
|
||||
}
|
||||
|
||||
return json.dumps(log_entry)
|
||||
|
||||
def formatTime(self, record: LogRecord, datefmt: Optional[str] = None) -> str: # noqa: N802
|
||||
ct = self.converter(record.created)
|
||||
if datefmt:
|
||||
s = time.strftime(datefmt, ct)
|
||||
else:
|
||||
t = time.strftime("%Y-%m-%dT%H:%M:%S", ct)
|
||||
s = f"{t}.{int(record.msecs):03d}Z"
|
||||
return s
|
||||
|
||||
Loading…
Reference in New Issue
Block a user