forked from Shippable/cexec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_logger.py
119 lines (94 loc) · 3.97 KB
/
app_logger.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import sys
import time
import os
import logging
import logging.handlers
from message_out import MessageOut
# pylint: disable=too-many-instance-attributes
class AppLogger(object):
logtype = {
'SYSTEM' : 10,
'USER' : 20,
'GLOBAL' : 30
}
loglevel = {
'DEBUG': 10,
'INFO': 20,
'WARN': 30,
'ERROR': 40,
'CRITICAL': 50,
}
def __init__(self, config, module):
self.config = config
self.module = module
self.handlers = None
self.log = None
self.__setup_log(module)
self.user_log_bytes = 0
self.message_out = MessageOut(self.module, self.config)
## flush stdout to avoid out of order logging
sys.stdout.flush()
def debug(self, message, logtype=logtype['SYSTEM']):
self.log.debug(message)
if logtype == self.logtype['USER']:
self.__publish_user_system_buffer(message, self.loglevel['DEBUG'])
if logtype == self.logtype['SYSTEM']:
self.__publish_system_buffer(message, self.loglevel['DEBUG'])
def info(self, message, logtype=logtype['SYSTEM']):
self.log.info(message)
if logtype == self.logtype['USER']:
self.__publish_user_system_buffer(message, self.loglevel['INFO'])
if logtype == self.logtype['SYSTEM']:
self.__publish_system_buffer(message, self.loglevel['INFO'])
def warn(self, message, exc_info=None, logtype=logtype['SYSTEM']):
self.log.warn(message, exc_info=exc_info)
if logtype == self.logtype['USER']:
self.__publish_user_system_buffer(message, self.loglevel['WARN'])
if logtype == self.logtype['SYSTEM']:
self.__publish_system_buffer(message, self.loglevel['WARN'])
def error(self, message, exc_info=None, logtype=logtype['SYSTEM']):
self.log.error(message, exc_info=exc_info)
if logtype == self.logtype['USER']:
self.__publish_user_system_buffer(message, self.loglevel['ERROR'])
if logtype == self.logtype['SYSTEM']:
self.__publish_system_buffer(message, self.loglevel['ERROR'])
def critical(self, message, logtype=logtype['SYSTEM']):
self.log.critical(message)
if logtype == self.logtype['USER']:
self.__publish_user_system_buffer(
message, self.loglevel['CRITICAL'])
if logtype == self.logtype['SYSTEM']:
self.__publish_system_buffer(message, self.loglevel['CRITICAL'])
def remove_handler(self, handler):
self.log.removeHandler(handler)
def __get_timestamp(self):
# pylint: disable=no-self-use
return int(time.time() * 1000000)
def __publish_system_buffer(self, message, level):
# pylint: disable=unused-argument
if not self.config['SYSTEM_LOGGING_ENABLED']:
## DO NOT use self.log inside this block, causes recursion
return
def __publish_user_system_buffer(self, message, level):
if not self.config['USER_SYSTEM_LOGGING_ENABLED']:
return
self.log.debug('Publishing logs: USER SYSTEM')
system_output_line = {
'consoleSequenceNumber' : self.__get_timestamp(),
'output': message,
}
self.log.debug(system_output_line)
def __setup_log(self, module_name):
module_name = os.path.basename(module_name)
module_name = module_name.split('.')[0]
logger_name = self.module
log_module_name = '{0} - {1}'.format(logger_name, module_name)
self.log = logging.getLogger(log_module_name)
self.log.setLevel(self.config['LOG_LEVEL'])
self.log.propagate = True
self.handlers = self.log.handlers
# Silence urllib3 and requests
if 'prod' in self.config['RUN_MODE'].lower():
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
self.log.debug('Log Config Setup successful: {0}'.format(module_name))