Пример #1
0
    def test_debugging(self):
        console_msg = (
            'This should log\nSo should this\nAnd this too\nThis should log '
            'also\nSo should this\nAnd this too\nBut this one should\nAnd this'
            ' one too')

        logger = gogo.Gogo('debug.none').logger
        logger.debug('This should log')
        logger.info('So should this')
        logger.warning('And this too')

        logger = gogo.Gogo('debug.on', verbose=True).logger
        logger.debug('This should log also')
        logger.info('So should this')
        logger.warning('And this too')

        logger = gogo.Gogo('debug.off', verbose=False).logger
        logger.debug("This shouldn't log")
        logger.info('But this one should')
        logger.warning('And this one too')

        results = sys.stdout.getvalue().strip()
        nt.assert_in("This should log", results)
        nt.assert_not_in("This shouldn't log", results)
        nt.assert_equal(console_msg, results)
Пример #2
0
    def test_debugging(self):
        console_msg = (
            "This should log\nSo should this\nAnd this too\nThis should log "
            "also\nSo should this\nAnd this too\nBut this one should\nAnd this"
            " one too"
        )

        logger = gogo.Gogo("debug.none").logger
        logger.debug("This should log")
        logger.info("So should this")
        logger.warning("And this too")

        logger = gogo.Gogo("debug.on", verbose=True).logger
        logger.debug("This should log also")
        logger.info("So should this")
        logger.warning("And this too")

        logger = gogo.Gogo("debug.off", verbose=False).logger
        logger.debug("This shouldn't log")
        logger.info("But this one should")
        logger.warning("And this one too")

        results = sys.stdout.getvalue().strip()
        nt.assert_in("This should log", results)
        nt.assert_not_in("This shouldn't log", results)
        nt.assert_equal(console_msg, results)
Пример #3
0
    def test_formatters(self):
        # basic
        sys.stderr = StringIO()
        logger = gogo.Gogo('stderr_if_gt_error', 'error').logger
        logger.warning('stdout')
        logger.error('stderr')

        # json_formatter
        formatter = gogo.formatters.json_formatter
        json_logger = gogo.Gogo('json', low_formatter=formatter).logger
        json_logger.debug('hello')

        # csv_formatter
        formatter = gogo.formatters.csv_formatter
        csv_logger = gogo.Gogo('csv', low_formatter=formatter).logger
        csv_logger.debug('hello')

        # console_formatter
        formatter = gogo.formatters.console_formatter
        console_lggr = gogo.Gogo('console', low_formatter=formatter).logger
        console_lggr.debug('hello')

        console_msg = (
            'stdout\nstderr\n{"time": "20...", "name": "json.base", "level": '
            '"DEBUG", "message": "hello"}\n20...,csv.base,DEBUG,"hello"\n'
            'console.base: DEBUG    hello')

        results = sys.stdout.getvalue().strip()
        nt.assert_equal_ellipsis(console_msg, results)
        nt.assert_equal('stderr', sys.stderr.getvalue().strip())
Пример #4
0
    def test_formatters(self):
        # basic
        sys.stderr = StringIO()
        logger = gogo.Gogo("stderr_if_gt_error", "error").logger
        logger.warning("stdout")
        logger.error("stderr")

        # json_formatter
        formatter = gogo.formatters.json_formatter
        json_logger = gogo.Gogo("json", low_formatter=formatter).logger
        json_logger.debug("hello")

        # csv_formatter
        formatter = gogo.formatters.csv_formatter
        csv_logger = gogo.Gogo("csv", low_formatter=formatter).logger
        csv_logger.debug('hello "world"')

        # console_formatter
        formatter = gogo.formatters.console_formatter
        console_lggr = gogo.Gogo("console", low_formatter=formatter).logger
        console_lggr.debug("hello")

        console_msg = (
            "stdout\n"
            "stderr\n"
            '{"time": "20...", "name": "js...", "level": "DEBUG", "message": "hello"}\n'
            '"20...","csv.base","DEBUG","hello ""world"""\n'
            "console.base: DEBUG    hello"
        )

        results = sys.stdout.getvalue().strip()
        nt.assert_equal_ellipsis(console_msg, results)
        nt.assert_equal("stderr", sys.stderr.getvalue().strip())
