16.09.2008, 18:50
Anyone that's managed a SA-MP server will know that the server log can get huge. While this doesn't impact the server performance, it makes reading it or transferring it over FTP or similar a nightmare. In addition, the log only has a timestamp, not a datestamp which can make it hard to find the exact timing of the event.
Edit: As of 0.2X, the log now contains a full time and date stamp
Therefore, I have written a batch script that will rename the server log to the current date. You can then set it to run at the end of each day. After the log has been renamed, it is placed in the log folder.
Windows Version
Copy and paste the code below into a .bat file (e.g logrotate.bat) and save it into the server directory.
This will stop and start the server. If you don't want that to happen remove the taskkill and start lines but be aware that the server must have stopped for the log rotation to work properly!
Remember to set %ServerDirectory% to the directory of your server.
Now, to automate this log rotation, we need to use the Windows task scheduler. Click Start > Control Panel > Scheduled Tasks. Then double click on "Add Scheduled Task". In the window that appears, click next then click browse and point it to the location of your .bat file. Select to perform this task Daily, click next and then set the time to 23:59. This is right at the end of the day. Click next and you may be prompted to enter your user name and password (if there is no password for you user account leave it blank). Finally, click finish and it's all done. Every day at 23:59, the script will run and rotate your log file, keeping the size down and making it easier to manage.
Linux Version
Save this as logrotate.sh
Contributed by Westie - Allows for the main log to be blanked while the server is running
Contributed by KaiserSouse - requires the server to be offline
And add entry into Crontab to automate it.
Enjoy!
Edit: As of 0.2X, the log now contains a full time and date stamp
Therefore, I have written a batch script that will rename the server log to the current date. You can then set it to run at the end of each day. After the log has been renamed, it is placed in the log folder.
Windows Version
Copy and paste the code below into a .bat file (e.g logrotate.bat) and save it into the server directory.
This will stop and start the server. If you don't want that to happen remove the taskkill and start lines but be aware that the server must have stopped for the log rotation to work properly!
Code:
@echo off set ServerDirectory="C:\sampserver" set ServerName="samp-server.exe" for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%c if not exist %ServerDirectory%\log mkdir %ServerDirectory%\log echo Daily log rotator for SA-MP Servers by KingJ echo Will rename the current log file to today's date (%DATE%) echo and place it in the log folder. echo. echo Ensure the server is not running. echo. echo This makes the server logs easier to manage echo. echo Stopping server... taskkill /f /im "%ServerName%" echo Renaming log file to server_log_%XDate%.txt rename %ServerDirectory%\server_log.txt server_log_%XDate%.txt echo Moving log to log folder... move %ServerDirectory%\server_log_%XDate%.txt %ServerDirectory%\log echo Starting Server... start %ServerDirectory%\%ServerName% echo. echo Log rotation complete
Now, to automate this log rotation, we need to use the Windows task scheduler. Click Start > Control Panel > Scheduled Tasks. Then double click on "Add Scheduled Task". In the window that appears, click next then click browse and point it to the location of your .bat file. Select to perform this task Daily, click next and then set the time to 23:59. This is right at the end of the day. Click next and you may be prompted to enter your user name and password (if there is no password for you user account leave it blank). Finally, click finish and it's all done. Every day at 23:59, the script will run and rotate your log file, keeping the size down and making it easier to manage.
Linux Version
Save this as logrotate.sh
Contributed by Westie - Allows for the main log to be blanked while the server is running
Code:
#!/bin/sh logfile=server_log.txt timestamp=$(date --utc +%s) thedate=date logdir=./logs/ if [ ! -d $logdir ] then mkdir $logdir fi cat $logfile > $logdir/server.$timestamp.txt echo "[logs] Refreshed at UNIX TS: $thedate" > $logfile
Code:
FILE=server_log.txt NOW=$(ls -l --time-style="+%b %e, %Y %k%M" $FILE | awk '{ print $6"-"$7"-"$8 }' | sed -e 's/,//g') cp $FILE ./logs/"$NOW.$FILE" rm -f ./server_log.txt
Enjoy!