-
-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathlogger.py
60 lines (44 loc) · 1.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
# -*- coding: utf-8 -*-
"""
Set Up Logging
When using a script interactively, logging should go to stdout and be
human-readable. When using a script as part of a pipe (usually
involving tee), logging should go to stderr.
"""
# ****************************************************************************
# Copyright (C) 2015 Volker Braun <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
import sys
import logging
logger = logging.getLogger()
default_formatter = logging.Formatter(
'%(levelname)s [%(module)s|%(funcName)s:%(lineno)s]: %(message)s')
plain_formatter = logging.Formatter('%(message)s')
class ExcludeInfoFilter(logging.Filter):
def filter(self, record):
return record.levelno != logging.INFO
class OnlyInfoFilter(logging.Filter):
def filter(self, record):
return record.levelno == logging.INFO
def init_logger(config):
level = getattr(logging, config.log.upper())
logger.setLevel(level)
ch_all = logging.StreamHandler(sys.stderr)
ch_all.setLevel(logging.DEBUG)
ch_all.setFormatter(default_formatter)
ch_all.addFilter(ExcludeInfoFilter())
logger.addHandler(ch_all)
if config.interactive:
ch_info = logging.StreamHandler(sys.stdout)
else:
ch_info = logging.StreamHandler(sys.stderr)
ch_info.setLevel(logging.DEBUG)
ch_info.setFormatter(plain_formatter)
ch_info.addFilter(OnlyInfoFilter())
logger.addHandler(ch_info)