Include timestamp in log
已回答
I have a question regarding logs. I have a script that I run frequently. Under
Run/Debug configurations > Logs > Save console output to file
I have specified a file on the local file system. The thing is that every time I run the script the log is overwritten. Is there a way to generate a timestamp in the filename? I don't think there is, and I have searched quite a bit, but wanted to ask in case I missed it.
BTW; Yes, I know this could be solved using Python's logging framework. That is not the question...
This question is related to PyCharm Community Edition 2016.3.2.
请先登录再写评论。
Right, unfortunately PyCharm's logging facility isn't the right tool for the job. It's is purely about the execution of a run configuration.
I was able to work around this by creating a separate small "log_saver" program. That program simply checks a directory where "naked" logs might be found. If it finds any, it dresses them up. (That is, it renames them to include a filename + the date and time that the log file was last modified.) Be sure to avoid including characters (such as the colon) which may not be allowed in filenames by your OS. Be sure this little Python program can end without any particular exit code.
Now set this program to run within the configuration of your real python program(s). Edit Configurations (under the Run menu) then in each Python program, scroll to the bottom of the Configurations to find the "Before Launch" section. Add your little log_saver program here.
So for any given time, the naked log in the directory are the most current logs for that Python program. The next time one of your Python program runs, it will rename the naked log to include a timestamp using the log_saver program. Then the real Python program will run and create a new naked log.