Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/sage_bootstrap/logger.py
4052 views
1
# -*- coding: utf-8 -*-
2
"""
3
Set Up Logging
4
5
When using a script interactively, logging should go to stdout and be
6
human-readable. When using a script as part of a pipe (usually
7
involving tee), logging should go to stderr.
8
"""
9
10
11
# ****************************************************************************
12
# Copyright (C) 2015 Volker Braun <[email protected]>
13
#
14
# This program is free software: you can redistribute it and/or modify
15
# it under the terms of the GNU General Public License as published by
16
# the Free Software Foundation, either version 2 of the License, or
17
# (at your option) any later version.
18
# https://www.gnu.org/licenses/
19
# ****************************************************************************
20
21
import sys
22
import logging
23
24
logger = logging.getLogger()
25
26
27
default_formatter = logging.Formatter(
28
'%(levelname)s [%(module)s|%(funcName)s:%(lineno)s]: %(message)s')
29
30
plain_formatter = logging.Formatter('%(message)s')
31
32
33
class ExcludeInfoFilter(logging.Filter):
34
35
def filter(self, record):
36
return record.levelno != logging.INFO
37
38
39
class OnlyInfoFilter(logging.Filter):
40
41
def filter(self, record):
42
return record.levelno == logging.INFO
43
44
45
def init_logger(config):
46
level = getattr(logging, config.log.upper())
47
logger.setLevel(level)
48
ch_all = logging.StreamHandler(sys.stderr)
49
ch_all.setLevel(logging.DEBUG)
50
ch_all.setFormatter(default_formatter)
51
ch_all.addFilter(ExcludeInfoFilter())
52
logger.addHandler(ch_all)
53
if config.interactive:
54
ch_info = logging.StreamHandler(sys.stdout)
55
else:
56
ch_info = logging.StreamHandler(sys.stderr)
57
ch_info.setLevel(logging.DEBUG)
58
ch_info.setFormatter(plain_formatter)
59
ch_info.addFilter(OnlyInfoFilter())
60
logger.addHandler(ch_info)
61
62