Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logger file permissions

Tags:

python

logging

Ubuntu server 16.04.5 LTS

I have a python script which creates log files using the following code:

today = datetime.today()
datem = datetime(today.year, today.month, today.day)
logger = logging.getLogger('processImport')
hdlr = logging.FileHandler('{0}myLog_{1}-{2}-{3}.log'.format(myLogFileLocation, datem.year, datem.month, datem.day))
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr) 
logger.setLevel(logging.INFO)

saving a log entry with:

logger.info(logMessage)

I then have a cron job that deletes older log files after a certain period by executing a python file which uses os.remove(fullFilePath) to delete the file.

However, I am getting a permissions error when this cron job executes.

OSError: [Errno 13] Permission denied: PathToTheFile\theLogFileName.log

When I check the permissions for the file they are set to:

-rw-r--r-- 1 www-data www-data etc etc

What do I need to do to enable the cron job to have permission to delete the log files please?

Thank you.

like image 366
Doug Avatar asked Nov 26 '18 03:11

Doug


1 Answers

There seems to be a write issue permission with the folder. Changing the permission should help.

Try this:

log_dir = '/abs/path/of/directory'
os.chmod(log_dir, 0777)

Let me know how it goes.

like image 63
Sagar Dawda Avatar answered Oct 05 '22 07:10

Sagar Dawda