3

I'd like to add kind of application logging in a REST API I'm developing with the fantastic Python EVE framework. The goal is to provide auditing features to the application, so that application logging is my first choice for it.

I've followed steps detailed in this section from the official documentation, but I haven't been lucky. Probably I'm missing something:

from eve import Eve
import logging

def log_every_get(resource, request, payload):
  # custom INFO-level message is sent to the log file
  app.logger.info('We just answered to a GET request!')

app = Eve()
app.on_post_GET += log_every_get
if __name__ == "__main__":

  # enable logging to 'app.log' file
  handler = logging.FileHandler('<chrootedfolder>/app.log')

  # set a custom log format, and add request
  # metadata to each log line
  handler.setFormatter(logging.Formatter(
    '%(asctime)s %(levelname)s: %(message)s '
    '[in %(filename)s:%(lineno)d] -- ip: %(clientip)s, '
    'url: %(url)s, method:%(method)s'))

  # the default log level is set to WARNING, so
  # we have to explictly set the logging level
  # to INFO to get our custom message logged.
  app.logger.setLevel(logging.INFO)

  # append the handler to the default application logger
  app.logger.addHandler(handler)

  app.run()

To give you some more info about the environment where I'm running my REST API: it's running under UWSGI (chrooted to a specific folder) which is behind NGINX (acting as a proxy).

Hope you can help somehow guys.

Thanks in advance.

RAmPE
  • 53
  • 3
  • Does your log file gets created?I am using very similar code to log database entries with `app.on_fetched_item` and it's working. – gcw Jul 29 '16 at 12:39
  • Nop, no files created. I found where the mistake is... the snippet works if you run it from the command line, but under uwsgi doesn't work as the block within the if is never reached. To move the logger initialization out there did the trick... a newbie error that took me several hours of research though :( Thanks anyway for spending some time on this – RAmPE Jul 30 '16 at 17:33

0 Answers0