Пример #5
0
    def test_structured_logging(self):
        kwargs = {"persist": True}
        extra = {"additional": True}
        meta = set(["level", "name", "time"])

        # Basic structured logger
        logger0 = gogo.Gogo("logger0").get_structured_logger("base", **kwargs)

        # Structured formatter
        formatter = gogo.formatters.structured_formatter
        logger1 = gogo.Gogo("logger1", low_formatter=formatter).logger

        # JSON formatter
        formatter = gogo.formatters.json_formatter
        logger2 = gogo.Gogo("logger2", low_formatter=formatter).logger

        # Custom logger
        logfmt = (
            '{"time": "%(asctime)s.%(msecs)d", "name": "%(name)s", "level":'
            ' "%(levelname)s", "message": "%(message)s", '
            '"persist": "%(persist)s", "additional": "%(additional)s"}'
        )

        fmtr = logging.Formatter(logfmt, datefmt=gogo.formatters.DATEFMT)
        logger3 = gogo.Gogo("logger3", low_formatter=fmtr).get_logger(**kwargs)

        # Now log some messages
        for logger in [logger0, logger1, logger2, logger3]:
            logger.debug("message", extra=extra)

        lines = sys.stdout.getvalue().strip().split("\n")
        results = [loads(i) for i in lines]

        # Assert the following loggers provide the log event meta data
        nt.assert_is_not_subset(meta, results[0])
        nt.assert_is_subset(meta, results[1])
        nt.assert_is_subset(meta, results[2])
        nt.assert_is_subset(meta, results[3])

        # Assert the following loggers provide the `extra` information
        nt.assert_in("additional", results[0])
        nt.assert_in("additional", results[1])
        nt.assert_not_in("additional", results[2])
        nt.assert_in("additional", results[3])

        # Assert the following loggers provide the `persist` information
        nt.assert_in("persist", results[0])
        nt.assert_in("persist", results[1])
        nt.assert_not_in("persist", results[2])
        nt.assert_in("persist", results[3])

        # Assert the following loggers provide the `msecs` in the time
        nt.assert_false(len(results[0].get("time", [])))
        nt.assert_false(len(results[1]["time"][20:]))
        nt.ok_(len(results[2]["time"][20:]))
        nt.ok_(len(results[3]["time"][20:]))
Пример #6
0
def run():
    """CLI runner
    """
    args = parser.parse_args()
    gogo_logger = gogo.Gogo(__name__, verbose=args.verbose).get_logger("run")

    if args.version:
        gogo_logger.info("gogo v%s" % gogo.__version__)
        exit(0)

    counted = {"get", "tcp"}
    appended = {"filename", "subject", "url", "host", "port"}
    items = args._get_kwargs()
    counted_args = [i for i in items if i[0] in counted]
    appended_args = [i for i in items if i[0] in appended]

    high_appended_args = [(k, v[0]) for k, v in appended_args]
    high_counted_args = [(k, v > 0) for k, v in counted_args]
    high_kwargs = dict(it.chain(high_appended_args, high_counted_args))

    low_appended_args = [(k, v[-1]) for k, v in appended_args]
    low_counted_args = [(k, v > 1) for k, v in counted_args]
    low_kwargs = dict(it.chain(low_appended_args, low_counted_args))

    high_hdlr = getattr(gogo.handlers, "%s_hdlr" % args.high_hdlr)
    low_hdlr = getattr(gogo.handlers, "%s_hdlr" % args.low_hdlr)
    high_format = getattr(gogo.formatters, "%s_formatter" % args.high_format)
    low_format = getattr(gogo.formatters, "%s_formatter" % args.low_format)

    nkwargs = {
        "verbose": args.verbose,
        "high_level": args.high_level.upper(),
        "low_level": args.low_level.upper(),
        "high_formatter": high_format,
        "low_formatter": low_format,
        "monolog": args.monolog,
        "high_hdlr": high_hdlr(**high_kwargs),
        "low_hdlr": low_hdlr(**low_kwargs),
    }

    logger = gogo.Gogo(args.name, **nkwargs).get_logger("runner")

    try:
        message = args.message.read()
    except AttributeError:
        message = args.message

    getattr(logger, args.level)(message)
    exit(0)
