1 # Copyright 2018 ke liang <lokyse@163.com>.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from .marker import MARKER_TAG
18 from .marker import Marker
19 from .colorFormatter import BaseColorFormatter
22 class MarkerFormatter(BaseColorFormatter):
24 def __init__(self, fmt=None, datefmt=None, colorfmt=None, style='%'):
26 if sys.version_info > (3, 2):
27 super(MarkerFormatter, self).__init__(
28 fmt=fmt, datefmt=datefmt, colorfmt=colorfmt, style=style)
29 elif sys.version_info > (2, 7):
30 super(MarkerFormatter, self).__init__(
31 fmt=fmt, datefmt=datefmt, colorfmt=colorfmt)
33 BaseColorFormatter.__init__(self, fmt, datefmt, colorfmt)
35 self._marker_tag = "%(marker)s"
38 self._marker_tag = "{marker}"
39 elif self.style == "$":
40 self._marker_tag = "${marker}"
42 self._tmpFmt = self._fmt
44 def format(self, record):
47 if self._fmt.find(self._marker_tag) != -1 \
48 and hasattr(record, MARKER_TAG):
49 marker = getattr(record, MARKER_TAG)
51 if isinstance(marker, Marker):
52 self._fmt = self._fmt.replace(
53 self._marker_tag, marker.getName())
54 elif self._fmt.find(self._marker_tag) != -1 \
55 and not hasattr(record, MARKER_TAG):
57 self._fmt = self._fmt.replace(self._marker_tag, "")
59 if sys.version_info > (3, 2):
60 self._style = logging._STYLES[self.style][0](self._fmt)
62 if sys.version_info > (2, 7):
63 return super(MarkerFormatter, self).format(record)
65 return BaseColorFormatter.format(self, record)
68 self._fmt = self._tmpFmt