kiwi.logger_color_formatter

  1# Copyright (c) 2015 SUSE Linux GmbH.  All rights reserved.
  2#
  3# This file is part of kiwi.
  4#
  5# kiwi is free software: you can redistribute it and/or modify
  6# it under the terms of the GNU General Public License as published by
  7# the Free Software Foundation, either version 3 of the License, or
  8# (at your option) any later version.
  9#
 10# kiwi 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 kiwi.  If not, see <http://www.gnu.org/licenses/>
 17#
 18import logging
 19
 20
 21class ColorMessage:
 22    """
 23    **Implements color messages for Python logging facility**
 24
 25    Has to implement the format_message method to serve as
 26    message formatter
 27    """
 28    def __init__(self):
 29        BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = list(range(8))
 30        self.color = {
 31            'BLACK': BLACK,
 32            'WARNING': YELLOW,
 33            'INFO': WHITE,
 34            'DEBUG': WHITE,
 35            'CRITICAL': YELLOW,
 36            'ERROR': RED,
 37            'RED': RED,
 38            'GREEN': GREEN,
 39            'YELLOW': YELLOW,
 40            'BLUE': BLUE,
 41            'MAGENTA': MAGENTA,
 42            'CYAN': CYAN,
 43            'WHITE': WHITE
 44        }
 45        self.esc = {
 46            'reset': '\033[0m',
 47            'color': '\033[3;%dm',
 48            'color_light': '\033[2;%dm',
 49            'bold': '\033[1m'
 50        }
 51
 52    def format_message(self, level: str, message: str) -> str:
 53        """
 54        Message formatter with support for embedded color sequences
 55
 56        The Message is allowed to contain the following color metadata:
 57
 58        * $RESET, reset to no color mode
 59        * $BOLD, bold
 60        * $COLOR, color the following text
 61        * $LIGHTCOLOR, light color the following text
 62
 63        The color of the message depends on the level and is defined
 64        in the ColorMessage constructor
 65
 66        :param str level: color level name
 67        :param str message: text
 68
 69        :return: color message with escape sequences
 70
 71        :rtype: str
 72        """
 73        message = message.replace(
 74            '$RESET',
 75            self.esc['reset']
 76        ).replace(
 77            '$BOLD',
 78            self.esc['bold']
 79        ).replace(
 80            '$COLOR',
 81            self.esc['color'] % (30 + self.color[level])
 82        ).replace(
 83            '$LIGHTCOLOR',
 84            self.esc['color_light'] % (30 + self.color[level])
 85        )
 86        for color_name, color_id in list(self.color.items()):
 87            message = message.replace(
 88                '$' + color_name,
 89                self.esc['color'] % (color_id + 30)
 90            ).replace(
 91                '$BG' + color_name,
 92                self.esc['color'] % (color_id + 40)
 93            ).replace(
 94                '$BG-' + color_name,
 95                self.esc['color'] % (color_id + 40)
 96            )
 97        return message + self.esc['reset']
 98
 99
100class ColorFormatter(logging.Formatter):
101    """
102    **Extended standard logging Formatter**
103
104    Extended format supporting text with color metadata
105
106    Example:
107
108    .. code:: python
109
110        ColorFormatter(message_format, '%H:%M:%S')
111    """
112    def format(self, record: logging.LogRecord) -> str:
113        """
114        Creates a logging Formatter with support for color messages
115
116        :param tuple record: logging message record
117
118        :return: result from format_message
119        :rtype: str
120        """
121        color = ColorMessage()
122        levelname = record.levelname
123        message = logging.Formatter.format(self, record)
124        return color.format_message(levelname, message)
class ColorMessage:
22class ColorMessage:
23    """
24    **Implements color messages for Python logging facility**
25
26    Has to implement the format_message method to serve as
27    message formatter
28    """
29    def __init__(self):
30        BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = list(range(8))
31        self.color = {
32            'BLACK': BLACK,
33            'WARNING': YELLOW,
34            'INFO': WHITE,
35            'DEBUG': WHITE,
36            'CRITICAL': YELLOW,
37            'ERROR': RED,
38            'RED': RED,
39            'GREEN': GREEN,
40            'YELLOW': YELLOW,
41            'BLUE': BLUE,
42            'MAGENTA': MAGENTA,
43            'CYAN': CYAN,
44            'WHITE': WHITE
45        }
46        self.esc = {
47            'reset': '\033[0m',
48            'color': '\033[3;%dm',
49            'color_light': '\033[2;%dm',
50            'bold': '\033[1m'
51        }
52
53    def format_message(self, level: str, message: str) -> str:
54        """
55        Message formatter with support for embedded color sequences
56
57        The Message is allowed to contain the following color metadata:
58
59        * $RESET, reset to no color mode
60        * $BOLD, bold
61        * $COLOR, color the following text
62        * $LIGHTCOLOR, light color the following text
63
64        The color of the message depends on the level and is defined
65        in the ColorMessage constructor
66
67        :param str level: color level name
68        :param str message: text
69
70        :return: color message with escape sequences
71
72        :rtype: str
73        """
74        message = message.replace(
75            '$RESET',
76            self.esc['reset']
77        ).replace(
78            '$BOLD',
79            self.esc['bold']
80        ).replace(
81            '$COLOR',
82            self.esc['color'] % (30 + self.color[level])
83        ).replace(
84            '$LIGHTCOLOR',
85            self.esc['color_light'] % (30 + self.color[level])
86        )
87        for color_name, color_id in list(self.color.items()):
88            message = message.replace(
89                '$' + color_name,
90                self.esc['color'] % (color_id + 30)
91            ).replace(
92                '$BG' + color_name,
93                self.esc['color'] % (color_id + 40)
94            ).replace(
95                '$BG-' + color_name,
96                self.esc['color'] % (color_id + 40)
97            )
98        return message + self.esc['reset']

