Ejemplo n.º 1
0
    def test_colorizing_formatter_with_a_colorizer(self):
        formatter = ColorizingFormatter(fmt='%(message)s')
        record = LogRecord(
            name='my_record',
            level=DEBUG,
            pathname='my_path',
            lineno=42,
            msg='%s + %s gives %s',
            args=(
                4,
                5,
                4 + 5,
            ),
            exc_info=None,
        )
        setattr(
            record,
            ColorizingStreamHandler._RECORD_ATTRIBUTE_NAME,
            self.create_colorizer(format='[%s]'),
        )

        self.assertEqual('[4] + [5] gives [9]', formatter.format(record))

        colorizer = getattr(
            record,
            ColorizingStreamHandler._RECORD_ATTRIBUTE_NAME,
        )
        colorizer.colorize.assert_any_call(4, context_color_tag=None)
        colorizer.colorize.assert_any_call(5, context_color_tag=None)
        colorizer.colorize.assert_any_call(9, context_color_tag=None)
Ejemplo n.º 2
0
 def test_colorizing_formatter_without_a_colorizer_mapping(self):
     formatter = ColorizingFormatter(fmt='%(message)s')
     record = LogRecord(
         name='my_record',
         level=DEBUG,
         pathname='my_path',
         lineno=42,
         msg='%(summand1)d + %(summand2)d gives %(sum)d',
         args=({
             'summand1': 4,
             'summand2': 5,
             'sum': 4 + 5
         }, ),
         exc_info=None,
     )
     self.assertEqual('4 + 5 gives 9', formatter.format(record))
Ejemplo n.º 3
0
 def test_colorizing_formatter_without_a_colorizer(self):
     formatter = ColorizingFormatter(fmt='%(message)s')
     record = LogRecord(
         name='my_record',
         level=DEBUG,
         pathname='my_path',
         lineno=42,
         msg='%d + %d gives %d',
         args=(
             4,
             5,
             4 + 5,
         ),
         exc_info=None,
     )
     self.assertEqual('4 + 5 gives 9', formatter.format(record))
Ejemplo n.º 4
0
    def test_csh_format_no_highlighter_no_color_support(self):
        colorizer = GenericColorizer(color_map={
            'bracket': ('[', ']'),
        })
        formatter = ColorizingFormatter(fmt='%(message)s')
        color_stream = MagicMock()
        color_stream.isatty = lambda: False
        handler = ColorizingStreamHandler(
            stream=color_stream,
            colorizer=colorizer,
        )
        handler.setFormatter(formatter)

        record = LogRecord(
            name='my_record',
            level=DEBUG,
            pathname='my_path',
            lineno=42,
            msg='%s + %s gives %s',
            args=(
                4,
                5,
                Mark(4 + 5, color_tag='bracket'),
            ),
            exc_info=None,
        )

        self.assertEqual('4 + 5 gives 9', handler.format(record))

        # Make sure that the colorizer attribute was removed after processing.
        self.assertFalse(hasattr(record, 'colorizer'))
Ejemplo n.º 5
0
class Logging:

    formatter = ColorizingFormatter("%(asctime)s [%(levelname)s] %(message)s")

    def __init__(self):
        logging.root.setLevel(logging.DEBUG)
        console = ColorizingStreamHandler()
        console.setLevel(logging.INFO)
        self._addhandler(console)

    def _addhandler(self, h):
        h.setFormatter(self.formatter)
        logging.root.addHandler(h)

    def setpath(self, logpath):
        self._addhandler(logging.FileHandler(logpath.pmkdirp()))
Ejemplo n.º 6
0
    def test_csh_format_with_context(self):
        colorizer = GenericColorizer(color_map={
            'bracket': ('[', ']'),
            'context': ('{', '}'),
        })
        highlighter = GenericColorizer(color_map={
            'bracket': ('<', '>'),
            'context': ('(', ')'),
        })
        formatter = ColorizingFormatter(fmt='%(levelname)s %(message)s')
        color_stream = MagicMock()
        color_stream.isatty = lambda: True
        handler = ColorizingStreamHandler(
            stream=color_stream,
            colorizer=colorizer,
            highlighter=highlighter,
            attributes_map={
                'message': 'context',
                'levelname': 'bracket',
            },
        )
        handler.setFormatter(formatter)

        record = LogRecord(
            name='my_record',
            level=DEBUG,
            pathname='my_path',
            lineno=42,
            msg='%s + %s gives %s',
            args=(
                4,
                5,
                Mark(4 + 5, color_tag='bracket'),
            ),
            exc_info=None,
        )

        self.assertEqual(
            '[DEBUG] {4 + 5 gives }{[9]}{}',
            handler.format(record),
        )

        # Make sure that the colorizer attribute was removed after processing.
        self.assertFalse(hasattr(record, 'colorizer'))
Ejemplo n.º 7
0
    'info': (Style.BRIGHT + Fore.BLUE, Style.RESET_ALL),
    'important': (Style.BRIGHT, Style.RESET_ALL),
    'success': (Fore.GREEN, Style.RESET_ALL),
    'warning': (Fore.YELLOW, Style.RESET_ALL),
    'error': (Fore.RED, Style.RESET_ALL),
    'critical': (Back.RED, Style.RESET_ALL),
}


class JMColorizer(GenericColorizer):
    default_color_map = jm_color_map


jm_colorizer = JMColorizer()

logFormatter = ColorizingFormatter("%(asctime)s [%(levelname)s]  %(message)s")
log = logging.getLogger('joinmarket')
log.setLevel(logging.DEBUG)

joinmarket_alert = ['']
core_alert = ['']
debug_silence = [False]

#TODO pass this through from client, bitcoin paramater:
DUST_THRESHOLD = 2730


class JoinMarketStreamHandler(ColorizingStreamHandler):
    def __init__(self):
        super(JoinMarketStreamHandler, self).__init__(colorizer=jm_colorizer)
Ejemplo n.º 8
0
"""

import logging

from chromalog.log import (
    ColorizingStreamHandler,
    ColorizingFormatter,
)

from chromalog.mark.helpers.simple import (
    important,
    success,
    error,
)

formatter = ColorizingFormatter('[%(levelname)s] %(message)s')

handler = ColorizingStreamHandler()
handler.setFormatter(formatter)

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)

logger.info("This is a regular info log message.")
logger.info(
    "Trying to read user information from %s using a json parser.",
    important(r'/usr/local/mylib/user-info.json'),
)
logger.warning(
    "Unable to read the file at %s ! Something is wrong.",