Try LogRotateWin
I've tried LogRotateWin over the last few days.
The projects seems somewhat abandoned and I've found a few bugs in just the little testing I've done. (E.g. some command line parameters that "--help" shows are not actually implemented. E.g. state tracking via logrotate.status statefile does not work for relative paths in logrotate.conf. Instead they ALWAYS get rotated. Not just when their time has come. So you can only use absolute paths.)
But the exe is just 40KB and it does have a few nice features. And I like that fact their mission statement is "The goal is to use the same command line parameters and files as the Linux version." I like it.
Usable example starter-batch and example conf file below.
Logging logrotate's output
Something of a meta-problem: how do you log the activities of logrotate itself? I wrapped the call to logrotate.exe in a batch file. It logs everything to a tempfile, then calls logrotate, then appends everything from the tempfile to a final logfile, then deletes that tempfile. And then that final logfile is then taken care of by logrotate.exe itself in the next run. I then run that starter-batch-file via Windows Task Scheduler.
Mega-dodgy but does the job.
Do-LogRotate.bat
:
setlocal
REM Quick and dirty. Wild mixture of Batch and PowerShell. No error checking.
REM Change current directory to where our script is.
cd /D "%~dp0"
set "TEMPOUTPUTFILE=logrotate.temp-log"
set "FINALOUTPUTFILE=logrotate.log"
echo. 1>> %TEMPOUTPUTFILE% 2>&1
powershell -command "& {Write-Output((get-date -format o) + ' Logrotate script START.')}" 1>> %TEMPOUTPUTFILE% 2>&1
logrotate.exe -s logrotate.status logrotate.conf --verbose 1>> %TEMPOUTPUTFILE% 2>&1
powershell -command "& {Write-Output((get-date -format o) + ' Logrotate script END.')}" 1>> %TEMPOUTPUTFILE% 2>&1
REM Somewhat of a hack. I can't have logrotate handle its own logs on
REM Windows it seems.
REM -- I guess file options for shared access are not set in the right way when
REM you use the ">>" redirect operator. Neither "move", nor "copytruncate"
REM logrotate directives work.
REM So instead append the contents to the other outputfile. -- Not sure this
REM this really works. But oh, well. The logrotate logs themselves are not
REM really that important. So it's not too terrible if we lose a few of them.
TYPE %TEMPOUTPUTFILE% >> %FINALOUTPUTFILE%
DEL %TEMPOUTPUTFILE%
logrotate.conf
:
C:\winscp\WinSCP.log {
daily
minsize 1M
rotate 10
}
# Unfortunately relative paths don't work.
C:\dev\logrotatetests\logrotate.log {
monthly
rotate 5
}
Related reading: I've struggled with Apache Tomcat logs on Windows. This is what made me look into logrotatewin. https://stackoverflow.com/questions/19787279/where-to-configure-internal-tomcat7-stdout-stderr-log-files/54423803#54423803