php日志,记录日志

来源:互联网 发布:python 爬虫 慢 编辑:程序博客网 时间:2024/05/17 08:05
开发中有些重要操作时,记录数据库日志同时又要记录文件日志,而且日志不能和站点的其他日志混在一起,所以写了这么个东东
/**日志 * Created by JetBrains PhpStorm. * User: feng * Date: 13-7-25 * Time: 下午3:15 * To change this template use File | Settings | File Templates. */class Log {    /**写入日志     * @param $model 模块名称,目录名,允许字母/数字/下划线/减号     * @param $msg  日志内容,可以为数组     * @param string $suffix    后缀名称,可以为空,允许字母/数字/下划线/减号/英文句号     * @param string $prefix    前缀名称,可以为空,允许字母/数字/下划线/减号/英文句号     * @return bool     */    public static function Write($model,$msg,$suffix='',$prefix=''){        if(empty($msg)){            return false;        }        if(is_array($msg)){            $msg=var_export($msg,true);        }        $msg="\n[time]".date('Y:m:d H:i:s').' '.$msg;        //模块文件夹格式化,允许字母/数字/下划线/减号        $model=preg_replace ("/[^\w-]/i",  "$1",  $model);        $model=empty($model)?'none':$model;        //文件存储地址        $file=WEBROOT.APPPATH.'logs/'.$model.'/';        if(!is_dir($file)){            mkdir($file,0777);        }        $file.=date('Y/');        if(!is_dir($file)){            mkdir($file,0777);        }        $prefix=preg_replace ("/[^\w-\.]/i",  "$1",  $prefix);        $suffix=preg_replace ("/[^\w-\.]/i",  "$1",  $suffix);        $file.=$prefix.date('m-d-').$suffix.'.log';        error_log($msg,3,$file);        return true;    }}

原创粉丝点击