bobbui/json-logging-python

json_logging library is breaking Sanic app

Sureya opened this issue · 5 comments

When I execute the following code. I get an error

# in-built
import logging
from sys import stdout

# 3rd party
import json_logging
from sanic import Sanic
from sanic_restful_api import Api, Resource


app = Sanic(__name__)
api = Api(app)

# JSON logging configuration
json_logging.ENABLE_JSON_LOGGING = True
json_logging.CREATE_CORRELATION_ID_IF_NOT_EXISTS = True
json_logging.init_sanic()
json_logging.init_request_instrument(app)
logger = logging.getLogger("extractor-app")
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(stdout))


class ParseData(Resource):
    async def post(self, request):
        input_record = request.json
        logger.info("Everything looks great, queuing record for ")
        input_record['_id'] = json_logging.get_correlation_id()
        logger.info("corelation parsed")
        return input_record, 200


api.add_resource(ParseData, '/review')

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=9558, debug=True)
 File "..//lib/python3.6/site-packages/json_logging/framework/sanic/__init__.py", line 100, in get_correlation_id_in_request_context
    return request.get('correlation_id')
AttributeError: 'Request' object has no attribute 'get'

But if I remove the json_logging it works,

this works

# in-built
import logging
from sys import stdout

# 3rd party
import json_logging
from sanic import Sanic
from sanic_restful_api import Api, Resource


app = Sanic(__name__)
api = Api(app)

# JSON logging configuration
logger = logging.getLogger("extractor-app")
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(stdout))


class ParseData(Resource):
    async def post(self, request):
        input_record = request.json
        logger.info("Everything looks great, queuing record for ")
        logger.info("corelation parsed")
        return input_record, 200


api.add_resource(ParseData, '/review')

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=9558, debug=True)

@thangbn: Any help on this would be much appreciated

@Sureya hi will try to take a look ASAP

@thangbn : Thanks 👍

@Sureya this bug is caused by a breaking upgrade introduce on sanic version 19.12.0: https://github.com/huge-success/sanic/releases/tag/v19.12.0
If you use an earlier version, it should be working fine.
I will try a patch the sanic support to make it work with latest sanic version

release fix on 1.2.1