def json_tb_app(): # Create app app = sanic.Sanic("json_tb_app") TestManager(app) from sanic_json_logging import LOGGING_CONFIG_DEFAULTS as cfg cfg["formatters"]["generic"]["class"] = "sanic_json_logging.formatters.JSONTracebackJSONFormatter" setup_json_logging(app, config=cfg) @app.route("/test_exception", methods=["GET"]) async def test_exception(request): raise Exception("foo") return response.text("") @app.route("/test_get", methods=["GET"]) async def test_get(request): logger = logging.getLogger("sanic.info") class MyClass: def __str__(self): return "my class" logger.info(MyClass()) return response.text("") return app
def generic_app(): # Create app app = sanic.Sanic("generic_app") TestManager(app) setup_json_logging(app) logger = logging.getLogger("myapplogger") async def log(): logger.info("some informational message", extra={"test1": "test"}) @app.route("/test_get", methods=["GET"]) async def test_get(request): await log() return response.text("") @app.route("/test_exception", methods=["GET"]) async def test_exception(request): try: raise ValueError("ugh") except ValueError as err: logger.exception("some exception", exc_info=err) return response.text("") return app
def main(): """ Main entrypoint of this application """ app = NoAccessLogSanic(__name__) setup_json_logging(app) app.add_route(fetch_averaged_metrics_per_country, "/countries/metrics", version="v1") app.run(host="0.0.0.0", port=8000)
def app_alt(): # Create app app = NoAccessLogSanic("test_sanic_app") setup_json_logging(app, context_var='test1') logger = logging.getLogger('root') async def log(): logger.info('some informational message', extra={'test1': 'test'}) @app.route("/test_get", methods=['GET']) async def test_get(request): await log() return response.text('') yield app
def custom_log_app(): # Create app app = sanic.Sanic("custom_log_app") TestManager(app) setup_json_logging(app) logger = logging.getLogger("myapplogger") @app.route("/test_get", methods=["GET"]) async def test_get(request): class MyClass: def __str__(self): return "my class" logger.info(MyClass()) return response.text("") return app
import logging import sanic.response import sanic.request from sanic_json_logging import setup_json_logging, NoAccessLogSanic app = NoAccessLogSanic('app1') setup_json_logging(app) logger = logging.getLogger('root') async def log(): logger.info('some informational message') @app.route("/endpoint1", methods=['GET']) async def endpoint1( request: sanic.request.Request) -> sanic.response.BaseHTTPResponse: await log() return sanic.response.text('') app.run()
import logging import sanic.request import sanic.response from sanic_json_logging import setup_json_logging app = sanic.Sanic("app1") setup_json_logging(app, context_var="test1") logger = logging.getLogger("root") async def log(): logger.info("some informational message") @app.route("/endpoint1", methods=["GET"]) async def endpoint1( request: sanic.request.Request) -> sanic.response.BaseHTTPResponse: await log() return sanic.response.text("") app.run()