I need to remove certain strings from my log files. Yes, I know this sounds backward, but for everyone’s sanity, trust me: there’s no way to avoid these useless notices from being written to log in the first place.
So to make my logs readable, such notices have to be deleted after being written.
Here’s an example of the polluted log file:
ERROR - 2012-08-06 02:12:01 --> Severity: Notice --> Undefined index: H1 /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14242
ERROR - 2012-08-06 02:12:01 --> Severity: Notice --> Undefined index: H1>>ID>> /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14288
ERROR - 2012-08-06 02:12:01 --> Severity: Notice --> Undefined index: H1 /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14242
ERROR - 2012-08-06 02:12:01 --> Severity: Notice --> Undefined index: H1>>ID>> /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14288
ERROR - 2012-08-06 02:12:01 --> Severity: Notice --> Undefined index: DIV /var/www/vhosts/example.com/httpdocs/application/helpers/mpdf/mpdf.php 14242
I want to remove lines that:
- contain ‘Notice’ AND
- contain ‘Undefined’ AND
- contain ‘mpdf.php’
I believe something along these lines may be a start:
$dir = '/var/www/vhosts/example.com/httpdocs/application/logs/log-2012-07-07.php';
$content = file_get_contents($dir);
$new = preg_replace($pattern, '', $content) // <===== need help here with $pattern regex
file_put_contents($dir, $new);
Do you have any suggestions on how to do this?
More general,
will remove all lines containing the strings $A and $B and $C.