I have Apache 2 installed and running away, logging stuff to the /logs folder called "access/error.txt" And I was wondering, as I do a lot, How to make Apache generate a new ACCESS log file for every unique IP that connects, and log all of that user's requests in their respective file. For example, a guy at 173.49.91.61 visits my server, apache automatically makes /logs/173.49.94.61.txt and logs all accesses to it. Get it?
Thanks, I don't k开发者_开发百科now if this is possible. Hopefully someone will know. grumbles to self inaudibly
Let me first state that this is generally a bad idea, as you'll have one file per IP address. You may think that's cool and easy to manage and all, until you have 2 million+ unique visitors at your site and you start to run into major issues. These issues include: very low disk performance, not being able to easily ls
and rm
in the logging directory, and the possibility of running out of inodes before you run out of disk space. You have been warned.
That said, if you really still want to do this, apache has the CustomLog
directive:
http://httpd.apache.org/docs/current/logs.html#piped
With this you could do something like this:
CustomLog "|/path/to/script" common
Where /path/to/script
is a program that reads stdin, and writes out files based on the IP address given. You'd have to write this yourself, as I'm not aware of anything available that does this. It might look something like this (perl):
#!/usr/bin/perl
while (<STDIN>) {
if ($_ =~ /^(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/) {
open FILE, ">>", "/path/to/log/dir/" . $1 . ".txt";
print FILE $_;
close FILE;
} else {
print STDERR "invalid input format!\n";
}
}
This script is just an example, as it will have race conditions and performance problems, as it opens and closes the file upon each entry. Careful, though, as if you have too many open filehandles at once, you'll be unable to open any more.
精彩评论