-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
93 lines (72 loc) · 2.29 KB
/
logger.py
File metadata and controls
93 lines (72 loc) · 2.29 KB
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
import logging
from logging.handlers import SMTPHandler, RotatingFileHandler
from patterns import Singleton
from util import systemlog
from constants.general import APPLICATION_NAME
class Logger(Singleton):
def configure(self, cfg):
"""Configures the Logger module"""
self.configured = False
self.log = logging.getLogger(APPLICATION_NAME)
formatter = logging.Formatter("%(asctime)s [%(levelname)s]: %(message)s")
# File logger
try:
fileLogLevel = cfg.fileLogLevel.lower()
if not fileLogLevel == "none":
try:
fileHandler = RotatingFileHandler(cfg.logFile, 'a', cfg.logMaxLenght)
except:
fileHandler = RotatingFileHandler(cfg.failsafeLogFile, 'a', cfg.logMaxLenght)
fileHandler.setFormatter(formatter)
fileHandler.setLevel(logLevel(fileLogLevel))
self.log.addHandler(fileHandler)
except Exception:
systemlog("File logger configuration failed")
# SMTP logger
try:
smtpLogLevel = cfg.smtpLogLevel.lower()
if not smtpLogLevel == "none":
subject_str = APPLICATION_NAME + ' error log'
smtpHandler = SMTPHandler(cfg.smtpHost, cfg.emailSender, cfg.emailReceivers, subject_str)
smtpHandler.setFormatter(formatter)
smtpHandler.setLevel(logLevel(smtpLogLevel))
self.log.addHandler(smtpHandler)
except Exception:
systemlog("SMTP Logger configuration failed")
self.log.setLevel(logging.DEBUG)
self.configured = True
self.info("Logger configured")
def debug(self, text):
"""Log a debug message"""
if self.configured:
self.log.debug(text)
def info(self, text):
"""Log an info message"""
if self.configured:
self.log.info(text)
def warning(self, text):
"""Log a warning message"""
if self.configured:
self.log.warning(text)
def error(self, text):
"""Log an error message"""
if self.configured:
self.log.error(text)
def critical(self, text):
"""Log a critical message"""
if self.configured:
self.log.critical(text)
def logLevel(level_str):
level = logging.NOTSET
level_str = level_str.lower()
if level_str == "debug":
level = logging.DEBUG
elif level_str == "info":
level = logging.INFO
elif level_str == "warning":
level = logging.WARNING
elif level_str == "error":
level = logging.ERROR
elif level_str == "critical":
level = logging.CRITICAL
return level