orange1
orange1

Reputation: 2939

Logging application use in python?

I'd like to log every time someone uses my Flask app. I'd ideally have a log that looks something like this

Timestamp
923829832
929299292
999993939

With a list of Unix time stamps that would represent each time a user had accessed the application. What's a good way to do this?

Upvotes: 0

Views: 166

Answers (1)

Jon Bloom
Jon Bloom

Reputation: 148

You could use Flask's after_request decorator.

For example:

import datetime
import time

@app.after_request
def log():
    with open("app.log", "a") as log:
        date = datetime.datetime.now()
        log.write(time.mktime(dt.timetuple()))

This will open a log file after each request, and log the timestamp to the end of it.

Upvotes: 1

Related Questions