StackOverFlow User
StackOverFlow User

Reputation: 958

How to change read write execute permission of a log file using logging module in Jython

Is there a file handler that we can use to change the permissions of the log file created by the Jython script in Linux? The following is the code flow:

  1. Jython script does some deployment stuff and logs on to a file that it creates. This script is ran using a specific user account, say user1.
  2. The same Jython script continues with some other deployment stuff using a different user account, say user2 and the stuff that it does is also logged on to the same file. Now this logging fails with an IOError as the log file has only 644 (rw-r--r--) permission. The log file is created by user1 as mentioned in #1.
  3. Jython script continues with an other user, user3 and also logs to the same file. Again an IOError is thrown by the script due to the same issue.

I want the file permissions to be changed to 777 for the log file. How do I do it? Is there a Log File Handler that I can utilize for this purpose?

Error:

IOError: [Errno 13] Permission denied: '/home/path/....xyz.out'

Code:

LOG_FILENAME = str(propertyFile.getProperty("log_file_path"))
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
logging.debug("--Log Message--")

Upvotes: 0

Views: 3358

Answers (1)

StackOverFlow User
StackOverFlow User

Reputation: 958

Found out the following solution:

Use stat module on os.chmod to set permissions. The following code sets the permissions to RWX for User, Group and Others. The OR condition takes care of setting it for User, Group and Others individually.

Code:

LOG_FILENAME = str(propertyFile.getProperty("log_file_path"));
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG);
os.chmod(LOG_FILENAME, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO);
logging.debug("--Log Message--");

Upvotes: 1

Related Questions