]> granicus.if.org Git - apache/blob - support/split-logfile.in
apply Apache License, Version 2.0
[apache] / support / split-logfile.in
1 #!@perlbin@
2 #
3 # Copyright 2000-2004 Apache Software Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #
18 # This script will take a combined Web server access
19 # log file and break its contents into separate files.
20 # It assumes that the first field of each line is the
21 # virtual host identity (put there by "%v"), and that
22 # the logfiles should be named that+".log" in the current
23 # directory.
24 #
25 # The combined log file is read from stdin. Records read
26 # will be appended to any existing log files.
27 #
28 %is_open = ();
29
30 while ($log_line = <STDIN>) {
31     #
32     # Get the first token from the log record; it's the
33     # identity of the virtual host to which the record
34     # applies.
35     #
36     ($vhost) = split (/\s/, $log_line);
37     #
38     # Normalize the virtual host name to all lowercase.
39     # If it's blank, the request was handled by the default
40     # server, so supply a default name.  This shouldn't
41     # happen, but caution rocks.
42     #
43     $vhost = lc ($vhost) or "access";
44     #
45     # if the vhost contains a "/" or "\", it is illegal so just use 
46     # the default log to avoid any security issues due if it is interprted
47     # as a directory separator.
48     if ($vhost =~ m#[/\\]#) { $vhost = "access" }
49     #
50     # If the log file for this virtual host isn't opened
51     # yet, do it now.
52     #
53     if (! $is_open{$vhost}) {
54         open $vhost, ">>${vhost}.log"
55             or die ("Can't open ${vhost}.log");
56         $is_open{$vhost} = 1;
57     }
58     #
59     # Strip off the first token (which may be null in the
60     # case of the default server), and write the edited
61     # record to the current log file.
62     #
63     $log_line =~ s/^\S*\s+//;
64     printf $vhost "%s", $log_line;
65 }
66 exit 0;