How to use Apache Piped Logs to regex replace out unwanted data in logs in real time

apache-2.2loggingperlregex

I have https_acces_log log files being generated by Apache that a certain web app is logging unwanted data into the file. I can't stop the app from logging the data, so I'd like to write a perl/python script which will take the data output by Apache and run a regex on it to replace the data in real time.

Assuming Perl is the right tool, here's where I'm at so far. Script produces no output right now. Help anyone?

$|=1;                               # Use unbuffered output
while (<STDIN>)                     # Loop through STDIN
{
    $Msg = $_;                      # Capture the line of input

    if ($Msg =~ m/&passwd=\w+GET/ )         #   Look for the string I don't want
    {
        $Msg =~ s/&passwd=\w+GET/&password=XXXXXXGET/g;
        print $Msg;                 # Print it
    }
    else
    {
        print $Msg\n;
    }
}

Best Answer

I may be mistaken here, but I believe you would need to have your script handle opening and writing to the log file you are expecting. Apache would not capture the stdout of your script and place it in the log file.

I don't run my own custom scripts with piped logs, but I do use rotatelogs and you do have to give it the path to the log file to write. Such as:

CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/ssl_access_log 86400" common