php日志输出

作为一个后端开发人员肯定是要经常观察日志等记录来查找开发或上线的各种bug,

今天给大家分享下我平常使用的错误日志记录:

getRuntimePath() . '/logs';
    if (!is_dir($dirPath)) {
        @mkdir($dirPath);
    }
    $dirPath .= '/' . date('Y');
    if (!is_dir($dirPath)) {
        @mkdir($dirPath);
    }
    $dirPath .= '/' . date('n');
    if (!is_dir($dirPath)) {
        @mkdir($dirPath);
    }
    /**
     * 第二部分
     */
    file_put_contents($dirPath . '/' . date('j') . '.txt', "\n\n" . date('Y-m-d H:i:s') . "\n", FILE_APPEND);
    file_put_contents($dirPath . '/' . date('j') . '.txt', "result:", FILE_APPEND);
    file_put_contents($dirPath . '/' . date('j') . '.txt', "\n", FILE_APPEND);
    file_put_contents($dirPath . '/' . date('j') . '.txt', print_r($error, TRUE), FILE_APPEND);
    /**
     * 第三部分
     */
    $traces = debug_backtrace();
    $msg = '';
    foreach ($traces as $trace) {
        if (isset($trace['file'], $trace['line']) && strpos($trace['file'], YII_PATH) !== 0) {
            $msg .= "\nin " . $trace['file'] . ' (' . $trace['line'] . ')';
        }
    }
    file_put_contents($dirPath . '/' . date('j') . '.txt', print_r($msg, TRUE), FILE_APPEND);
}

第一部分:保存错误日志的路径与目录格式(随意制定),我这里是存在yii框架下protected/runtime/logs/yyyy/MM/dd.txt中

第二部分:错误信息,记录错误信息时间及原因

第三部分:保存回溯路径,便于更好地帮助我们找到错误,究竟是框架使用错误还是代码错误,同时也可以快速找到错误地点进行分析,毕竟一个做应用的程序员10%coding 90%debuging...233333

 

上面做一个简短的小玩笑,希望可以帮助的到大家,也希望大家有更好的方案可以一起分享

你可能感兴趣的:(php)