demo/demo_coloredformatter.py
author Radek Brich <radek.brich@devl.cz>
Thu, 04 Apr 2013 20:24:34 +0200
changeset 2 d3ffa15f5886
permissions -rwxr-xr-x
Add ColoredFormatter. Add setup.py.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
from pycolib.coloredformatter import ColoredFormatter
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
import logging
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
def prepare_logger(name, level=logging.DEBUG):
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
    logger = logging.getLogger(name)
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
    logger.setLevel(level)
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
    return logger
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
def prepare_console_handler(level=logging.DEBUG):
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
    handler = logging.StreamHandler()
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
    formatter = ColoredFormatter('%(asctime)s %(levelname)-5s %(message)s', '%H:%M:%S')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
    formatter.set_color('message', 'RECV', 2)
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
    formatter.set_color('message', 'SEND', 6)
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
    handler.setFormatter(formatter)
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
    handler.setLevel(level)
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
    return handler
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
def setup():
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
    logging.addLevelName(21, 'RECV')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
    logging.addLevelName(22, 'SEND')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
    logger_main = prepare_logger('main')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
    handler_console = prepare_console_handler()
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
    logger_main.addHandler(handler_console)
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
def finish():
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
    logging.shutdown()
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
def main():
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
    setup()
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
    log = logging.getLogger('main')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
    log.info('test')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
    try:
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        raise Exception('test exception')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
    except:
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
        log.exception('test exc')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
    log.log(21, 'recv')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
    log.log(22, 'send')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
    log.debug('debug')
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
    finish()
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
if __name__ == '__main__':
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
    main()
d3ffa15f5886 Add ColoredFormatter. Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59