Пример #7
0
def run():
    """CLI runner
    """
    args = parser.parse_args()
    gogo_logger = gogo.Gogo(__name__, verbose=args.verbose).get_logger('run')

    if args.version:
        gogo_logger.info('gogo v%s' % gogo.__version__)
        exit(0)

    counted = {'get', 'tcp'}
    appended = {'filename', 'subject', 'url', 'host', 'port'}
    items = args._get_kwargs()
    counted_args = [i for i in items if i[0] in counted]
    appended_args = [i for i in items if i[0] in appended]

    high_appended_args = [(k, v[0]) for k, v in appended_args]
    high_counted_args = [(k, v > 0) for k, v in counted_args]
    high_kwargs = dict(it.chain(high_appended_args, high_counted_args))

    low_appended_args = [(k, v[-1]) for k, v in appended_args]
    low_counted_args = [(k, v > 1) for k, v in counted_args]
    low_kwargs = dict(it.chain(low_appended_args, low_counted_args))

    high_hdlr = getattr(gogo.handlers, '%s_hdlr' % args.high_hdlr)
    low_hdlr = getattr(gogo.handlers, '%s_hdlr' % args.low_hdlr)
    high_format = getattr(gogo.formatters, '%s_formatter' % args.high_format)
    low_format = getattr(gogo.formatters, '%s_formatter' % args.low_format)

    nkwargs = {
        'verbose': args.verbose,
        'high_level': args.high_level.upper(),
        'low_level': args.low_level.upper(),
        'high_formatter': high_format,
        'low_formatter': low_format,
        'monolog': args.monolog,
        'high_hdlr': high_hdlr(**high_kwargs),
        'low_hdlr': low_hdlr(**low_kwargs)
    }

    logger = gogo.Gogo(args.name, **nkwargs).get_logger('runner')

    try:
        message = args.message.read()
    except AttributeError:
        message = args.message

    getattr(logger, args.level)(message)
    exit(0)
Пример #8
0
 def __init__(self):
     log_format = '[%(asctime)s] [%(levelname)s] - %(name)s: %(message)s'
     formatter = logging.Formatter(log_format)
     self.corelogger = gogo.Gogo(
         'core',
         low_hdlr=gogo.handlers.file_hdlr('core.log'),
         low_formatter=formatter,
         high_level='info',
         high_formatter=formatter).logger
     self.clientlogger = gogo.Gogo(
         'client',
         low_hdlr=gogo.handlers.file_hdlr('client.log'),
         low_formatter=formatter,
         high_level='info',
         high_formatter=formatter).logger
Пример #9
0
    def test_extra(self):
        extra = {"additional": True}
        logger1 = gogo.Gogo("basic").get_structured_logger("base2", context=True)
        logger1.debug("basic", extra=extra)

        result = loads(sys.stdout.getvalue().strip())
        nt.assert_equal(result["context"], extra["context"])
Пример #10
0
    def test_structured_formatter(self):
        console_msg = {
            "snowman": "\u2603",
            "name": "structured_formatter.base",
            "level": "INFO",
            "message": "log message",
            "time": "20...",
            "msecs": "...",
            "set_value": [1, 2, 3],
        }

        log_format = gogo.formatters.BASIC_FORMAT
        skwargs = {"datefmt": "%Y"}
        formatter = gogo.formatters.StructuredFormatter(log_format, **skwargs)

        kwargs = {"low_level": "info", "low_formatter": formatter}
        logger = gogo.Gogo("structured_formatter", **kwargs).logger
        extra = {"set_value": set([1, 2, 3]), "snowman": "\u2603"}
        logger.info("log message", extra=extra)
        result = loads(sys.stdout.getvalue())
        result["msecs"] = str(result["msecs"])
        keys = sorted(result.keys())
        nt.assert_equal(sorted(console_msg.keys()), keys)
        whitelist = {"msecs", "time"}

        for k in keys:
            f = nt.assert_equal_ellipsis if k in whitelist else nt.assert_equal
            f(console_msg[k], result[k])
