def _check_falcon_request_log(app, headers, expected): _, stream = config_logger('cf.falcon.logger') client = testing.TestClient(app) _check_expected_response(client.simulate_get('/test/path', headers=headers)) assert check_log_record(stream, WEB_LOG_SCHEMA, expected) == {}
def _check_django_request_log(headers, expected): _, stream = config_logger('cf.django.logger') client = Client() _check_expected_response(client.get('/test/path', **headers), body='Hello test!') assert check_log_record(stream, WEB_LOG_SCHEMA, expected) == {}
def test_logging_without_request(): """ Test logger is usable in non request context """ app = Flask(__name__) _set_up_flask_logging(app) logger, stream = config_logger('main.logger') logger.info('works') assert check_log_record(stream, JOB_LOG_SCHEMA, {'msg': v_str('works')}) == {}
async def _logging_correlation_id_route(request): logger, stream = config_logger('user.logging') new_extra = extend(extra, {'request': request}) if provide_request else extra logger.info('in route headers', extra=new_extra) assert check_log_record(stream, JOB_LOG_SCHEMA, expected) == {} return text('ok')
def on_get(self, req, resp): self.extra.update({REQUEST_KEY: req}) self.logger.log(logging.INFO, 'in route headers', extra=self.extra) assert check_log_record(self.stream, JOB_LOG_SCHEMA, self.expected) == {} resp.set_header('Content-Type', 'text/plain') resp.status = falcon.HTTP_200 resp.body = 'ok'
def get(self, request, *args, **kwargs): """ Log a custom user message with the logger """ expected = kwargs.get('expected') or {} extra = kwargs.get('extra') or {} if self.provide_request: extra.update({REQUEST_KEY: request}) self.logger.log(logging.INFO, 'in route headers', extra=extra) assert check_log_record(self.stream, JOB_LOG_SCHEMA, expected) == {} return HttpResponse("ok", content_type='text/plain')
def test_logging_without_request(): """ Test logger is usable in non request context """ app = falcon.API() _set_up_falcon_logging(app) cf_logging.FRAMEWORK.context.set_correlation_id('value') logger, stream = config_logger('main.logger') logger.info('works') assert check_log_record(stream, JOB_LOG_SCHEMA, {'msg': v_str('works')}) == {}
def test_flask_request_log(headers, expected): """ That the expected records are logged by the logging library """ app = Flask(__name__) @app.route('/test/path') def _root(): return Response('ok', mimetype='text/plain') _set_up_flask_logging(app) _, stream = config_logger('cf.flask.logger') client = app.test_client() _check_expected_response(client.get('/test/path', headers=headers)) assert check_log_record(stream, WEB_LOG_SCHEMA, expected) == {}
def test_sanic_request_log(headers, expected): """ Test that the JSON logs contain the expected properties based on the input. """ app = sanic.Sanic('test cf_logging') @app.route('/test/path') async def _headers_route(request): if 'no-content-length' in request.headers: return text('ok', headers={'Content-Type': 'text/plain'}) return text('ok', headers={ 'Content-Length': 2, 'Content-Type': 'text/plain' }) _set_up_sanic_logging(app) _, stream = config_logger('cf.sanic.logger') client = app.test_client _check_expected_response( client.get('/test/path', headers=headers)[1], 200, 'ok') assert check_log_record(stream, WEB_LOG_SCHEMA, expected) == {}
def _logging_correlation_id_route(): logger, stream = config_logger('user.logging') logger.info('in route headers', extra=extra) assert check_log_record(stream, JOB_LOG_SCHEMA, expected) == {} return Response('ok')