-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
log.py
72 lines (54 loc) · 1.91 KB
/
log.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
import json
import logging
from logging.handlers import SysLogHandler
import sys
LOG_LEVEL = logging.DEBUG
LOG_VERSION = 0
class StatsMessage(object):
def __init__(self, kernel_id, code, execute_type, remote_ip, referer):
self.msg = [LOG_VERSION, remote_ip, referer, execute_type, kernel_id, code]
def __str__(self):
return json.dumps(self.msg)
syslog = SysLogHandler(address="/dev/log", facility=SysLogHandler.LOG_LOCAL3)
syslog.setFormatter(logging.Formatter(
"%(asctime)s %(process)5d %(name)-28s %(message)s"))
# Default logger for SageCell
logger = logging.getLogger("sagecell")
permalink_logger = logger.getChild("permalink")
stats_logger = logger.getChild("stats")
# Intermediate loggers to be parents for actual receivers and kernels.
kernel_logger = logger.getChild("kernel")
provider_logger = logger.getChild("provider")
root = logging.getLogger()
root.addHandler(syslog)
root.setLevel(LOG_LEVEL)
class TornadoFilter(logging.Filter):
"""
Drop HA-Proxy healthchecks.
"""
def filter(self, record):
return len(record.args) != 3 or \
record.args[:2] != (200, 'OPTIONS / (10.0.3.1)')
logging.getLogger("tornado.access").addFilter(TornadoFilter())
class StdLog(object):
"""
A file-like object for sending stdout/stderr to a log.
"""
def __init__(self, logger, level):
self.logger = logger
self.level = level
def fileno(self):
return 1
def flush(self):
pass
def write(self, data):
self.logger.log(self.level, data)
def std_redirect(logger):
"""
Redirect stdout and stderr to the given logger.
Also set their underscore versions to make IPython happier.
"""
sys.__stdout__ = sys.stdout = StdLog(
logger.getChild("stdout"), logging.DEBUG)
sys.__stderr__ = sys.stderr = StdLog(
logger.getChild("stderr"), logging.WARNING)