Пример #11
0
def main(script, tests, verbose=False, stop=True):
    """ Main method

    Returns 0 on success, 1 on failure
    """
    failures = 0
    logger = gogo.Gogo(__name__, verbose=verbose).logger
    short_script = p.basename(script)
    env = TestFileEnvironment('.scripttest')

    start = timer()

    for pos, test in enumerate(tests):
        num = pos + 1
        opts, arguments, expected = test
        joined_opts = ' '.join(opts) if opts else ''
        joined_args = '"%s"' % '" "'.join(arguments) if arguments else ''
        command = "%s %s %s" % (script, joined_opts, joined_args)
        short_command = "%s %s %s" % (short_script, joined_opts, joined_args)
        result = env.run(command,
                         cwd=p.abspath(p.dirname(p.dirname(__file__))))
        output = result.stdout

        if isinstance(expected, bool):
            text = StringIO(output).read()
            outlines = [str(bool(text))]
            checklines = StringIO(str(expected)).readlines()
        elif p.isfile(expected):
            outlines = StringIO(output).readlines()

            with open(expected, encoding='utf-8') as f:
                checklines = f.readlines()
        else:
            outlines = StringIO(output).readlines()
            checklines = StringIO(expected).readlines()

        args = [checklines, outlines]
        kwargs = {'fromfile': 'expected', 'tofile': 'got'}
        diffs = ''.join(unified_diff(*args, **kwargs))
        passed = not diffs

        if not passed:
            failures += 1
            msg = "ERROR! Output from test #%i:\n  %s\n" % (num, short_command)
            msg += "doesn't match:\n  %s\n" % expected
            msg += diffs if diffs else ''
        else:
            logger.debug(output)
            msg = 'Scripttest #%i: %s ... ok' % (num, short_command)

        logger.info(msg)

        if stop and failures:
            break

    time = timer() - start
    logger.info('%s' % '-' * 70)
    end = 'FAILED (failures=%i)' % failures if failures else 'OK'
    logger.info('Ran %i scripttests in %0.3fs\n\n%s' % (num, time, end))
    sys.exit(failures)
Пример #12
0
    def test_structured_formatter(self):
        console_msg = {
            'snowman': '\u2603',
            'name': 'structured_formatter.base',
            'level': 'INFO',
            'message': 'log message',
            'time': '20...',
            'msecs': '...',
            'set_value': [1, 2, 3]
        }

        log_format = gogo.formatters.BASIC_FORMAT
        skwargs = {'datefmt': '%Y'}
        formatter = gogo.formatters.StructuredFormatter(log_format, **skwargs)

        kwargs = {'low_level': 'info', 'low_formatter': formatter}
        logger = gogo.Gogo('structured_formatter', **kwargs).logger
        extra = {'set_value': set([1, 2, 3]), 'snowman': '\u2603'}
        logger.info('log message', extra=extra)
        result = loads(sys.stdout.getvalue())
        result['msecs'] = str(result['msecs'])
        keys = sorted(result.keys())
        nt.assert_equal(sorted(console_msg.keys()), keys)
        whitelist = {'msecs', 'time'}

        for k in keys:
            f = nt.assert_equal_ellipsis if k in whitelist else nt.assert_equal
            f(console_msg[k], result[k])
Пример #13
0
def get_logger(logger_name):
    levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
    going = gogo.Gogo('tpod',
                      low_formatter=gogo.formatters.fixed_formatter,
                      high_level='error',
                      levels=levels)
    return going.get_logger(logger_name)
Пример #14
0
def get_app(options):
    """Get app."""
    router = SwaggerRouter(swagger_ui='/swagger/')

    app = web.Application(
        router=router,
        middlewares=[
            jsonify,
            SentryMiddleware(),
            log_middleware,
        ],
    )

    app['logger'] = pygogo.Gogo(
        __name__,
        low_formatter=pygogo.formatters.structured_formatter,
        verbose=options.option('debug'))

    if options.option('debug'):
        logging.basicConfig(level=logging.DEBUG)

    app['sessions'] = {}
    app['logger'].get_logger().debug("starting")
    app['config'] = configparser.ConfigParser()
    app['config'].read(options.option('config') or '')
    setup(app)

    setup_routes(app)
    return app
