A better way to archive old Alfresco logs in Windows – Part 2 of 3
In the first article of this series we shared the advantages of using an external approach to automatically delete aged log files in Alfresco. The typical approach, changing the logging settings, can be problematic because it is more difficult to deploy and manage in complex environments. In this article we will discuss how to implement a better approach in Windows.
Windows traces its roots back to MS DOS. At that time, they provided few and very limited tools to handle plain text files. The situation has improved quite a bit since the 80’s and 90’s, but many Alfresco developers and administrators feel at a loss because they are more experienced with Unix and Linux. This article provides straightforward steps to roll off Alfresco related logs using the Windows Scheduler and DOS batch processing. Keep an eye out for the third part of this series where we explain the external method of rolling off files in Linux.
Steps to Roll Off Alfresco Logs
Step 1: Create a batch script to handle roll offs
Create a batch file named rolloff-logs.bat and make sure that you set the variables LOGROOT to the location of the log files, and MAXDAYS to the number of days of logs you want to keep. See instructions below:
@ECHO OFF REM Script to roll off log files on the path LOGROOT and REM matching the patterns in LOGS after MAXDAYS days. REM Configure this batch file to run in the Windows Task Manager. SET LOGROOT=c:\alfresco-content-services\tomcat\logs SET MAXDAYS=30 REM catalina host-manager localhost manager logs could be configured to REM rolloff via the maxDays property. REM The default is to keep all. We’re leaving it that way so we have one point REM (this script) for rolloffs. REM forFiles looks for a pattern on path LOGROOT where the files are REM -MAXDAYS days old and runs a delete command REM on *each* file matching the pattern. REM Normally each forfiles is going to find at a maximum one file if the host REM hasn't been shut down for 24+ hours. FORFILES /p %LOGROOT% /m localhost_access_log????-??-??.txt /d %MAXDAYS% /c "cmd /c del @PATH" FORFILES /p %LOGROOT% /m alfrescotomcat-stdout.????-??-??.log /d %MAXDAYS% /c "cmd /c del @PATH" FORFILES /p %LOGROOT% /m alfrescotomcat-stderr.????-??-??.log /d %MAXDAYS% /c "cmd /c del @PATH" FORFILES /p %LOGROOT% /m catalina.????-??-??.log /d %MAXDAYS% /c "cmd /c del @PATH" FORFILES /p %LOGROOT% /m commons-daemon.????-??-??.log /d %MAXDAYS% /c "cmd /c del @PATH" FORFILES /p %LOGROOT% /m host-manager.????-??-??.log /d %MAXDAYS% /c "cmd /c DEL @PATH" FORFILES /p %LOGROOT% /m localhost.????-??-??.log /d %MAXDAYS% /c "cmd /c DEL @PATH" FORFILES /p %LOGROOT% /m manager.????-??-??.log /d %MAXDAYS% /c "cmd /c del @PATH" FORFILES /p %LOGROOT% /m alfresco.log.????-??-?? /d %MAXDAYS% /c "cmd /c del @PATH" FORFILES /p %LOGROOT% /m share.log.????-??-?? /d %MAXDAYS% /c "cmd /c del @PATH"
Use the following arguments to the FORFILES command in your script:
/p %LOGROOT% specifies where to start the search
/m <pattern> is the files spec using wildcards, when no regex is available
/d %MAXDAYS% is the maximum age of the file in days before it becomes a rolloff candidate
/c "cmd /c DEL @PATH" runs the specified command for each file found, represented by the variable @PATH
Step 2: Set alfresco.log and share.log to go to the <alfresco root>\tomcat\logs directory
The log files alfresco.log and share.log are created at the current working directory when Alfresco is started. Use the Tomcat configuration utility to set the path to the logs directory.
Run the following command:
<alfresco root>\tomcat\bin\alfrescoTomcatw.exe //ES//alfrescoTomcat
Select the Startup tab in the window that opens and enter the path to the Tomcat logs directory in the Working Path edit. Then, select OK to save and exit.
The setting will not take effect until Alfresco is restarted.
Step 3: Create a Windows Scheduler entry to run the batch file periodically
Open the Scheduler application. You can use the Windows key + R and enter taskschd.msc
Select Create Basic Task.
Give the task a name and optional description, and then click Next.
Select how often you want the task to run and click Next.
Enter a time to run and recurrence and click Next.
Select Start a program and click Next.
Enter the path to our rolloff batch file and the arguments, and click Next.
In the summary section, select Open the Properties dialogue for this task when I click Finish.
Select the Run whether user is logged on or not option and change the User to the Alfresco service account, or another account that has rights to remove the log files. Click OK to close and you’re done.
We hope that you found this article helpful. Stay tuned for part three in the series, which will discuss how to roll off logs in Linux. If you need help with something a bit more complicated, please consider using our support and consulting services. Thanks!