I have a log4j config that rotates my logs daily. I want to be able to create a script that will compress and move yesterdays log files and move them to a mapped drive on windows.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
If your compression program has a CLI, you can easily create a batch script to do the following with your log file, or log files, or log directory containing the log file(s):
- (x)copy your items if the compression program cannot handle locked items
- compress the items
- move the compressed archive to your directory or UNC path of choice
- delete the original items
If you then create a Windows scheduled task to run this batch script daily or upon logging on, you should be set.
Important notes regarding the above steps in the batch script:
- Most compression program interfaces, if not all, already provide an output path option, so most probably step 3 can be combined with step 2.
- If your compression program also has an option to remove or replace the uncompressed items after compression, then step 4 can also be combined with step 2.
- If by "rotating daily" you mean that each day the log file is overwritten, then depending on the time of execution, there is actually no need for step 4 in your situation.
- Should you then end up with only one step remaining in your batch script, just enter that step directly as the task to schedule.
Concerning compression programs: WinZip, WinRAR, 7zip and IZArc offer command line interfaces, and even Microsoft's Win2k3 Resource Kit contains a Compress console utility…