Пример #15
0
def main(script, tests, verbose=False, stop=True):
    """
    Returns 0 on success, 1 on failure
    """
    failures = 0
    logger = gogo.Gogo(__name__, verbose=verbose).logger
    short_script = p.basename(script)
    env = TestFileEnvironment(".scripttest")

    start = timer()
    for pos, test in enumerate(tests):
        num = pos + 1
        opts, arguments, expected = test
        joined_opts = " ".join(opts) if opts else ""
        joined_args = '"%s"' % '" "'.join(arguments) if arguments else ""
        command = "%s %s %s" % (script, joined_opts, joined_args)
        short_command = "%s %s %s" % (short_script, joined_opts, joined_args)
        result = env.run(command, cwd=PARENT_DIR, expect_stderr=True)
        output = result.stdout

        if isinstance(expected, bool):
            text = StringIO(output).read()
            outlines = [str(bool(text))]
            checklines = StringIO(str(expected)).readlines()
        elif p.isfile(expected):
            outlines = StringIO(output).readlines()

            with open(expected, encoding="utf-8") as f:
                checklines = f.readlines()
        else:
            outlines = StringIO(output).readlines()
            checklines = StringIO(expected).readlines()

        args = [checklines, list(filter_output(outlines))]
        kwargs = {"fromfile": "expected", "tofile": "got"}
        diffs = "".join(unified_diff(*args, **kwargs))

        if diffs:
            failures += 1
            msg = "ERROR! Output from test #%i:\n  %s\n" % (num, short_command)
            msg += "doesn't match:\n  %s\n" % expected
            msg += diffs if diffs else ""
        else:
            logger.debug(output)
            msg = "Scripttest #%i: %s ... ok" % (num, short_command)

        logger.info(msg)

        if stop and failures:
            break

    time = timer() - start
    logger.info("%s" % "-" * 70)
    end = "FAILED (failures=%i)" % failures if failures else "OK"
    logger.info("Ran %i scripttests in %0.3fs\n\n%s", num, time, end)
    sys.exit(failures)
Пример #16
0
def get_logger(name, custom_formatter=None) -> pygogo.Gogo.logger:
    global formatter
    _formatter = formatter
    if custom_formatter:
        _formatter = logging.Formatter(custom_formatter)
    logger = pygogo.Gogo(name,
                         low_formatter=_formatter,
                         high_level="error",
                         high_formatter=formatter).logger
    return logger
Пример #17
0
    def test_params_and_looping(self):
        levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
        f = StringIO()
        going = gogo.Gogo(low_hdlr=gogo.handlers.fileobj_hdlr(f))
        logger1 = going.get_logger('area1')
        logger2 = gogo.Gogo().get_logger('area2')

        logger1_msg = 'A debug message\nAn info message'
        logger1.debug('A debug message')
        logger1.info('An info %s', 'message')
        f.seek(0)
        nt.assert_equal(f.read().strip(), logger1_msg)
        logger2_msg = ''

        for level in [getattr(logging, l) for l in levels]:
            name = logging.getLevelName(level)
            logger2_msg += '%s message\n' % name
            logger2.log(level, '%s %s', name, 'message')
            # TODO: lookup yielding from a nose test

        nt.assert_equal(logger2_msg.strip(), sys.stdout.getvalue().strip())
Пример #18
0
    def test_params_and_looping(self):
        levels = ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL")
        f = StringIO()
        going = gogo.Gogo(low_hdlr=gogo.handlers.fileobj_hdlr(f))
        logger1 = going.get_logger("area1")
        logger2 = gogo.Gogo().get_logger("area2")

        logger1_msg = "A debug message\nAn info message"
        logger1.debug("A debug message")
        logger1.info("An info %s", "message")
        f.seek(0)
        nt.assert_equal(f.read().strip(), logger1_msg)
        logger2_msg = ""

        for level in [getattr(logging, i) for i in levels]:
            name = logging.getLevelName(level)
            logger2_msg += "%s message\n" % name
            logger2.log(level, "%s %s", name, "message")
            # TODO: lookup yielding from a nose test

        nt.assert_equal(logger2_msg.strip(), sys.stdout.getvalue().strip())
