Skip to main content

module to log requests and response

Project description

A logger for just about everything. The walkover product.

Motivation

logging is very essential part of any application when you debug ,it helps you a alot. So it is useful for logging purposes.

Documentation

To install loggover use below command:
pip install loggover

After installation here comes the configuration part , one need to do in order to use this logger in flask application. All you need to do is to copy below snippet and paste it in a file with name "config.json" (NOTE: file name must be same) in your current working directory. PLease go through all the comments in the below snippet to understand configurable properties betterly.


{
    //change below project id to your project id from viasocket's project
    "project_id":"yhfxSRneYYbMjudS1ysj",
    //change below auth_key to your auth_key from viasocket's project
    "auth_key":"ucdPnQgLqT2GPsquD9xv",
    //includes is the list of routes whose logs you want to log."*" means all routes otherwise you can use like "includes"=["/login","/register"]
    "includes":["*"],
    //It is list of levels containg logging levels whose logs you want to log (like error or warn or info) 
    "levels":["*"],
    //This is the time interval in "seconds" after which the this config file will be automatically loaded to get updated value of includes and levels.
    "time_to_refresh_cofg_file" : "30",
    //Below parameter help you set the alias and conditions to check for a particular route.Refer below example for more clarity, here all routes of /login routes go to login flow and it will go if and only if (status_code==200 or name=='priyansh' and method=='get') is true.
    "route_to_endpoint_map" : {
        "/login":{
            "alias":"/login",
            "conditions":{
                "conditionA":{
                   "condition1":["status_code","200","=="],
                   "condition2":["name","priyansh","=="]
                },
                "conditionB":{
                    "condition1":["method","GET","=="]
                }
            }
        },
        "/register":{
            "alias":"/register"
        },
        "/logout":{
            "alias":"/logout"
        }
    }
}
hasshh!! we are done with this. Lets move forward.

After successfully completing above step , lets move to the core functionality of our logger i.e logging request and response data. For this copy and paste below code in the end of your flask application. Before this, PLease make sure you have following imports in your app, install packages if require.


import datetime
import time
import json
import loggover
from flask import Flask, g, request, abort, jsonify,Response


@app.before_request
def start_timer():
    print('before every request')
    g.start = time.time()

@app.after_request
def fun(response):
    loggover.log_request(response)
    return response

Heads up!! We are done.

logger does not stop serving here only , it has something more for you. Apart from request and response logs , if you want to log info or error or warning logs then for this logger has three more methods. you can use this methods anywhere you want.

for reference see below snippet.

info() method

loggover.info('Information message')

warn() method

loggover.warn('Warning!!')

error() method

loggover.error('Opps!!Error!')

We won't stop here!!

In our applications, we may come accross many Runtime Exceptions and Http Exceptions, but we always want our application to have a normal flow and termination, to handle such Exception in your application, pase following snippet at the top of your app.

@app.errorhandler(Exception)
def handle_exception(e):
    x=init.handle_exc(e)
    return x

we are almost done now...but for better understanding about how to use this logger, have a look at complete flask application which uses this logger.


import datetime
import time
import json
import loggover

from flask import Flask, g, request, abort, jsonify,Response

app = Flask(name)

@app.errorhandler(Exception) def handle_exception(e): x=loggover.handle_exc(e) return x

@app.route("/") def index(): loggover.info('inside index') print(1/0) print("index is running!") return "Hello world"

@app.route("/login") def login(): loggover.error('inside login') name=request.form.get('name') pwd=request.form.get('pwd') response = {"name": name, "pwd": pwd} print(type(response)) return response

@app.route("/register") def register(): loggover.warn('inside register') print("on register page!") return "response page!"

@app.route("/logout") def logout(): loggover.info('inside logout') abort(404, description="Resource not found") print("on logout page!") return "logout page"

@app.route('/users/') def print_user(id): print(id) print('url_rule ',request.url_rule) return "user detail printed"

if name == "main": app.run()

@app.before_request def start_timer(): print('before every request') g.start = time.time()

@app.after_request def fun(response): loggover.log_request(response) return response

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

loggover-1.0.0-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page