This file is indexed.

/usr/lib/python3/dist-packages/cement/ext/ext_colorlog.py is in python3-cement 2.10.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""

The ColorLog Extension provides logging based on the standard
``logging`` module and is a drop-in replacement for the default log
handler :class:`cement.ext.ext_logging.LoggingLogHandler`.

Requirements
------------

 * ColorLog (``pip install colorlog``)


Configuration
-------------

This handler honors all of the same configuration settings as the
``LoggingLogHandler`` including:

    * level
    * file
    * to_console
    * rotate
    * max_bytes
    * max_files


In addition, it also supports:

    * colorize_file_log
    * colorize_console_log


A sample config section (in any config file) might look like:

.. code-block:: text

    [log.colorlog]
    file = /path/to/config/file
    level = info
    to_console = true
    rotate = true
    max_bytes = 512000
    max_files = 4
    colorize_file_log = false
    colorize_console_log = true


Usage
-----

.. code-block:: python

    from cement.core.foundation import CementApp

    class MyApp(CementApp):
        class Meta:
            label = 'myapp'
            extensions = ['colorlog']
            log_handler = 'colorlog'

    with MyApp() as app:
        app.run()
        app.log.debug('This is my debug message')
        app.log.info('This is my info message')
        app.log.warning('This is my warning message')
        app.log.error('This is my error message')
        app.log.fatal('This is my critical message')


The colors can be customized by passing in a ``colors`` dictionary mapping
overriding the ``ColorLogHandler.Meta.colors`` meta-data:

.. code-block:: python

    from cement.core.foundation import CementApp
    from cement.ext.ext_colorlog import ColorLogHandler

    COLORS = {
        'DEBUG':    'cyan',
        'INFO':     'green',
        'WARNING':  'yellow',
        'ERROR':    'red',
        'CRITICAL': 'red,bg_white',
    }

    class MyApp(CementApp):
        class Meta:
            label = 'myapp'
            log_handler = ColorLogHandler(colors=COLORS)


Or by sub-classing and creating your own custom class:

.. code-block:: python

    from cement.core.foundation import CementApp
    from cement.ext.ext_colorlog import ColorLogHandler

    class MyCustomLog(ColorLogHandler):
        class Meta:
            label = 'my_custom_log'
            colors = {
                'DEBUG':    'cyan',
                'INFO':     'green',
                'WARNING':  'yellow',
                'ERROR':    'red',
                'CRITICAL': 'red,bg_white',
            }

    class MyApp(CementApp):
        class Meta:
            label = 'myapp'
            log_handler = MyCustomLog

"""

import os
import sys
import logging
from colorlog import ColoredFormatter
from ..ext.ext_logging import LoggingLogHandler
from ..utils.misc import is_true


class ColorLogHandler(LoggingLogHandler):

    """
    This class implements the :class:`cement.core.log.ILog` interface.  It is
    a sub-class of :class:`cement.ext.ext_logging.LoggingLogHandler` which is
    based on the standard :py:class:`logging` library, and adds colorized
    console output using the
    `ColorLog <https://pypi.python.org/pypi/colorlog>`_ library.

    **Note** This extension has an external dependency on ``colorlog``.  You
    must include ``colorlog`` in your applications dependencies as Cement
    explicitly does **not** include external dependencies for optional
    extensions.

    """
    class Meta:

        """Handler meta-data."""

        #: The string identifier of the handler.
        label = "colorlog"

        #: Color mapping for each log level
        colors = {
            'DEBUG':    'cyan',
            'INFO':     'green',
            'WARNING':  'yellow',
            'ERROR':    'red',
            'CRITICAL': 'red,bg_white',
        }

        #: Default configuration settings.  Will be overridden by the same
        #: settings in any application configuration file under a
        #: ``[log.colorlog]`` block.
        config_defaults = dict(
            file=None,
            level='INFO',
            to_console=True,
            rotate=False,
            max_bytes=512000,
            max_files=4,
            colorize_file_log=False,
            colorize_console_log=True,
        )

        #: Formatter class to use for non-colorized logging (non-tty, file,
        #: etc)
        formatter_class_without_color = logging.Formatter

        #: Formatter class to use for colorized logging
        formatter_class = ColoredFormatter

    def _get_console_format(self):
        format = super(ColorLogHandler, self)._get_console_format()
        colorize = self.app.config.get('log.colorlog', 'colorize_console_log')
        if sys.stdout.isatty() or 'CEMENT_TEST' in os.environ:
            if is_true(colorize):
                format = "%(log_color)s" + format
        return format

    def _get_file_format(self):
        format = super(ColorLogHandler, self)._get_file_format()
        colorize = self.app.config.get('log.colorlog', 'colorize_file_log')
        if is_true(colorize):
            format = "%(log_color)s" + format
        return format

    def _get_console_formatter(self, format):
        colorize = self.app.config.get('log.colorlog', 'colorize_console_log')
        if sys.stdout.isatty() or 'CEMENT_TEST' in os.environ:
            if is_true(colorize):
                formatter = self._meta.formatter_class(
                    format,
                    log_colors=self._meta.colors
                )
            else:
                formatter = self._meta.formatter_class_without_color(
                    format
                )
        else:
            klass = self._meta.formatter_class_without_color  # pragma: nocover
            formatter = klass(format)                        # pragma: nocover

        return formatter

    def _get_file_formatter(self, format):
        colorize = self.app.config.get('log.colorlog', 'colorize_file_log')
        if is_true(colorize):
            formatter = self._meta.formatter_class(
                format,
                log_colors=self._meta.colors
            )
        else:
            formatter = self._meta.formatter_class_without_color(format)

        return formatter


def load(app):
    app.handler.register(ColorLogHandler)