Search code examples
pythonloggingservicesystemd

Logging into syslog from a new service in systemD


I created a new service in systemD, so far it does not do anything, I am just trying to check that everything is running step by step.

For that, I am adding logs, however, it does not appear in Syslog, which I understand is the default for services.

When I use simple prints in my code, it does appear in Syslog though

import logging
class recoveryService:

        def __init__(self):
                self.id = 'integ38'
                print self.id # prints to log
                logging.info("the id is {}".format(self.id)) #does not print to log


        def run(self):
                print 'reached run' #prints to log
                logging.info('reached run log') #does not print to log    

if __name__ == '__main__':
    recovery = recoveryService()
    recovery.run()

How can I make these loggings appear in syslog?


Solution

  • logging.getLogger().setLevel('INFO') call this once before your first logging call. The default level of the root logger is WARNING so no logs below that level are shown. Also be aware that it is good practice to define a handler or use logging.basicConfig to set up your logging.