-
Notifications
You must be signed in to change notification settings - Fork 0
/
logsetup.py
56 lines (43 loc) · 1.83 KB
/
logsetup.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
import logging
import sys
from pythonjsonlogger import jsonlogger
from logging.handlers import RotatingFileHandler
class StackdriverJsonFormatter(jsonlogger.JsonFormatter, object):
def __init__(self,
fmt="%(levelname) %(message)",
style='%',
*args,
**kwargs):
jsonlogger.JsonFormatter.__init__(self, fmt=fmt, *args, **kwargs)
def process_log_record(self, log_record):
log_record['severity'] = log_record['levelname']
del log_record['levelname']
return super(StackdriverJsonFormatter,
self).process_log_record(log_record)
class _MaxLevelFilter(object):
def __init__(self, highest_log_level):
self._highest_log_level = highest_log_level
def filter(self, log_record):
return log_record.levelno <= self._highest_log_level
# A handler for low level logs that should be sent to STDOUT
info_handler = logging.StreamHandler(sys.stdout)
formatter = StackdriverJsonFormatter()
info_handler.setFormatter(formatter)
info_handler.setLevel(logging.INFO)
info_handler.addFilter(_MaxLevelFilter(logging.ERROR))
# A handler for high level logs that should be sent to STDERR
error_handler = logging.StreamHandler(sys.stderr)
formatter = StackdriverJsonFormatter()
error_handler.setFormatter(formatter)
error_handler.setLevel(logging.ERROR)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(info_handler)
logger.addHandler(error_handler)
# See https://github.com/googleapis/google-api-python-client/issues/299
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
def enable_logfile(f):
rotatingFile = RotatingFileHandler(f,
maxBytes=(1048576 * 5),
backupCount=7)
logger.addHandler(rotatingFile)