php错误信息存入日志

日志函数

/**
 * 统一日志记录
 * @author  
 * @date    
 * @param   string  $path  路径
 * @param   string  $content  日志内容
 * @param   string  $mode  记录格式,day=天,month=月,year=年
 * @param   string  $env  请求模式
 *
 * @return  bool
 */
function log($path, $content, $mode = 'day', $env = '')
{
    if (!$content) {
        return false;
    }
    $content = strval($content) . "\n";
    if (PHP_SAPI == 'cli') {
        $path = 'cli/' . $path;
    }
    $path = '/www/wwwroot/log/' . $path . '/';
    if ($mode == 'year') {
        $fileName = date('Y') . '.log';
    } else if ($mode == 'month') {
        $path = $path . date('Y');
        $fileName = '/' . date('m') . '.log';
    } else {
        $path = $path . date('Y') . '/' . date('m') . '/';
        $fileName =  date('d') . '.log';
    }
    if (!is_dir($path)) {
        $res = mkdir($path, 0777, true);
        if (!$res) {
            return false;
        }
    }
    $file = $path . $fileName;
    $content = date('Y-m-d H:i:s') . ':' . $content;
    $res = file_put_contents($file, $content, FILE_APPEND);
    if ($res) {
        return true;
    } else {
        return false;
    }
}

调用示例


 Db::startTrans();
 try {
	//sql操作
     Db::commit();
 } catch (\Exception $e) {
     Db::rollback();
     log('admin', $e->getMessage());
 }
发布了65 篇原创文章 · 获赞 20 · 访问量 2101

猜你喜欢

转载自blog.csdn.net/weixin_43993175/article/details/103991424