/
__init__.py
62 lines (48 loc) · 2.19 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
""" Module that contains the CfLogger class """
import logging
import sys
from sap.cf_logging import defaults
from sap.cf_logging.core.constants import REQUEST_KEY, RESPONSE_KEY
from sap.cf_logging.core.framework import Framework
from sap.cf_logging.formatters.json_formatter import JsonFormatter
from sap.cf_logging.job_logging.framework import JobFramework
from sap.cf_logging.record.request_log_record import RequestWebRecord
from sap.cf_logging.record.simple_log_record import SimpleLogRecord
__version__ = '4.1.1'
_SETUP_DONE = False
FRAMEWORK = None
class CfLogger(logging.Logger):
""" CfLogger class inherits from logging.Logger and makes custom
log messages
"""
# pylint: disable=too-many-arguments,arguments-differ,keyword-arg-before-vararg
def makeRecord(self, name, level, fn, lno, msg, msgargs, exc_info,
func=None, extra=None, *args, **kwargs):
""" Returns SimpleLogMessage or a RequestWebRecord depending on the extra variable """
# check what record type this is
cls = None
if extra is not None and REQUEST_KEY in extra and RESPONSE_KEY in extra:
cls = RequestWebRecord
else:
cls = SimpleLogRecord
return cls(extra, FRAMEWORK, name, level, fn, lno, msg, msgargs, exc_info,
func, *args, **kwargs)
def init(cfl_framework=None, level=defaults.DEFAULT_LOGGING_LEVEL):
""" Initialize function. It sets up the logging library to output JSON
formatted messages.
Optional arguments framework to use and logging.level
"""
global FRAMEWORK # pylint: disable=global-statement
global _SETUP_DONE # pylint: disable=global-statement
if _SETUP_DONE:
raise RuntimeError('cf_logging already initialized')
if cfl_framework is not None and not isinstance(cfl_framework, Framework):
raise TypeError('expecting framework of type {}'.format(Framework.__name__))
_SETUP_DONE = True
FRAMEWORK = cfl_framework or JobFramework()
logging.setLoggerClass(CfLogger)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JsonFormatter())
root = logging.getLogger()
root.setLevel(level)
root.addHandler(handler)