title: How to log into a file for a python & bottle web server?
body: <p>Following is my code for server. I need to add logging to it . This is a very basic rest api server. I have deployed it on Amazon EC2 . Sometimes due to errors or some other reason the http server shuts down. If I am logged on to EC2 i can see the erros while they happen . But if I am not monitoring it realtime i don't know what error occurred. Therefore I want to add logging which would log the erros in a log file which I can look at later. Please suggest how do i do that. </p>
  
  <pre><code>import json
  import uuid # this is for generating unique id
  import datetime
  import bottle
  from bottle import route, run, request, abort
  from pymongo import Connection
  
  connection = Connection('localhost', 27017)
  db = connection.mydatabase
  
  @route('/documents', method='PUT')
  def put_document():
      data = request.body.readline()
      if not data:
          abort(400, 'No data received')
      entity = json.loads(data)
      if not entity.has_key('_id'):
          abort(400, 'No _id specified')
      try:
          db['documents'].save(entity)
      except ValidationError as ve:
          abort(400, str(ve))
  
  @route('/documents/:id', method='GET')
  def get_document(id):
      entity = db['documents'].find_one({'_id':id})
      if not entity:
          abort(404, 'No document with id %s' % id)
      return entity
  
  @route('/startSession', method = 'GET')
  def startSession():
      #here we need to create a unique id and store it in the database.
      date = str(datetime.datetime.utcnow());
      id = str(uuid.uuid4())
      reply = {'date' : date,
                  'user_id': id
                  }
  
      response = {'date' : date,
          'user_id': id
          }
      return_id = db['users'].save(reply)
  #print 'the id returned is', return_id
  #print 'the changed reply is',reply
  #print 'the NON changed respponse is ',response
      return json.dumps(response)
  
  @route('/set_bus_location', method = 'PUT')
  def set_bus_location():
      data = request.body.readline()
      print 'data is ',data
      if not data:
          abort(400, 'No data received')
      entity = json.loads(data)
      db['bus_locations'].save(entity)
  
  run(host='0.0.0.0', port=8080)
  </code></pre>