• Uncategorized

About python : How-to-make-supervisor-generate-a-new-log-file-for-a-python-service-every-day

Question Detail

I am working on a python service made by third parts.

This service is managed by Supervisor
(I turn the service on and off by running sudo supervisorctl start my_service and sudo supervisorctl stop my_service ),
and its log are stored in just one file,
/var/log/supervisor/my_service-stdout.log.

What I want is that the service generates a new log file every time the service is started on a different date.

In the app, the code referring to the logs is

import logging
logging.basicConfig(format='[%(levelname)1.1s %(asctime)s.%(msecs)03d %(module)s:%(lineno)d] %(message)s', datefmt='%F %T')

I tryed to substitute it with

# log configuration params

LOG_FILE_NAME = "my_service-stdout_{}.log"
LOG_FORMAT = "[%(levelname)1.1s %(asctime)s.%(msecs)03d %(module)s:%(lineno)d] %(message)s'"
LOG_FILE_PATH = "/var/log/supervisor/my_service/"
start_date = datetime.datetime.now().date()

log_name = LOG_FILE_NAME.format(start_date)

# logs configuration
logging.basicConfig(filename=LOG_FILE_PATH + log_name, format=LOG_FORMAT, datefmt='%F %T', level=logging.INFO)

then run

sudo supervisorctl stop my_service
my_service: stopped
sudo supervisorctl start my_service
my_service: ERROR (spawn error)

but I get this traceback, as if the code could not access the directory I specified

Traceback (most recent call last):
      File "my_serviceSrv.py", line 2, in <module>
        import config as cf
      File "/home/orka/services/my_service/config.py", line 23, in <module>
        logging.basicConfig(filename=LOG_FILE_PATH + log_name, format=LOG_FORMAT, datefmt='%F %T', level=logging.INFO)
      File "/usr/lib/python2.7/logging/__init__.py", line 1547, in basicConfig
        hdlr = FileHandler(filename, mode)
      File "/usr/lib/python2.7/logging/__init__.py", line 913, in __init__
        StreamHandler.__init__(self, self._open())
      File "/usr/lib/python2.7/logging/__init__.py", line 943, in _open
        stream = open(self.baseFilename, self.mode)
    IOError: [Errno 13] Permission denied: '/var/log/supervisor/my_service/my_service-stdout_2021-12-28.log'

but the permissions on /var/log/supervisor/my_service/ folder are already drwxr-xr-x.

Furthermore, it keeps on writing the logs in the previous file. Shouldn’t it stop writing logs at all ?

What is wrong?
How can I make the service generate a new log file every time the service is started on a different date?

Question Answer

No answer for now.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.