How to log errors in python script (process)
Question: I need to log errors in python script which running as process.

My current code:

    
    
    import ips
    import sys
    import time
    import logging
    import os
    import string
    import json
    import multiprocessing
    import MySQLdb as mdb
    
    from daemon import Daemon
    
    
    logging.basicConfig(format = u'%(levelname)-8s [%(asctime)s] %(message)s', level = logging.INFO, filename = u'/var/log/info.log')
    
    
    class MyDaemon(Daemon):
        def run(self):        
            print 'the script was run'
            logging.info("the script was run")
    
            while True:
    
                [My code Here]
    
                time.sleep(10)
    
    
    if __name__ == "__main__":
        daemon = MyDaemon('/var/run/pid.pid')
        if len(sys.argv) > 1:
            if 'start' == sys.argv[1]:
                daemon.start()
            elif 'stop' == sys.argv[1]:
                daemon.stop()
            elif 'restart' == sys.argv[1]:
                daemon.restart()
            else:
                print "Unknown command"
                sys.exit(2)
            sys.exit(0)
        else:
            print "usage: %s start|stop|restart" % sys.argv[0]
            sys.exit(2)
    

Some times my process shutdown but I doesn't see error report. How I can see
error report if process shutdown?


Answer: Use the logging module to capture an exception. E.g.:

    
    
    import logging
    
    except serial.SerialException as e:
          logging.debug("Could not open serial port: {}".format(com_port, e))
    

I don't know your code, you enter in the loop but I can advise you to use
maybe returns in program. If it breaks for a specific reason, then you get
some hints. This can be logged too. In python you easily can return several
parameters. I don't know, why you don't use these features.

