onaplogging: Docstrings, refactor, type hinting
[logging-analytics.git] / pylog / onaplogging / marker / markerFilter.py
1 # Copyright 2018 ke liang <lokyse@163.com>.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from logging import Filter, LogRecord
16 from warnings import warn
17 from typing import List, Optional, Union
18
19 from onaplogging.utils.system import is_above_python_2_7
20
21 from .marker import match_markers, Marker
22
23
24 class MarkerFilter(Filter):
25     """Marker filtering.
26
27     Extends:
28         logging.Filter
29     Properties:
30         marker_to_match (Marker/list): a marker of list of markers.
31     Methods
32         filter: Filter records by the current filter marker(s).
33     """
34
35     @property
36     def markers_to_match(self):
37         # type: () -> Union[Marker, List[Marker]]
38         return self.markersToMatch  # TODO renamed - deprecated
39
40     @markers_to_match.setter
41     def markers_to_match(self, value):
42         # type: ( Union[Marker, List[Marker]] ) -> None
43         self.markersToMatch = value
44
45     def __init__(self,
46                  name="",        # type: str
47                  markers=None):  # type: Optional[Union[Marker, List[Marker]]]
48
49         if is_above_python_2_7():
50             super(MarkerFilter, self).__init__(name)
51
52         else:
53             Filter.__init__(self, name)
54
55         warn("markersToMatch attribute will be replaced by a property. "
56               "Use markers_to_match property instead.", DeprecationWarning)
57         self.markers_to_match = markers
58
59     def filter(self, record):
60         # type: (LogRecord) -> bool
61         """Filter by looking for a marker match.
62
63         Args:
64             record: A record to match with the filter(s).
65         Returns:
66             bool: Whether the record matched with the filter(s)
67         """
68         return match_markers(record, self.markers_to_match)