Rotating Logs

Place for people to discuss public servers, and also for admins to lay out the details of their servers
Post Reply
User avatar
mrapple
Sergeant Major
Sergeant Major
Posts: 460
Joined: Wed May 27, 2009 11:59 am
Location: Unknown
Contact:

Rotating Logs

Post by mrapple »

Im not quite sure where to post this, but I think here is the best place.

So, ever since I have started hosting I have been presented with a problem that I'm sure affects alot of hosters.

Lets say that you are running the logDetail plugin and have bzfs running like this

Code: Select all

bzfs -conf /home/USER/MAP/map.conf > /home/USER/MAP/log.txt
This is what most people do and it works well for keeping a running log of your server.

Now, lets say you have to restart your BZFS for a map update, or restart your entire server for a system update. Then you have a problem. Your problem is that when you start BZFS, it overwrites the old log. Now you could download all the logs, rename them, put them in folders, etc. But thats a pain.

So I was doing a little research and found something that made lots of sense once I thaught about it.

Now lets say you have a start script for every map that contains

Code: Select all

bzfs -conf /home/USER/MAP/map.conf > /home/USER/MAP/log.txt
Now for the cool part. Create a new folder in /home/USER/MAP/ called logs. (You can name it whatever you want, but I chose logs)

Then you can add this in your start script to the name of the log file.

Code: Select all

$(date +%m-%d-%Y@%T)
So now it might look like this

Code: Select all

bzfs -conf /home/USER/MAP/map.conf > /home/USER/MAP/logs/log_$(date +%m-%d-%Y@%T).txt
Now, whenever you start your BZFS instance, it will create a log by the current date and time that looks like this in the logs folder:

Code: Select all

log_10-16-2009@16:21:56.txt
This makes it so you are never overwriting logs. It also helps you easily find logs between dates.

Hope this helps a few of you who are also having this dilemma!

-mrapplecomputer1
Image
User avatar
joevano
General
General
Posts: 1863
Joined: Sat Jun 18, 2005 1:08 pm
Location: South Bend, Indiana, USA

Re: Rotating Logs

Post by joevano »

mrapplecomputer1 wrote:Then you can add this in your start script to the name of the log file.

Code: Select all

$(date +%m-%d-%Y@%T)
So now it might look like this

Code: Select all

bzfs -conf /home/USER/MAP/map.conf > /home/USER/MAP/logs/log_$(date +%m-%d-%Y@%T).txt
Really you should be using '>>' instead of '>' . '>>' is append and '>' is write a new file. Addend will start a new file if one doesn't exist and will just add on to the end of an existing file instead of starting over. That way you can get rid of the time thing and end up with something that looks like this...

Code: Select all

bzfs -conf /home/USER/MAP/map.conf >> /home/USER/MAP/logs/log_$(%Y-%m-%d).txt
PS By using a YMD format instead of a MDY format your logs will sort chronologically in directory listings!
There is nothing worse than aggressive stupidity. -- Johann Wolfgang von Goethe
"How many legs does a dog have if you call his tail a leg? Four. Calling a tail a leg doesn't make it a leg." -- Abraham Lincoln
User avatar
mrapple
Sergeant Major
Sergeant Major
Posts: 460
Joined: Wed May 27, 2009 11:59 am
Location: Unknown
Contact:

Re: Rotating Logs

Post by mrapple »

joevano wrote: '>>' is append and '>' is write a new file.
Wow. Really? Well that sounds great, but don't you think the log file would get a little long after a while? I think adding the date and time will cut down on one big log file, and be easy to find stuff. But this is also another solution also :)
joevano wrote:PS By using a YMD format instead of a MDY format your logs will sort chronologically in directory listings!
You can also (atleast in my SFTP client) sort by modification/creation date which helps alot. thats just for the sake of easy reading :)
Image
User avatar
joevano
General
General
Posts: 1863
Joined: Sat Jun 18, 2005 1:08 pm
Location: South Bend, Indiana, USA

Re: Rotating Logs

Post by joevano »

mrapplecomputer1 wrote: I think adding the date and time will cut down on one big log file, and be easy to find stuff.

Uhm... I still used the date, but if you restart it a couple times in the same day you will only have ONE log file to look in using my technique.
mrapplecomputer1 wrote:
joevano wrote:PS By using a YMD format instead of a MDY format your logs will sort chronologically in directory listings!
You can also (at least in my SFTP client) sort by modification/creation date which helps alot. thats just for the sake of easy reading :)
No offense, but best practice is to name files that have dates in them in YMD format so that it doesn't matter which client you have or don't have. Believe me, I've worked with log files in one form or another for over 20 years and you will never regret using a YMD format, but you will spend time writing scripts to rename files when you didn't do that...
There is nothing worse than aggressive stupidity. -- Johann Wolfgang von Goethe
"How many legs does a dog have if you call his tail a leg? Four. Calling a tail a leg doesn't make it a leg." -- Abraham Lincoln
User avatar
War Pig
Private First Class
Private First Class
Posts: 46
Joined: Sat Feb 16, 2008 11:38 pm

Re: Rotating Logs

Post by War Pig »

mrapple,

Joevano has it right, but also have a look at logrotate if you're running linux. It will automatically rotate, compress and email logfiles based on their size.

#man logrotate

WP
<life> <!-- insert something interesting here --> </life>
Post Reply