aboutsummaryrefslogtreecommitdiffstats
path: root/osdf/logging/oof_mdc_context.py
blob: 9c9b52c316d787440368132c85689821b5435887 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -------------------------------------------------------------------------
#   Copyright (c) 2020 AT&T Intellectual Property
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# -------------------------------------------------------------------------
#

import logging
import re
import sys

from onaplogging.marker import Marker
from onaplogging.marker import MARKER_TAG
from onaplogging.mdcContext import _replace_func_name
from onaplogging.mdcContext import fetchkeys
from onaplogging.mdcContext import findCaller as fc
from onaplogging.mdcContext import MDC

from osdf.utils.mdc_utils import set_error_details


def findCaller(self, stack_info=False, stacklevel=1):
    """replacing onaplogging.mdcContext with this method to work with py3.8

    """
    return fc(stack_info)


def mdc_mapper():
    """Convert the MDC dict into comma separated, name=value string

    :return: string format
    """
    return ','.join(f'{k}={v}' for (k, v) in MDC.result().items() if k not in ['customField2'])


@fetchkeys
def info(self, msg, *args, **kwargs):
    """Wrapper method for log.info is called

    """
    if self.isEnabledFor(logging.INFO):
        MDC.put('customField2', mdc_mapper())
        self._log(logging.INFO, no_sep(msg), args, **kwargs)


@fetchkeys
def debug(self, msg, *args, **kwargs):
    """Wrapper method for log.debug is called

    msg: log message
    args: logging args
    kwargs: all the optional args
    """
    if self.isEnabledFor(logging.DEBUG):
        self._log(logging.DEBUG, no_sep(msg), args, **kwargs)


@fetchkeys
def warning(self, msg, *args, **kwargs):
    """Wrapper method for log.warning is called

    msg: log message
    args: logging args
    kwargs: all the optional args
    """
    if self.isEnabledFor(logging.WARNING):
        self._log(logging.WARNING, no_sep(msg), args, **kwargs)


@fetchkeys
def exception(self, msg, *args, **kwargs):
    """Wrapper method for log.exception is called

    msg: log message
    args: logging args
    kwargs: all the optional args
    """
    kwargs['exc_info'] = 1
    self.error(no_sep(msg), *args, **kwargs)


@fetchkeys
def critical(self, msg, *args, **kwargs):
    """Wrapper method for log.critical

    msg: log message
    args: logging args
    kwargs: all the optional args
    """
    if self.isEnabledFor(logging.CRITICAL):
        self._log(logging.CRITICAL, no_sep(msg), args, **kwargs)


@fetchkeys
def error(self, msg, *args, **kwargs):
    """Wrapper method for log.error is called

    msg: log message
    args: logging args
    kwargs: all the optional args
    """
    if self.isEnabledFor(logging.ERROR):
        if not MDC.get('errorCode'):
            set_error_details(400, 'Internal Error')
        MDC.put('customField2', mdc_mapper())
        self._log(logging.ERROR, no_sep(msg), args, **kwargs)


@fetchkeys
def log(self, level, msg, *args, **kwargs):
    """Wrapper method for log.log is called

    msg: log message
    args: logging args
    kwargs: all the optional args
    """
    if not isinstance(level, int):
        if logging.raiseExceptions:
            raise TypeError("level must be an integer")
        else:
            return

    if self.isEnabledFor(level):
        self._log(level, no_sep(msg), args, **kwargs)


def handle(self, record):
    """Wrapper method for log.handle is called

    """
    c_marker = getattr(self, MARKER_TAG, None)

    if isinstance(c_marker, Marker):
        setattr(record, MARKER_TAG, c_marker)

    if (not self.disabled) and self.filter(record):
        self.callHandlers(record)


def no_sep(message):
    """This method will remove newline, | from the message

    """
    if message is None:
        return ''
    return re.sub(r'[\|\n]', ' ', str(message))


def patch_logging_mdc():
    """The patch to add MDC ability in logging Record instance at runtime

    """
    local_module = sys.modules[__name__]
    for attr in dir(logging.Logger):
        if attr in _replace_func_name:
            new_func = getattr(local_module, attr, None)
            if new_func:
                setattr(logging.Logger, attr, new_func)