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.
15 from logging import Filter, LogRecord
16 from warnings import warn
17 from typing import List, Optional, Union
19 from onaplogging.utils.system import is_above_python_2_7
21 from .marker import match_markers, Marker
24 class MarkerFilter(Filter):
30 marker_to_match (Marker/list): a marker of list of markers.
32 filter: Filter records by the current filter marker(s).
36 def markers_to_match(self):
37 # type: () -> Union[Marker, List[Marker]]
38 return self.markersToMatch # TODO renamed - deprecated
40 @markers_to_match.setter
41 def markers_to_match(self, value):
42 # type: ( Union[Marker, List[Marker]] ) -> None
43 self.markersToMatch = value
47 markers=None): # type: Optional[Union[Marker, List[Marker]]]
49 if is_above_python_2_7():
50 super(MarkerFilter, self).__init__(name)
53 Filter.__init__(self, name)
55 warn("markersToMatch attribute will be replaced by a property. "
56 "Use markers_to_match property instead.", DeprecationWarning)
57 self.markers_to_match = markers
59 def filter(self, record):
60 # type: (LogRecord) -> bool
61 """Filter by looking for a marker match.
64 record: A record to match with the filter(s).
66 bool: Whether the record matched with the filter(s)
68 return match_markers(record, self.markers_to_match)