ash84
ash84

Reputation: 817

how to get request of flask in decorator logger?

I want writing log about query strings and form data of request object in flask.

I use decorator function for logging.

customlog.py

import logging

def webLog(func):
    @wraps(func)
    def newFunc(*args, **kwargs):
        logging.debug(request.url + " : " + str(request.remote_addr))
        return func(*args, **kwargs)
    return newFunc

main.py

@app.route('/')
@customlog.webLog
def hello_world():
    return 'Hello World!'

but, request in main.py, another source file.

How to get request object for logging?
Using parameters of decorator function?

Upvotes: 2

Views: 3747

Answers (1)

Gohn67
Gohn67

Reputation: 10648

I think you can just add the following import from flask import request to customlog.py. Here's the test code I used that worked. I just replaced logging.debug with a simple print statement for testing.

from functools import wraps
from flask import request

def webLog(func):
    @wraps(func)
    def newFunc(*args, **kwargs):
        print request.url + " : " + str(request.remote_addr)
        return func(*args, **kwargs)
    return newFunc

Upvotes: 4

Related Questions