-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlogging_setup.py
49 lines (34 loc) · 1.52 KB
/
logging_setup.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
import logging
import sys
from logging.handlers import RotatingFileHandler
def trace(self, message, *args, **kws):
# Yes, logger takes its '*args' as 'args'.
self._log(logging.TRACE, message, args, **kws)
def add_trace_level():
logging.TRACE = 9
logging.addLevelName(logging.TRACE, "TRACE")
logging.Logger.trace = trace
class UmpumpedLogHandler(logging.Handler):
def emit(self, record):
logging.umpumped_events.append(record)
ei = record.exc_info
def setup_logging(LOGFILE_PATH,level=logging.DEBUG):
# Create our own implementation to have trace logging
# Setup Logger
logging.umpumped_events = []
umpumped_handler = logging.umpumped_handler = UmpumpedLogHandler(logging.INFO)
logging.getLogger('PIL').setLevel(logging.WARNING)
rotating_file_handler = RotatingFileHandler(LOGFILE_PATH, maxBytes=10_000_000,
backupCount=2)
rotating_file_handler.setLevel(level)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(level)
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)20s - %(levelname)8s - %(message)s',
handlers=[stream_handler, rotating_file_handler, umpumped_handler]
# filename='/tmp/myapp.log'
)
logger = logging.getLogger()
logger.debug('DEBUG LEVEL - MAIN MODULE')
logger.info('INFO LEVEL - MAIN MODULE')
logger.trace('TRACE LEVEL - MAIN MODULE')