~jocave/snapcraft/plainbox-provider-plugin

« back to all changes in this revision

Viewing changes to snapcraft/log.py

  • Committer: Michael Terry
  • Date: 2015-08-06 14:59:07 UTC
  • mfrom: (129 snapcraft)
  • mto: This revision was merged to the branch mainline in revision 132.
  • Revision ID: michael.terry@canonical.com-20150806145907-wzkkzm3mitiewa03
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
 
3
#
 
4
# Copyright (C) 2015 Canonical Ltd
 
5
#
 
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.
 
9
#
 
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.
 
14
#
 
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/>.
 
17
 
 
18
import logging
 
19
import sys
 
20
 
 
21
 
 
22
_COLOR_BOLD = '\033[1m'
 
23
_COLOR_END = '\033[0m'
 
24
 
 
25
 
 
26
class _StdoutFilter(logging.Filter):
 
27
 
 
28
    def filter(self, record):
 
29
        return record.levelno <= logging.WARNING
 
30
 
 
31
 
 
32
class _StderrFilter(logging.Filter):
 
33
 
 
34
    def filter(self, record):
 
35
        return record.levelno >= logging.ERROR
 
36
 
 
37
 
 
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]
 
44
 
 
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)
 
51
 
 
52
    logger.setLevel(logging.INFO)