Implements color messages for Python logging facility

Has to implement the format_message method to serve as message formatter

color
esc
def format_message(self, level: str, message: str) -> str:
53    def format_message(self, level: str, message: str) -> str:
54        """
55        Message formatter with support for embedded color sequences
56
57        The Message is allowed to contain the following color metadata:
58
59        * $RESET, reset to no color mode
60        * $BOLD, bold
61        * $COLOR, color the following text
62        * $LIGHTCOLOR, light color the following text
63
64        The color of the message depends on the level and is defined
65        in the ColorMessage constructor
66
67        :param str level: color level name
68        :param str message: text
69
70        :return: color message with escape sequences
71
72        :rtype: str
73        """
74        message = message.replace(
75            '$RESET',
76            self.esc['reset']
77        ).replace(
78            '$BOLD',
79            self.esc['bold']
80        ).replace(
81            '$COLOR',
82            self.esc['color'] % (30 + self.color[level])
83        ).replace(
84            '$LIGHTCOLOR',
85            self.esc['color_light'] % (30 + self.color[level])
86        )
87        for color_name, color_id in list(self.color.items()):
88            message = message.replace(
89                '$' + color_name,
90                self.esc['color'] % (color_id + 30)
91            ).replace(
92                '$BG' + color_name,
93                self.esc['color'] % (color_id + 40)
94            ).replace(
95                '$BG-' + color_name,
96                self.esc['color'] % (color_id + 40)
97            )
98        return message + self.esc['reset']

Message formatter with support for embedded color sequences

The Message is allowed to contain the following color metadata:

  • $RESET, reset to no color mode
  • $BOLD, bold
  • $COLOR, color the following text
  • $LIGHTCOLOR, light color the following text

The color of the message depends on the level and is defined in the ColorMessage constructor

Parameters
  • str level: color level name
  • str message: text
Returns

color message with escape sequences

class ColorFormatter(logging.Formatter):
101class ColorFormatter(logging.Formatter):
102    """
103    **Extended standard logging Formatter**
104
105    Extended format supporting text with color metadata
106
107    Example:
108
109    .. code:: python
110
111        ColorFormatter(message_format, '%H:%M:%S')
112    """
113    def format(self, record: logging.LogRecord) -> str:
114        """
115        Creates a logging Formatter with support for color messages
116
117        :param tuple record: logging message record
118
119        :return: result from format_message
120        :rtype: str
121        """
122        color = ColorMessage()
123        levelname = record.levelname
124        message = logging.Formatter.format(self, record)
125        return color.format_message(levelname, message)

Extended standard logging Formatter

Extended format supporting text with color metadata

Example:

.. code:: python

ColorFormatter(message_format, '%H:%M:%S')
def format(self, record: logging.LogRecord) -> str:
113    def format(self, record: logging.LogRecord) -> str:
114        """
115        Creates a logging Formatter with support for color messages
116
117        :param tuple record: logging message record
118
119        :return: result from format_message
120        :rtype: str
121        """
122        color = ColorMessage()
123        levelname = record.levelname
124        message = logging.Formatter.format(self, record)
125        return color.format_message(levelname, message)

Creates a logging Formatter with support for color messages

Parameters
  • tuple record: logging message record
Returns

result from format_message