python flask normal function to decoration
Question: I have this python `flask` code :

    
    
    @app.errorhandler(404)
    def not_found(e):
        logg = open("server_log_404.txt", 'a')
        logg.write("\r\n\r\n========================\n")
        logg.write("%s\n" % datetime.datetime.today().ctime())
        logg.write("%s\n" % request.url )
        logg.write("%s" % request.headers)
        logg.write("%s\n" % str(request.form))
        logg.write("%s\n" % str(request.args))
        logg.close()
        return render_template('404.html'), 404
    

I need change function `logg` in decoration for logging more pages. I need
function like this :

    
    
    @app.errorhandler(404)
    @make_logg
    def not_found(e):
        return render_template('404.html'), 404
    

Is possible ?


Answer: Just transform it into a decorator:

    
    
    from functools import wraps
    def make_logg(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            logg = open("server_log_404.txt", 'a')
            logg.write("\r\n\r\n========================\n")
            logg.write("%s\n" % datetime.datetime.today().ctime())
            logg.write("%s\n" % request.url )
            logg.write("%s" % request.headers)
            logg.write("%s\n" % str(request.form))
            logg.write("%s\n" % str(request.args))
            logg.close()
            return f(*args, **kwargs)
        return wrapper
    

