Exemplo n.º 1
0
def pytest_configure(config):
    """ Session-wide test configuration.

    Executes before any tests do and can be used to configure the test
    environment.

    To guard against accidental data loss ``pytest_configure`` will bail
    instantly if the test db contains any data. It also configures it's own
    logging which will override any previous user-configured logging. This
    is to ensure the correct messags reach the users console during test run.

    """
    db = neo4j.Graph(DEFAULT_DB)
    rels = next(db.match(), None)
    if rels:
        logging.warning('Test Runner will only operate on an empty graph.    '
                        'Either clear the DB with `neotool clear` or provide '
                        'a differnt DB URI ')

        pytest.exit(1)

    handler = ColorizingStreamHandler()
    handler.level_map = PY2NEO_LOGGING_LEVEL_COLOUR_MAP
    logger.addHandler(handler)

    logger.info('- all logging captured at >= DEUBG and piped '
                'to stdout for test failures.                 ')
    logger.info('- tests running on Neo4J %s', db.neo4j_version)
Exemplo n.º 2
0
def setup_logging(logger, debug=False, color=True):
    """
    Configure logging.

    :param logger: Logger to configure.
    :type logger: Logger
    :param debug: If True - print debug messages
    :param color: If True - print colored messages
    """

    fmt_str = "%(asctime)s: %(levelname)s:" \
              " %(module)s.%(funcName)s():%(lineno)d: %(message)s"

    logger.handlers = []
    if color:
        colored_console_handler = ColorizingStreamHandler()
        colored_console_handler.level_map[logging.INFO] = (None, 'cyan', False)
        colored_console_handler.setFormatter(logging.Formatter(fmt_str))
        logger.addHandler(colored_console_handler)
    else:
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(logging.Formatter(fmt_str))
        logger.addHandler(console_handler)

    if debug:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)
Exemplo n.º 3
0
def pytest_configure(config):
    """ Session-wide test configuration.

    Executes before any tests do and can be used to configure the test
    environment.

    To guard against accidental data loss ``pytest_configure`` will bail
    instantly if the test db contains any data. It also configures it's own
    logging which will override any previous user-configured logging. This
    is to ensure the correct messags reach the users console during test run.

    """
    db = neo4j.Graph(DEFAULT_DB)
    rels = next(db.match(), None)
    if rels:
        logging.warning(
            'Test Runner will only operate on an empty graph.    '
            'Either clear the DB with `neotool clear` or provide '
            'a differnt DB URI ')

        pytest.exit(1)

    handler = ColorizingStreamHandler()
    handler.level_map = PY2NEO_LOGGING_LEVEL_COLOUR_MAP
    logger.addHandler(handler)

    logger.info('- all logging captured at >= DEUBG and piped '
                'to stdout for test failures.                 ')
    logger.info('- tests running on Neo4J %s', db.neo4j_version)
Exemplo n.º 4
0
def configure_generic_logging(console=True, logfile='log/deathbird.log', log_level=logging.INFO):
    logging.basicConfig(level=log_level,
                        format='%(filename)s %(lineno)d %(asctime)s %(message)s',
                        filename=logfile,
                        filemode='w')

    if console:
        # console = logging.StreamHandler
        console = ColorizingStreamHandler()
        console.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s %(message)s')
        console.setFormatter(formatter)
        logging.getLogger('').addHandler(console)
Exemplo n.º 5
0
def colorize(message, format, color=False):
    '''
    Returns the given message in a colorized format
    string with ANSI escape codes for colorized console outputs:
    - message:   the message to be formatted.
    - format:    triple containing format information:
                 (bg-color, fg-color, bf-boolean) supported colors are
                 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
    - color:     boolean determining whether or not the colorization
                 is to be actually performed.
    '''
    colorize.colorHandler = ColorizingStreamHandler(sys.stdout)
    if color is False: return message
    params = []
    (bg, fg, bold) = format
    if bg in colorize.colorHandler.color_map:
        params.append(str(colorize.colorHandler.color_map[bg] + 40))
    if fg in colorize.colorHandler.color_map:
        params.append(str(colorize.colorHandler.color_map[fg] + 30))
    if bold:
        params.append('1')
    if params:
        message = ''.join((colorize.colorHandler.csi, ';'.join(params), 'm',
                           message, colorize.colorHandler.reset))
    return message
Exemplo n.º 6
0
def config_logging():
    levels = {
        "DEBUG": logging.DEBUG,
        "INFO": logging.INFO,
        "WARNING": logging.WARNING,
        "ERROR": logging.ERROR,
    }
    if "LOG_LEVEL" in environ:
        level = levels.get(environ["LOG_LEVEL"], logging.INFO)
    else:
        level = logging.INFO
    # format_str = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"
    format_str = "%(levelname)-8s %(message)s"
    logger = logging.getLogger()
    # handler = ColorHandler()
    handler = ColorizingStreamHandler()
    formatter = logging.Formatter(format_str)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(level)
Exemplo n.º 7
0
def logger(v):

    logging.addLevelName(41, 'VUL')
    logger = logging.getLogger('cmsMap')

    try:
        from logutils.colorize import ColorizingStreamHandler
        handler = ColorizingStreamHandler(sys.stdout)
        handler.level_map[logging.getLevelName('VUL')] = (None, 'red', True)
        handler.level_map[logging.INFO] = (None, 'white', False)
    except ImportError:
        handler = logging.StreamHandler(sys.stdout)

    formatter = logging.Formatter("[%(funcName)s] %(message)s",
                                  "%Y-%m-%d %H:%M:%S")
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(v * 10)

    return logger
Exemplo n.º 8
0
def add_console_logging(verbose, start_level = logging.ERROR):
    if verbose is None:
        return

    level = (start_level - int(verbose) * 10)
    level = min(level, logging.CRITICAL)
    level = max(level, logging.DEBUG)

    try:
        from logutils.colorize import ColorizingStreamHandler as StreamHandler
        StreamHandler.level_map[logging.DEBUG] = (None, 'black', False)
        StreamHandler.level_map[logging.INFO] = (None, 'blue', False)
    except:
        from logging import StreamHandler
    finally:
        sh = StreamHandler()
        sh.setLevel(level=level)
        logger.addHandler(sh)
        logger.debug('Set console logging to level {level}'.format(
            level=level))

    return level
Exemplo n.º 9
0
def setup(level=None):
    logger = logging.getLogger()

    # disable all other handlers
    for h in logger.handlers:
        logger.removeHandler(h)

    if isinstance(level, basestring):
        level = level.upper()

    if level:
        try:
            logger.setLevel(level)
        except TypeError as err:
            # Logging here actually doesn't work ??
            # logger.warn('Bad Log level was passed in %(err)s', err)
            # Bad log level passed in
            pass

    logger.addHandler(ColorizingStreamHandler())