12b5488aa0a4a974f5f3d1ea168ad1b2081aa45a
[logging-analytics.git] / pylog / onaplogging / marker / markerHandler.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 import sys
16 from logging.handlers import SMTPHandler
17 from .marker import matchMarkerHelp
18
19
20 class MarkerNotifyHandler(SMTPHandler):
21
22     def __init__(self, mailhost, fromaddr, toaddrs, subject,
23                  credentials=None, secure=None, timeout=5.0, markers=None):
24
25         if sys.version_info > (3, 2):
26             super(MarkerNotifyHandler, self).__init__(
27                 mailhost, fromaddr, toaddrs, subject,
28                 credentials, secure, timeout)
29         elif sys.version_info > (2, 7):
30             super(MarkerNotifyHandler, self).__init__(
31                 mailhost, fromaddr, toaddrs, subject,
32                 credentials, secure)
33         else:
34             SMTPHandler.__init__(self,
35                                  mailhost, fromaddr, toaddrs, subject,
36                                  credentials, secure)
37
38         self.markers = markers
39
40     def handle(self, record):
41
42         if self.markers is None:
43             return False
44
45         if matchMarkerHelp(record, self.markers):
46             if sys.version_info > (2, 7):
47                 return super(SMTPHandler, self).handle(record)
48             else:
49                 return SMTPHandler.handle(self, record)
50
51         return False