Skip to main content

module to log requests and response

Project description

Documentation

In order to use this logger , all you need to do is to import it in your machine using:

    
        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.


{
    "project_id":"szfb72eUt5eRaWxaXE6J",
    "auth_key":"ucdPnQgLqT2GPsquD9xv",
    "includes":["/login"],
    "levels":["*"],
    "time_to_refresh_cofg_file" : "30"
}

lets look at the parameters we configured:-

1. project_id (this is the project id of the project you created at viasocket.)

2. auth_key (this is the authkey of the project you created at viasocket.)

3. includes (it is list of routes whosev logs you want to log)

4. levels (it is list of levels containg logging levels whose logs you want to log (like error or warn or info) )

5. time_to_refresh_cofg(this is the time interval in "seconds" after which the endpoint "END_POINT_TO_SIMULATE_DB" will be automatically hit to get updated value of includes and levels)

Note: configure all the properties carefully and don't left any property empty.

Apart from configuration file, there is one more configurable property which you can set in your application using the method of logger.

Refer below code snippet to configure "route_to_endpoint_map" parameters in your flask application: Actually "route_to_endpoint_map" parameters help you seperate your logs according to routes.
    
map = {}
map['/login'] = '/login'
map['/register'] = '/register'
map['/users/'] = '/users'
init.set_route_to_endpoint_map(map)
    

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.


@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 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 init

from flask import Flask, g, request



map = {}
map['/login'] = '/login'
map['/register'] = '/register'
map['/users/'] = '/users'
init.set_route_to_endpoint_map(map)

app = Flask(__name__)


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

@app.route("/login")
def login():
    init.error('inside login')
    print("on login page!")
    return "login page"

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

@app.route("/logout")
def logout():
    init.info('inside logout')
    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):
    init.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-0.0.6-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file loggover-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: loggover-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3rc1

File hashes

Hashes for loggover-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 33b6fb872618f436fc9b27f190374cc9a26aab96f405544ac68519b44d6c488f
MD5 69c82831f207ad16919b1884876e6e38
BLAKE2b-256 c976d869ddab954648faa216d68bfbc3df211523edd7908d0d5b287e46344740

See more details on using hashes here.

Supported by

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