用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

记录各种日志类

2016-07-08 作者: 叶冷漠举报

[php]代码库

/**
 * 功能描述:记录各种日志
 */
class Log
{
    var $MLogPath = LOG_PATH;
    var $MLogTitle;


    // 静态函数日志记录

    static function LOG($type, $msg, $path = '')
    {
        $user = trim(@shell_exec("whoami"));
        $path = date('Ym') . "/";
        if ($user == 'root') {
            $path = date('Ym') . "-root/";
        }
        if (php_sapi_name() == "cli") {
            $path .= 'cli';
        } else {
            $uri = $_SERVER['REQUEST_URI'];
            if (strpos($uri, "/admin/") !== false) {
                $path .= "admin";
            } else {
                $path .= "front";
            }
        }
        $log = new Log();
        $log->log_by_type($type, $msg, $path);
    }

    /**
     * 功能描述:构造函数
     */
    function __construct($selfpath = '')
    {
        if (!empty($selfpath)) {
            Dir::checkDirectory($selfpath, true);
        }
        $this->MLogPath = $selfpath ? $selfpath : $this->MLogPath;
        $this->time = date('Y-m-d H:i:s', time());
        $this->ip = $_SERVER["REMOTE_ADDR"];
        $this->pid = getmypid();
    }

    /**
     * 功能描述:按时间记录日志
     */
    function log_by_date($profix, $msg, $path = '')
    {
        $this->MLogTitle = $profix . date('Y-m-d', time()) . ".txt";
        if ($path) {
            $this->MLogPath .= $path;
            if (!is_dir($this->MLogPath)) {
                mkdir($this->MLogPath);
            }
        }
        //$this->MLogPath .= '/';
        $logFile = $this->MLogPath . $this->MLogTitle;
        $fp = fopen($logFile, "a+");
        $msg = $this->time . "\t" . $this->ip . "\t" . $msg . "\n\n";
        fwrite($fp, $msg);
        fflush($fp);
        fclose($fp);
    }

    /**
     * 功能描述:按类型记录日志
     */
    function log_by_type($type, $msg, $path = '')
    {
        $this->MLogPath = ROOTDIR ."log/";
        $this->MLogTitle = $type."_".date('Y-m-d').".txt";
        if($path)
        {
            $arr = explode("/",$path);
            foreach($arr as $name){
                $this->MLogPath .= $name."/";
                if(!file_exists($this->MLogPath)){
                    mkdir($this->MLogPath);
                }
            }
        }

        $pre_utf_header = "";
        if(!file_exists($this->MLogPath.$this->MLogTitle))
            $pre_utf_header = "\xEF\xBB\xBF";
        $fp = fopen($this->MLogPath.$this->MLogTitle,"a+");
        $msg=$this->pid."\t".$this->time."\t".$this->ip."\t".$msg."\r\n";
        fwrite($fp,$pre_utf_header . $msg);
        fflush($fp);
        fclose($fp);
    }

    public function log_by_dateEx($msg)
    {
        $now = date("Y-m-d H:i:s");
        $cur_year = "year" . date("Y", time());
        $cur_month = "month" . date("m", time());
        $cur_day = "day" . date("d", time());

        $msg = $this->time . "\t" . $this->ip . "\t" . $msg . "\n";
        if (!file_exists($this->MLogPath . $foldername . "/" . $cur_year)) {
            mkdir($this->MLogPath . $foldername . "/" . $cur_year);
        }
        if (!file_exists($this->MLogPath . $foldername . "/" . $cur_year . "/" . $cur_month)) {
            mkdir($this->MLogPath . $foldername . "/" . $cur_year . "/" . $cur_month);
        }
        $fp = fopen($this->MLogPath . $foldername . "/" . $cur_year . "/" . $cur_month . "/" . $cur_day . ".txt", "a+");
        fwrite($fp, $msg);
        fclose($fp);
    }


}


使用:Log::LOG('erp_api', "send_trade_order");


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...