是否有一种干净的方法来解析wordpress日志?

时间:2015-08-27 13:53:28

标签: php wordpress

我正在做一个插件,每天检查日志错误并通过UDP发送。

我的想法是打开debug.log并逐行检查,如果错误是在我最后一次检查错误之前,然后我检查它是否是严重错误或警告。 这很容易,但问题是错误可以做多行!

有些时候有一个堆栈跟踪(好吧,我可以跳过它,因为它始终以#开头)。 这实际上是我的代码,但是当错误有多行时它不起作用。

$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);
    }
}

你知道我该怎么办? 感谢

编辑:这是我的日志文件的示例

[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:

0 个答案:

没有答案