php日志输出

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

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

<?php

/**
 * @param type $error
 */
function writeLog($error)
{
    /**
     * 第一部分路径
     */
    $dirPath = Yii::app()->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

 

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值