minou
minou

Reputation: 16573

Adding logging info to every request in App Engine

As part of a security review, a customer is asking us to add logging info to each request to our website. For example, in a Python/Flask app, I could do this:

@app.before_request
def log_stuff():
    logging.info('something really important')

This may be a dumb question, but would doing this significantly increase GAE operating costs. I suspect not, but wanted to make sure I'm not missing something.

Upvotes: 0

Views: 77

Answers (2)

GAEfan
GAEfan

Reputation: 11370

creolo's answer is correct, but I'll add some...

You might want to put some logic to only log outside requests, or special logging for internals. Something like:

@app.before_request
def before_request():
    # if you want to stop checking when in dev:
    if app.config["DEBUG"]:
        return None
        
    if request.headers.get('User-Agent', 'na').startswith('AppEngine-Google'):
        ...
    if request.path.startswith('/admin/'):
        ...
    if current_user.is_active:
        ...
    if current_user.is_superuser:
        ...

    return None

Also, if space becomes an issue, you can save a very small amount of disk space by customizing your logging config to shorten it, or just use a print() statement:

import logging

LOGGING_FORMAT      = '%(levelname).1s %(message).25s'
LOGGING_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'    #,%(msecs)d
LOGGING_LEVEL       = logging.DEBUG

logging.basicConfig(format=LOGGING_FORMAT, datefmt=LOGGING_DATE_FORMAT, level=LOGGING_LEVEL)

More at: https://docs.python.org/3/howto/logging.html#changing-the-format-of-displayed-messages

Upvotes: 1

creolo
creolo

Reputation: 338

You might need some more space, depending how extensive your log messages are, how many logs you want to store and how high your traffic is. But since 1 GB is far less than 1$ per month, you should not see a significant increase in your GAE costs.

You could just write an example logging message and calculate, how much space you will need for an expected number of requests and for how long this shall be stored. But this should not have any big impact. This is no complex operation, the only thing you should consider is the diskspace you might need in addition.

Upvotes: 1

Related Questions