Log API requests and responses with Python

resurfaceio resurfaceio Last update: May 06, 2022

resurfaceio-logger-python

Easily log API requests and responses to your own system of record.

PyPICodeFactorLicenseContributing

Contents

Dependencies

Requires Python 3.7 or higher and a requests HTTP library. No other dependencies to conflict with your app.

Installing With pip

pip3 install --upgrade usagelogger

Logging From AIOHTTP

from aiohttp import webfrom usagelogger.middleware.aiohttp import HttpLoggerForAIOHTTPasync def test(request):    return web.Response(text="Hello")app = web.Application(    middlewares=[        HttpLoggerForAIOHTTP(            url="http://localhost:7701/message", rules="include debug"        )    ])app.router.add_get("/", test)web.run_app(app)

Logging From Django

First edit settings.py to register middleware, like this:

Note: We recommend placing Resurface middleware at the top in the middleware stack.

MIDDLEWARE = [    "usagelogger.middleware.django.HttpLoggerForDjango",  # Always on the top    "django.middleware...",]

Then add a new section to settings.py for logging configuration, like this:

USAGELOGGER = {    'url': 'http://localhost:7701/message',     'rules': 'include debug' }

Logging From Flask

from flask import Flaskfrom usagelogger.middleware.flask import HttpLoggerForFlaskapp = Flask(__name__)app.wsgi_app = HttpLoggerForFlask(  # type: ignore    app=app.wsgi_app, url="http://localhost:7701/message", rules="include debug")@app.route("/")def home():    return "This route works!"app.run(debug=True)

Logging From Requests

from usagelogger import resurfaces = resurface.Session(url="http://localhost:7701/message", rules="include debug")s.get(...)

Logging With API

Loggers can be directly integrated into your application using our API. This requires the most effort compared withthe options described above, but also offers the greatest flexibility and control.

API documentation

Protecting User Privacy

Loggers always have an active set of rules that control what data is loggedand how sensitive data is masked. All of the examples above apply a predefined set of rules (include debug),but logging rules are easily customized to meet the needs of any application.

Logging rules documentation


© 2016-2022 Resurface Labs Inc.

PRAGMA foreign_keys = off; BEGIN TRANSACTION; COMMIT TRANSACTION; PRAGMA foreign_keys = on;

Subscribe to our newsletter