Пример #19
0
 def _logger(log_file_name='proton_generic_logs',
             log_file_path='./../../trace/proton_generic_logs.log'):
     log_format = '[%(asctime)s] <---> [%(name)s] <---> [%(levelname)s] <---> [%(message)s]'
     formatter = logging.Formatter(log_format)
     my_logger = gogo.Gogo(
         log_file_name,
         low_hdlr=gogo.handlers.file_hdlr(log_file_path),
         low_formatter=formatter,
         high_level='error',
         high_formatter=formatter,
     ).logger
     return my_logger
Пример #20
0
def log_all(log_fo, log_object, errormsg):
    # define filepath
    log_file_path = "%s_%s.log" % (log_object["telco"],
                                   log_object["scrubber_start_time"])
    logger = gogo.Gogo(low_hdlr=gogo.handlers.file_hdlr(log_file_path)).logger
    # define output string
    formatted = "[%s] - %s - %s - %s - %s - %s" % (
        datetime.datetime.utcnow().isoformat(), log_object["telco"],
        log_object["file_name"], log_object["line_number"],
        log_object["line_content"], errormsg)
    # append output string to logfile each time log_all is called.
    log_fo.write(formatted + "\r\n")
Пример #21
0
    def _logger(self,
                logFileName='pk_logger_genericName',
                logFilePath='./logs/pk_logger_genericName.log'):
        log_format = '[%(asctime)s] <---> [%(name)s] <---> [%(levelname)s] <---> [%(message)s]'
        formatter = logging.Formatter(log_format)
        myLogger = gogo.Gogo(
            logFileName,
            low_hdlr=gogo.handlers.file_hdlr(logFilePath),
            low_formatter=formatter,
            high_level='error',
            high_formatter=formatter,
        ).logger

        return myLogger
    def __init__(self):
        super(PersistApiCalls, self).__init__()
        self.__log_format = '[%(asctime)s] <---> [%(name)s] <---> [%(levelname)s] <---> [%(message)s]'
        self.__formatter = logging.Formatter(self.__log_format)

        self.timer = {}
        self.logger = gogo.Gogo(
            'pk_scraperService_middleware',
            low_hdlr=gogo.handlers.file_hdlr(
                './logs/pk_scraperService_middleware.log'),
            low_formatter=self.__formatter,
            high_level='error',
            high_formatter=self.__formatter,
        ).logger
Пример #23
0
    def test_named_loggers(self):
        sys.stderr = sys.stdout
        logger1 = gogo.Gogo('named').logger
        logger2 = gogo.Gogo('named').logger
        nt.assert_equal(logger1, logger2)

        formatter = gogo.formatters.structured_formatter

        going = gogo.Gogo('named2', low_formatter=formatter)
        logger1 = going.get_logger('foo', test='foo')
        logger2 = going.get_logger('bar', test='bar')
        logger1.debug('message')
        logger2.debug('message')

        for h1, h2 in zip(logger1.handlers, logger2.handlers):
            nt.assert_not_equal(h1, h2)

            for f1, f2 in zip(h1.filters, h2.filters):
                nt.assert_not_equal(f1, f2)

        hdlr = gogo.handlers.stdout_hdlr()
        going = gogo.Gogo('named3', low_hdlr=hdlr, low_formatter=formatter)
        logger1 = going.get_logger('baz', test='baz')
        logger2 = going.get_logger('buzz', test='buzz')

        logger1.debug('message')
        logger2.debug('message')

        for h1, h2 in zip(logger1.handlers, logger2.handlers):
            nt.assert_not_equal(h1, h2)

            for f1, f2 in zip(h1.filters, h2.filters):
                nt.assert_not_equal(f1, f2)

        lines = sys.stdout.getvalue().strip().split('\n')
        nt.assert_not_equal(*(loads(l)['test'] for l in lines[0:2]))
        nt.assert_not_equal(*(loads(l)['test'] for l in lines[2:4]))
