-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
96 lines (70 loc) · 2.75 KB
/
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
# coding=utf-8
import logging
import os
from settings import log_folder
class StreamToLogger(object):
"""
Fake file-like stream object that redirects writes to a logger instance.
originally taken from
https://www.electricmonk.nl/log/2011/08/14/redirect-stdout-and-stderr-to-a-logger-in-python/
"""
def __init__(self, logger, log_level=logging.INFO):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
def write(self, buf):
# xxx: этот метод искажает traceback
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())
def flush(self):
pass
def fileno(self):
return self.logger.handlers[0].stream.fileno()
def setup_tornado_loggers():
"""Настраиваем логгеры tornado, чтобы они писали в том числе через root_handler"""
for logger_name in {'tornado.access', 'tornado.application', 'tornado.general'}:
logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO)
set_root_handler(logger)
def make_log_path(logger_name):
if not os.path.exists(log_folder):
os.makedirs(log_folder)
return os.path.join(log_folder, '%s.log' % logger_name)
def set_root_handler(logger):
"""Установка основного хендлера для логов. Для удобства все логи аккуммулируются в одном файле.
"""
root_handler = logging.FileHandler(make_log_path('all'))
function_info_str = (
'- [%(filename)s:%(lineno)s '
'- %(funcName)s] '
)
fmt_str = (
'%(asctime)s '
'- %(name)s '
'- %(levelname)s '
) + function_info_str + (
'- %(message)s'
)
custom_formatter = logging.Formatter(fmt_str)
root_handler.setFormatter(custom_formatter)
root_handler.set_name('agentd_root')
logger.addHandler(root_handler)
def create_logger(logger_name, level=logging.INFO):
logger = logging.getLogger(logger_name)
logger.setLevel(level)
handler_names = {handler.name for handler in logger.handlers}
if 'agentd_root' not in handler_names:
set_root_handler(logger)
custom_handler_name = '%s_file_handler' % logger_name
if custom_handler_name not in handler_names:
custom_handler = logging.FileHandler(make_log_path(logger_name))
formatter = logging.Formatter(
'%(asctime)s '
'- %(levelname)s '
'- [%(filename)s:%(lineno)s - %(funcName)s ] '
'- %(message)s'
)
custom_handler.setFormatter(formatter)
custom_handler.set_name(custom_handler_name)
logger.addHandler(custom_handler)
return logger