0

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?

2
  • Do the simplest thing that could possibly work, and see how it goes. Commented Mar 20, 2015 at 18:43
  • 3
    Your HTTP server already produces such logs; that's what access logging is for. Why do you need additional timestamps? Commented Mar 20, 2015 at 18:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.