Пример #24
0
    def test_named_loggers(self):
        sys.stderr = sys.stdout
        logger1 = gogo.Gogo("named").logger
        logger2 = gogo.Gogo("named").logger
        nt.assert_equal(logger1, logger2)

        formatter = gogo.formatters.structured_formatter

        going = gogo.Gogo("named2", low_formatter=formatter)
        logger1 = going.get_logger("foo", test="foo")
        logger2 = going.get_logger("bar", test="bar")
        logger1.debug("message")
        logger2.debug("message")

        for h1, h2 in zip(logger1.handlers, logger2.handlers):
            nt.assert_not_equal(h1, h2)

            for f1, f2 in zip(h1.filters, h2.filters):
                nt.assert_not_equal(f1, f2)

        hdlr = gogo.handlers.stdout_hdlr()
        going = gogo.Gogo("named3", low_hdlr=hdlr, low_formatter=formatter)
        logger1 = going.get_logger("baz", test="baz")
        logger2 = going.get_logger("buzz", test="buzz")

        logger1.debug("message")
        logger2.debug("message")

        for h1, h2 in zip(logger1.handlers, logger2.handlers):
            nt.assert_not_equal(h1, h2)

            for f1, f2 in zip(h1.filters, h2.filters):
                nt.assert_not_equal(f1, f2)

        lines = sys.stdout.getvalue().strip().split("\n")
        nt.assert_not_equal(*(loads(i)["test"] for i in lines[0:2]))
        nt.assert_not_equal(*(loads(i)["test"] for i in lines[2:4]))
Пример #25
0
    def test_handlers(self):
        f = StringIO()
        hdlr = gogo.handlers.fileobj_hdlr(f)
        lggr = gogo.Gogo("test_handlers", high_hdlr=hdlr).logger

        msg1 = "stdout hdlr only"
        lggr.debug(msg1)
        f.seek(0)
        nt.assert_equal(msg1, sys.stdout.getvalue().strip())
        nt.assert_false(f.read())

        msg2 = "both hdlrs"
        lggr.error(msg2)
        f.seek(0)
        nt.assert_equal("%s\n%s" % (msg1, msg2), sys.stdout.getvalue().strip())
        nt.assert_equal(f.read().strip(), msg2)
Пример #26
0
    def base_logger(self):
        """ The base module logger vendor.

        :returns: The base module logger vendor
        :rtype: pygogo.Gogo
        """

        if not hasattr(self, '_base_logger'):
            self._base_logger = pygogo.Gogo(
                self.module_name,
                low_hdlr=logging.handlers.TimedRotatingFileHandler(
                    filename=os.path.join(
                        self.log_dir,
                        ('{self.module_name}.log').format(**locals())),
                    when='midnight'),
                low_formatter=pygogo.formatters.structured_formatter,
                high_level='info',
                high_formatter=pygogo.formatters.fixed_formatter)
        return self._base_logger
Пример #27
0
    def test_multiple_loggers(self):
        f = StringIO()

        going = gogo.Gogo(
            "myapp",
            low_hdlr=gogo.handlers.fileobj_hdlr(f),
            low_formatter=gogo.formatters.fixed_formatter,
            high_hdlr=gogo.handlers.stdout_hdlr(),
            high_level="info",
            high_formatter=gogo.formatters.console_formatter,
        )

        root = going.logger
        logger1 = going.get_logger("area1")
        logger2 = going.get_logger("area2")

        root.info("Jackdaws love my big sphinx of quartz.")
        logger1.debug("Quick zephyrs blow, vexing daft Jim.")
        logger1.info("How quickly daft jumping zebras vex.")
        logger2.warning("Jail zesty vixen who grabbed pay.")
        logger2.error("The five boxing wizards jump quickly.")

        console_msg = (
            "myapp.base  : INFO     Jackdaws love my big sphinx of quartz."
            "\nmyapp.area1 : INFO     How quickly daft jumping zebras vex."
            "\nmyapp.area2 : WARNING  Jail zesty vixen who grabbed pay."
            "\nmyapp.area2 : ERROR    The five boxing wizards jump quickly."
        )

        file_msg = [
            "myapp.base   INFO     Jackdaws love my big sphinx of quartz.\n",
            "myapp.area1  DEBUG    Quick zephyrs blow, vexing daft Jim.\n",
            "myapp.area1  INFO     How quickly daft jumping zebras vex.\n",
            "myapp.area2  WARNING  Jail zesty vixen who grabbed pay.\n",
            "myapp.area2  ERROR    The five boxing wizards jump quickly.\n",
        ]

        f.seek(0)
        nt.assert_equal(console_msg, sys.stdout.getvalue().strip())
        nt.assert_equal([line[24:] for line in f], file_msg)
