title: flask how to log all warning exceptions (full stack trace) to file
body: <p>So I am trying to build a restful API using flask, served up by apache on centos (httpd). </p>
  
  <p>Basic API calls work just fine but I am not making much progress on the more advanced aspects because every time it fails I just get an HTTP 500 response which is completely useless for troubleshooting and I have no server-side logs to look at. I am literally trying to solve this through trial and error and it is making me bang my head against the wall.</p>
  
  <p>In order to make any progress on this project I need to setup some basic error logging, but I do not understand the documentation or existing threads about this. It is completely over my head.</p>
  
  <p>What I want to do is have flask write out all warnings and exceptions generated by my application to a specific file (it can be in the app directory to keep it simple).</p>
  
  <p>I am looking for the simplest, easiest, least mind bendy way of doing this... suggestions?</p>
  
  <p>Here is a very simplified version of my app... it shows the basic structure I am using, so please use that for reference.</p>
  
  <pre><code>    from flask import Flask, jsonify, request
      from flask_restful import reqparse, abort, Resource, Api
  
      app = Flask(__name__)
      api = Api(app)
  
      class fetchTicket(Resource):
          def post(self):
  
          request_data = request.get_json(force=True)
  
          r_ticket_id = request_data['ticket_id']
  
          return jsonify(ticket_id=r_ticket_id)
  
  
      api.add_resource(fetchTicket, '/ticket/fetch')
  
      if __name__ == "__main__":
          import logging
          from logging.handlers import FileHandler
  
          app.debug = True
          file_handler = FileHandler("/var/www/project_folder/error.log")
          file_handler.setLevel(logging.DEBUG)
          app.logger.addHandler(file_handler)
          app.run()
  </code></pre>
  
  <p>But when I run the above code no error.log file is created. I am not sure what I am doing wrong.</p>
  
  <p>Note: I did set the folder permissions so that the apache user has access to write to the directory where the log file should go, as per <a href="http://fideloper.com/user-group-permissions-chmod-apache" rel="nofollow">http://fideloper.com/user-group-permissions-chmod-apache</a> but it did not help so I don't think it is a permissions issue.</p>