SA-MP Server Log Rotation
#1

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!

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
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

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
Contributed by KaiserSouse - requires the server to be offline



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
And add entry into Crontab to automate it.


Enjoy!
Reply


Messages In This Thread
SA-MP Server Log Rotation - by KingJ - 16.09.2008, 18:50
Re: SA-MP Server Log Rotation - by pspleo - 16.09.2008, 18:55
Re: SA-MP Server Log Rotation - by kaisersouse - 16.09.2008, 18:56
Re: SA-MP Server Log Rotation - by KingJ - 16.09.2008, 18:57
Re: SA-MP Server Log Rotation - by ғαιιοцт - 16.09.2008, 18:57
Re: SA-MP Server Log Rotation - by kaisersouse - 16.09.2008, 19:02
Re: SA-MP Server Log Rotation - by KingJ - 16.09.2008, 19:05
Re: SA-MP Server Log Rotation - by ғαιιοцт - 16.09.2008, 19:16
Re: SA-MP Server Log Rotation - by KingJ - 16.09.2008, 19:21
Re: SA-MP Server Log Rotation - by ғαιιοцт - 16.09.2008, 19:22
Re: SA-MP Server Log Rotation - by KingJ - 16.09.2008, 21:11
Re: SA-MP Server Log Rotation - by KingJ - 16.09.2008, 21:41
Re: SA-MP Server Log Rotation - by Wash - 17.09.2008, 10:33
Re: SA-MP Server Log Rotation - by [M]deLux - 17.09.2008, 12:39
Re: SA-MP Server Log Rotation - by KingJ - 17.09.2008, 12:54
Re: SA-MP Server Log Rotation - by Wash - 17.09.2008, 13:50
Re: SA-MP Server Log Rotation - by KingJ - 17.09.2008, 18:10
Re: SA-MP Server Log Rotation - by mamorunl - 18.09.2008, 10:18
Re: SA-MP Server Log Rotation - by Wash - 18.09.2008, 14:01
Re: SA-MP Server Log Rotation - by chrisb345 - 18.09.2008, 19:23
Re: SA-MP Server Log Rotation - by Acoole - 18.09.2008, 20:44
Re: SA-MP Server Log Rotation - by -Sneaky- - 02.01.2009, 09:28
Re: SA-MP Server Log Rotation - by LethaL - 02.01.2009, 10:31
Re: SA-MP Server Log Rotation - by -Sneaky- - 02.01.2009, 10:37
Re: SA-MP Server Log Rotation - by kingdutch - 25.03.2009, 07:02
SA MP Server Log Rotation - by CipHoony - 22.07.2009, 10:10
Re: SA-MP Server Log Rotation - by Westie - 02.08.2009, 10:47
Re: SA-MP Server Log Rotation - by Machinehank34 - 11.08.2009, 16:06
Re: SA-MP Server Log Rotation - by Patrik356b - 25.08.2009, 21:03
Re: SA-MP Server Log Rotation - by shady91 - 08.10.2009, 03:53
Re: SA-MP Server Log Rotation - by DeltaAirlines12 - 21.10.2009, 22:07
SA MP Server Log Rotation - by brubsifiary - 28.12.2009, 02:03
Re: SA-MP Server Log Rotation - by Domenko - 19.02.2010, 15:53
Re: SA-MP Server Log Rotation - by Westie - 20.02.2010, 10:54
Re: SA-MP Server Log Rotation - by Westie - 10.03.2010, 01:39
Re: SA-MP Server Log Rotation - by viKKmaN - 10.03.2010, 18:34
Re: SA-MP Server Log Rotation - by Micko9 - 22.03.2010, 13:22
Re: SA-MP Server Log Rotation - by JernejL - 23.03.2011, 10:01
Re: SA-MP Server Log Rotation - by rapidhost - 04.12.2011, 12:33
Re : SA-MP Server Log Rotation - by ombre - 16.10.2012, 14:37

Forum Jump:


Users browsing this thread: 7 Guest(s)