Skip to content

Commit 47bf154

Browse files
authored
Merge pull request #6919 from chaen/v8.0_FIX_jsonLog
[8.0] Log fixes for stdoutJson and MicrosecondjsonFormatter (MQ logs and stdoutjson)
2 parents 4836a93 + 566ad69 commit 47bf154

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/DIRAC/FrameworkSystem/private/standardLogging/Formatter/MicrosecondJsonFormatter.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import time
2-
from pythonjsonlogger.jsonlogger import JsonFormatter
2+
from pythonjsonlogger.jsonlogger import JsonFormatter, RESERVED_ATTRS
33

44

55
class MicrosecondJsonFormatter(JsonFormatter):
66
"""Standard JSON formater with microsecond precision"""
77

8+
def __init__(self, *args, **kwargs):
9+
"""
10+
Add to the list of attributes we don't want to see
11+
all the DIRAC spefic log formating instructions
12+
"""
13+
if "reserved_attrs" not in kwargs:
14+
kwargs["reserved_attrs"] = RESERVED_ATTRS + (
15+
"spacer",
16+
"headerIsShown",
17+
"timeStampIsShown",
18+
"contextIsShown",
19+
"threadIDIsShown",
20+
"color",
21+
)
22+
super().__init__(*args, **kwargs)
23+
824
def formatTime(self, record, datefmt=None):
925
""":py:meth:`logging.Formatter.formatTime` with microsecond precision by default"""
1026
ct = self.converter(record.created)

src/DIRAC/Resources/LogBackends/StdoutJsonBackend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@
88
from DIRAC.FrameworkSystem.private.standardLogging.Formatter.MicrosecondJsonFormatter import MicrosecondJsonFormatter
99

1010

11+
# These are the standard logging fields that we want to see
12+
# in the json. All the non default are printed anyway
13+
DEFAULT_FMT = "%(levelname)s %(message)s %(asctime)s"
14+
15+
1116
class StdoutJsonBackend(AbstractBackend):
1217
"""
1318
This just spits out the log on stdout in a json format.
1419
"""
1520

1621
def __init__(self, backendParams=None, backendFilters=None):
22+
# The `Format` parameter is passed as `fmt` to MicrosecondJsonFormatter
23+
# which uses it to know which "standard" fields to keep in the
24+
# json output. So we need these
25+
if not backendParams:
26+
backendParams = {}
27+
backendParams.setdefault("Format", DEFAULT_FMT)
28+
1729
super().__init__(logging.StreamHandler, MicrosecondJsonFormatter, backendParams, backendFilters)
1830

1931
def _setHandlerParameters(self, backendParams=None):

0 commit comments

Comments
 (0)