zoukankan      html  css  js  c++  java
  • discuz函数解析--写日志

        public static function writelog($file, $log) {
            global $_G;
            $yearmonth = dgmdate(TIMESTAMP, 'Ym', $_G['setting']['timeoffset']);
            $logdir = DISCUZ_ROOT.'./data/log/';
            $logfile = $logdir.$yearmonth.'_'.$file.'.php';
            if(@filesize($logfile) > 2048000) { //判断文件大小,如果超过2M,则将日志文件重命名
                $dir = opendir($logdir);
                $length = strlen($file);
                $maxid = $id = 0;
                while($entry = readdir($dir)) {
                    if(strpos($entry, $yearmonth.'_'.$file) !== false) {
                        $id = intval(substr($entry, $length + 8, -4));
                        $id > $maxid && $maxid = $id;
                    }
                }
                closedir($dir);
    
                $logfilebak = $logdir.$yearmonth.'_'.$file.'_'.($maxid + 1).'.php';
                @rename($logfile, $logfilebak);
            }
            if($fp = @fopen($logfile, 'a')) {
                @flock($fp, 2);//文件锁
                if(!is_array($log)) {
                    $log = array($log);
                }
                foreach($log as $tmp) {
                    fwrite($fp, "<?PHP exit;?>	".str_replace(array('<?', '?>'), '', $tmp)."
    ");
                }
                fclose($fp);
            }
        }

    dz的写日志函数,重用性还可以。

  • 相关阅读:
    每日博客
    每日博客
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4980827.html
Copyright © 2011-2022 走看看