SamQi has asked for the wisdom of the Perl Monks concerning the following question:

I'm setting up a virtual hosting environment with mod_vhost_alias, so I've got to manually split up log files for each client. I figured that would be really simple, and discovered that it was. All I had to do was write a script that takes input on STDIN and have it do what I need to. The Apache group has split-logfiles which theoretically does this. But I'm running into a peculiar problem. When I put it into my Apache log files with:
CustomLog "|/opt/apache/logs/split-logfiles" combined
It opens each file that it is supposed to, but then none of the log entries get written. I decided to do some testing of my own and wrote this:
#!/usr/bin/perl $isopen = 0; while ($log_line = <STDIN>) { $myfile = "/var/tmp/test.log"; unless($isopen and -f $myfile) { open MYHAPPYFILE, ">$myfile" or die "Error opening $myfile"; $isopen = 1; } printf MYHAPPYFILE "%s", $log_line; } exit 0;
And when I put that into apache instead of split-logfiles, it does the same thing: /var/tmp/test.log gets created but none of the log entries get put into it. When I tried printing to STDERR instead of MYHAPPYFILE, it showed up fine in the error log. Any ideas?

Replies are listed 'Best First'.
Re: Apache log pipe doesn't write to log files
by trs80 (Priest) on Nov 10, 2002 at 06:57 UTC
    I haven't tested this, but this is how I would write it based on what you have provided.
    #!/usr/bin/perl use strict; # split-logfiles # you don't need to test if it is open #$isopen = 0; my $myfile = "/var/tmp/test.log"; # no reason to do this testing # unless($isopen and -f $myfile) { # open the happy file here, but you may want to use >> if # you need to append rather then overwrite on each run open MYHAPPYFILE, ">$myfile" or die "Error opening $myfile"; # don't need to test if it is open because we # are dying if it isn't # $isopen = 1; # } while (my $log_file = <STDIN>) { printf MYHAPPYFILE "%s", $log_line; } exit 0;
    And without commments:
    #!/usr/bin/perl use strict; my $myfile = "/var/tmp/test.log"; open MYHAPPYFILE, ">$myfile" or die "Error opening $myfile"; while (my $log_file = <STDIN>) { printf MYHAPPYFILE "%s", $log_line; } exit 0;
Re: Apache log pipe doesn't write to log files
by chromatic (Archbishop) on Nov 10, 2002 at 06:52 UTC

    Are the files completely empty, or is there a blank line? You're overwriting the combined log file, not appending to it, which may be part of the problem. You will also run into trouble if a line evaluates to false. I'd probably write:

    open( my $fh, '>>', '/var/tmp/test.log' ) or die "Cannot open file: $! +\n"; select $fh; print while <STDIN>;

    That's untested, and it requires Perl 5.6.0 or better.

Re: Apache log pipe doesn't write to log files
by SamQi (Beadle) on Nov 10, 2002 at 06:07 UTC
    It should also be noted that if I run this on the command line:
    $ cat my_normal_log_file | ./split-logfiles
    it works just fine.

      Try closing your ouput file before you exit.


      Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy
        I feel so bright now. I turned off buffering, and it worked just fine. Thanks!