-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlog.pyx
61 lines (50 loc) · 1.58 KB
/
log.pyx
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
import logging
from .nginx_core cimport ngx_log_error
from .nginx_core cimport (
NGX_LOG_ALERT,
NGX_LOG_CRIT,
NGX_LOG_ERR,
NGX_LOG_WARN,
NGX_LOG_INFO,
NGX_LOG_DEBUG,
)
from .nginx_core cimport ngx_log_t, ngx_uint_t
cdef class Log:
cdef ngx_log_t *log
def __init__(self, *args):
raise NotImplementedError
@staticmethod
cdef Log from_ptr(ngx_log_t *log):
cdef Log rv = Log.__new__(Log)
rv.log = log
return rv
class NginxLogHandler(logging.Handler):
def __init__(self, log):
super().__init__()
self._log = log
self.lock = None
def emit(self, record):
cdef ngx_uint_t level = NGX_LOG_ALERT
if record.levelno == logging.DEBUG:
level = NGX_LOG_DEBUG
elif record.levelno == logging.INFO:
level = NGX_LOG_INFO
elif record.levelno == logging.WARN:
level = NGX_LOG_WARN
elif record.levelno == logging.ERROR:
level = NGX_LOG_ERR
elif record.levelno == logging.CRITICAL:
level = NGX_LOG_CRIT
ngx_log_error(level, (<Log> self._log).log, 0,
'[{}] {}'.format(
record.name,
record.getMessage()).encode())
def createLock(self):
pass
cdef set_last_resort(Log log):
logging.lastResort = NginxLogHandler(log)
logging.basicConfig(level=logging.NOTSET,
handlers=[logging.lastResort])
cdef unset_last_resort():
logging.lastResort = logging._defaultLastResort
logging.root.handlers.clear()