コード例 #1
0
def test_thread_safety():
    """ test context keeps separate correlation ID per thread """
    class _SampleThread(threading.Thread):
        def __init__(self):
            super(_SampleThread, self).__init__()
            self.correlation_id = str(uuid.uuid1())
            self.read_correlation_id = ''

        def run(self):
            cf_logging.FRAMEWORK.context.set_correlation_id(
                self.correlation_id)
            time.sleep(0.1)
            self.read_correlation_id = cf_logging.FRAMEWORK.context.get_correlation_id(
            )

    cf_logging.init(level=logging.DEBUG)

    thread_one = _SampleThread()
    thread_two = _SampleThread()

    thread_one.start()
    thread_two.start()

    thread_one.join()
    thread_two.join()

    assert thread_one.correlation_id == thread_one.read_correlation_id
    assert thread_two.correlation_id == thread_two.read_correlation_id
コード例 #2
0
def test_log_in_expected_format(log_callback):
    """ Test the cf_logger as a standalone """
    cf_logging.init(level=logging.DEBUG)
    logger, stream = config_logger('cli.test')
    log_callback(logger, 'hi')
    log_json = JSONDecoder().decode(stream.getvalue())
    _, error = JsonValidator(JOB_LOG_SCHEMA).validate(log_json)

    assert error == {}
コード例 #3
0
def init(level=defaults.DEFAULT_LOGGING_LEVEL):
    """
    Initializes logging in JSON format.

    :param level: - valid log level from standard logging package (optional)
    """
    framework = Framework(DJANGO_FRAMEWORK_NAME, DjangoContext(),
                          DjangoRequestReader(), DjangoResponseReader())

    cf_logging.init(framework, level)
コード例 #4
0
def test_exception_stacktrace():
    """ Test exception stacktrace is logged """
    cf_logging.init(level=logging.DEBUG)
    logger, stream = config_logger('cli.test')

    try:
        return 1 / 0
    except ZeroDivisionError:
        logger.exception('zero division error')
        log_json = JSONDecoder().decode(stream.getvalue())
        _, error = JsonValidator(JOB_LOG_SCHEMA).validate(log_json)

        assert error == {}
        assert 'ZeroDivisionError' in str(log_json['stacktrace'])
コード例 #5
0
def test_set_correlation_id():
    """ Test setting correlation_id """
    correlation_id = '1234'
    cf_logging.init(level=logging.DEBUG)
    cf_logging.FRAMEWORK.context.set_correlation_id(correlation_id)

    logger, stream = config_logger('cli.test')
    logger.info('hi')

    log_json = JSONDecoder().decode(stream.getvalue())
    _, error = JsonValidator(JOB_LOG_SCHEMA).validate(log_json)

    assert error == {}
    assert log_json['correlation_id'] == correlation_id
    assert cf_logging.FRAMEWORK.context.get_correlation_id() == correlation_id
コード例 #6
0
def init(app, level=defaults.DEFAULT_LOGGING_LEVEL, username_key='username'):
    """ Initializes logging in JSON format.

    :param app: - Falcon application object
    :param level: - valid log level from standard logging package (optional)
    :param username_key: key used by the framework to get the username
        out of the request user, set in the request context,
        like `request.context.get('user').get(key)`
    """
    if not isinstance(app, falcon.API):
        raise TypeError('application should be instance of Falcon API')

    framework = Framework(FALCON_FRAMEWORK_NAME, FalconContext(),
                          FalconRequestReader(username_key),
                          FalconResponseReader())
    cf_logging.init(framework, level)
コード例 #7
0
def _init_framework(level):
    logging.getLogger('werkzeug').disabled = True

    framework = Framework(FLASK_FRAMEWORK_NAME,
                          FlaskContext(), FlaskRequestReader(), FlaskResponseReader())
    cf_logging.init(framework, level)
コード例 #8
0
def test_init_incorrect_framework():
    """ test cf_logging.init fails for invalid framework """
    cf_logging._SETUP_DONE = False
    cf_logging.init({})
コード例 #9
0
def test_init_called_twice(mocker):
    """ test cf_logging.init can be called only once """
    framework = mocker.Mock(Framework)
    cf_logging._SETUP_DONE = False
    cf_logging.init(framework, level=logging.DEBUG)
    cf_logging.init(framework, level=logging.DEBUG)
コード例 #10
0
def initialize_logging():
    cf_logging.init()