2
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
4
# Copyright (C) 2015 Canonical Ltd
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License version 3 as
8
# published by the Free Software Foundation.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
_COLOR_BOLD = '\033[1m'
23
_COLOR_END = '\033[0m'
26
class _StdoutFilter(logging.Filter):
28
def filter(self, record):
29
return record.levelno <= logging.WARNING
32
class _StderrFilter(logging.Filter):
34
def filter(self, record):
35
return record.levelno >= logging.ERROR
38
def configure(logger_name=None):
39
stdout_handler = logging.StreamHandler(stream=sys.stdout)
40
stdout_handler.addFilter(_StdoutFilter())
41
stderr_handler = logging.StreamHandler(stream=sys.stderr)
42
stderr_handler.addFilter(_StderrFilter())
43
handlers = [stdout_handler, stderr_handler]
45
formatter = logging.Formatter(
46
_COLOR_BOLD + '{msg}' + _COLOR_END, style='{')
47
logger = logging.getLogger(logger_name)
48
for handler in handlers:
49
handler.setFormatter(formatter)
50
logger.addHandler(handler)
52
logger.setLevel(logging.INFO)