Is there a clean way to parse wordpress logs?

I am doing a plugin that checks log errors every day and send them by UDP.

My idea was to open debug.log and check line by line if the error is anterior to the last time I checked them, and then I check if it’s a critical error or a warning.
That is pretty easy, but problem is an error can do more than one line!

Read More

And there is a stack trace some times (well, I could just skip it because it’s always begins with #).
This is my code actually but it doesn’t work when an error have multiple lines.

$path = fs_get_wp_config_path();
$path = $path . "/wp-content/debug.log";
$logs = file($path);
$date = get_option('last_date');
if ($date == false)
{
    add_option('last_date', '27-Aug-2015 09:43:55 UTC');
    $date = get_option('last_date');
}
$last_date = new DateTime("27-Aug-2015 09:43:55 UTC");
for($i = 0; $i < count($logs); $i++)
{
     var_dump($logs[$i]);
     if (substr($logs[$i], 0, 12) == "Stack trace:")
    {
        $i++;
        while (substr($logs[$i], 0, 1) == '#' && $i < count($logs))
            $i++;
        $i++;
    }
    else
    {
        $log_date = substr($logs[$i], 1, 24);
        $new_date = new DateTime($log_date);
    //var_dump ($new_date);
    }
}

Do you know how can I do that ?
Thanks

EDIT : This is a sample of my log file

[27-Aug-2015 12:49:14 UTC] PHP Fatal error:  Uncaught exception 'Exception' with message 'DateTime::__construct(): 
Failed to parse time string (tack trace:
) at position 0 (t): The timezone could not be found in the database' in /Users/opsone/Sites/wordpress/wp-content/p
lugins/opsonemonitoring/log.php:51
Stack trace:
#0 /Users/opsone/Sites/wordpress/wp-content/plugins/opsonemonitoring/log.php(51): DateTime->__construct('tack trace
:n')
#1 /Users/opsone/Sites/wordpress/wp-content/plugins/opsonemonitoring/opsonemonitoring.php(30): get_log()
#2 /Users/opsone/Sites/wordpress/wp-content/plugins/opsonemonitoring/opsonemonitoring.php(142): opsoneMonitoring()
#3 [internal function]: mysettings_page('')
#4 /Users/opsone/Sites/wordpress/wp-includes/plugin.php(496): call_user_func_array('mysettings_page', Array)
#5 /Users/opsone/Sites/wordpress/wp-admin/admin.php(212): do_action('settings_page_m...')
#6 /Users/opsone/Sites/wordpress/wp-admin/options-general.php(10): require_once('/Users/opsone/S...')
#7 {main}
  thrown in /Users/opsone/Sites/wordpress/wp-content/plugins/opsonemonitoring/log.php on line 51
[27-Aug-2015 12:52:04 UTC] PHP Fatal error:  Uncaught exception 'Exception' with message 'DateTime::__construct(): 
Failed to parse time string (tack trace:
) at position 0 (t): The timezone could not be found in the database' in /Users/opsone/Sites/wordpress/wp-content/p
lugins/opsonemonitoring/log.php:51
Stack trace:

Related posts