Пример #28
0
def create_logger(file_path):
    """
    Función que crea un logger para realizar las funciones de logging del proceso. La librería utilizada es Pygogo.

    :param file_path: ruta del fichero sobre el cual realizar el logging-
    :return: objeto logger.
    """

    # Se especifica el formato que tendrá el logging.
    logging_fmt = '%(asctime)s;%(message)s'
    formatter = pygogo.logging.Formatter(logging_fmt,
                                         datefmt=pygogo.formatters.DATEFMT)
    # Se especifica el archivo que contendrá el logging
    fhdlr = create_file_formatter(file_path)

    # Se crea el logger. No se realiza ninguna distinción entre los diferentes niveles de aviso (INFO, WARNING, etc).
    # Por ello, se especifica el mismo file handler para el nivel bajo y alto de las alertas.
    return pygogo.Gogo(name='',
                       low_hdlr=fhdlr,
                       low_formatter=formatter,
                       high_hdlr=fhdlr,
                       high_formatter=formatter,
                       monolog=True).get_logger()
Пример #29
0
 def _logger(log_file_name, log_file_path):
     """
     Spins up a logger.
     :param log_file_name:[str] logger file name
     :param log_file_path:[str] path where log file needs to be created.
     :return: A valid logger on success; error message on failure.
     """
     try:
         if log_file_name is None or log_file_path is None:
             raise Exception(
                 'log_file_name and log_file_path are mandatory to spawn a logger. '
                 'They cannot be None.')
         elif type(log_file_name) is not str or type(
                 log_file_path) is not str:
             raise Exception(
                 'log_file_name and log_file_path must be of type String.')
         else:
             dirname = os.path.dirname(log_file_path)
             if os.path.exists(dirname):
                 log_format = '[%(asctime)s] <---> [%(name)s] <---> [%(levelname)s] <---> [%(message)s]'
                 formatter = logging.Formatter(log_format)
                 my_logger = gogo.Gogo(
                     log_file_name,
                     low_hdlr=gogo.handlers.file_hdlr(log_file_path),
                     low_formatter=formatter,
                     high_level='error',
                     high_formatter=formatter,
                 ).logger
                 return my_logger
             else:
                 raise Exception(
                     'Given path does not exist in the system. Please enter valid path. '
                     'PS: PROTON loggers are in ~/PROTON/trace directory')
     except Exception as e:
         print('Unable to spawn a logger. Stack Trace to follow.')
         print(str(e))
Пример #30
0
import traceback
import pygogo as gogo

from contextlib import closing
from os.path import splitext

from builtins import *
from six.moves.urllib.request import urlopen

from . import processor
from riko.parsers import xml2etree, etree2dict, xpath, get_abspath
from riko.bado import coroutine, return_value, util, io
from meza._compat import encode

OPTS = {'ftype': 'none'}
logger = gogo.Gogo(__name__, monolog=True).logger

# TODO: convert relative links to absolute
# TODO: remove the closing tag if using an HTML tag stripped of HTML tags
# TODO: clean html with Tidy


@coroutine
def async_parser(_, objconf, skip=False, **kwargs):
    """ Asynchronously parses the pipe content

    Args:
        _ (None): Ignored
        objconf (obj): The pipe configuration (an Objectify instance)
        skip (bool): Don't parse the content
        kwargs (dict): Keyword arguments