title: How can I get my logger in a Python Flask app to use unique IDs per page view for logging?
body: <p>I have a Flask app with a logging system that I wrote based on the standard Python logger.</p>
  
  <p>I want to have a unique ID for each page view, so I can track the entire process and check for logged errors and premature ending. </p>
  
  <p>The first thing I tried was to put unique ID creator into the __init__ for the logger object.The result of that was that all requests had the same view. I moved where the unique ID was created to a method, and this improved the situation - multiple IDs appeared in the logs, and everything appeared to be working. </p>
  
  <p>However, it seems sometimes two requests use the same logger object. It seems when one request is running, another initiates and runs the ID generation method. Then the first request starts using the new ID also...</p>
  
  <pre><code>22:04:31 - MPvzGNAelE : in content
  22:04:31 - MPvzGNAelE : in parse options
  22:04:31 - MPvzGNAelE : finished parse options
  22:04:31 - MPvzGNAelE : about to query API for user info. user id : 7401173, resource id: 59690
  #the following is where the 2nd requests starts
  22:04:31 - SbEOjmbFSa : in  frame initial 
  22:04:31 - SbEOjmbFSa : in  frame 2 - cleaned IDs are 1114 127059932
  22:04:31 - SbEOjmbFSa : in parse options
  22:04:31 - SbEOjmbFSa : finished parse options
  22:04:31 - SbEOjmbFSa : in  frame finishing - for 1114 127059932
  #the following is the first request continuing with the new ID
  22:04:31 - SbEOjmbFSa : api user info status is success
  22:04:31 - SbEOjmbFSa : user_id is 5549565, user name is joe_spryly
  22:04:31 - SbEOjmbFSa : config[data_source] is 0
  22:04:31 - SbEOjmbFSa : api seems to be working, so going to retrieve items for 59690 7401173
  22:04:31 - SbEOjmbFSa : getting items from API for 59690 7401173
  </code></pre>
  
  <p>This is my log object code...</p>
  
  <pre><code>class AS_Log(object):      
      def __init__(self):
          self.log=logging.getLogger('aman_log')
          logging.basicConfig(filename='amanset_log',level=logging.DEBUG)
  
      def generate_id(self):
          from random import choice
          import string
          chars=string.letters+string.digits
          self.log_id=''.join([choice(chars) for i in range(10)])
  
      def format_date(self,timestamp):
          return datetime.fromtimestamp(timestamp).strftime('%m-%d-%y %H:%M:%S')   
  
      def log_msg(self,message):
          self.log.debug('{0} - {1} : {2}'.format(self.format_date(time()),self.log_id,message))     
  </code></pre>
  
  <p>I initiate the log in the flask app like</p>
  
  <pre><code>as=AS_Log()
  </code></pre>
  
  <p>Then call generate_id per request like</p>
  
  <pre><code>@app.route('/&lt;resource_id&gt;/&lt;user_id&gt;/')
  def aman_frame(resource_id,user_id):
      as.generate_id()
      return amanset_frame(resource_id,user_id)
  </code></pre>
  
  <p>then use the log in the amanset_frame function.</p>
  
  <p>I have some notion that this is related application context in Flask but I don't understand how to use that to fix this. Do I use app_context(), and if so, how?</p>