mTag = $tag; Log::record("{$this->mTag} begin----------------------------", Log::DEBUG); } public function __destruct() { Log::record("{$this->mTag} end ----------------------------", Log::DEBUG); } } class Log { const SQL = 1; const INFO = 2; const DEBUG = 3; const WARING = 4; const ERR = 5; const RUN = 6; const WAIT_HANDLE = 10; private $mSqlog; private $mAppFileName; private $mAppFile; private $mOpenAll; private $mAllFileName; private $mAllFile; private $mCurLevel; private $mOpenSql; private $mPathFileName; private $mPathFile; private static $stInstance = null; private function __construct() { $this->mSqlog = false; $this->mAppFile = false; $this->mAppFileName = ''; $this->mOpenAll = false; $this->mAllFile = false; $this->mAllFileName = ''; $this->mCurLevel = self::DEBUG; $this->mOpenSql = true; $this->mPathFileName = ''; $this->mPathFile = false; } public static function instance() { if (self::$stInstance == null) { self::$stInstance = new Log(); } return self::$stInstance; } public static function start_sql_log() { $pThis = self::instance(); $pThis->mSqlog = []; } public static function sql_log() { $pThis = self::instance(); if (is_array($pThis->mSqlog)) { return $pThis->mSqlog; } else { return []; } } public static function end_sql_log() { $pThis = self::instance(); $pThis->mSqlog = []; } private static function add_sql_log($log) { $pThis = self::instance(); if (is_array($pThis->mSqlog)) { $pThis->mSqlog[] = $log; } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static function record($message, $lev = self::ERR) { self::instance()->doRecord($message, $lev); } private function doRecord($message, $lev = self::ERR) { $slevel = $this->get_level($lev); $content = $this->format_msg($message,$slevel); if ($lev == self::SQL && $this->mOpenSql) { $this->write($content); if($this->mOpenAll) $this->write_all($content); } elseif ($lev >= $this->mCurLevel && $lev <= self::RUN) { $this->write($content); if($this->mOpenAll) $this->write_all($content); } if ($lev == self::ERR) { $msg = $this->msg(); $content = $this->format_msg($msg,$slevel); $this->write($content); if($this->mOpenAll) $this->write_all($content); } } private function get_level($lev) { if ($lev == self::SQL) return 'SQL'; if ($lev == self::INFO) return 'INFO'; if ($lev == self::DEBUG) return 'DEBUG'; if ($lev == self::WARING) return 'WARING'; if ($lev == self::ERR) return 'ERR'; if ($lev == self::RUN) return 'RUN'; return 'Unknown'; } private function format_msg($message,$level) { $now = @date('Y-m-d H:i:s', time()); if(defined('USE_COROUTINE') && USE_COROUTINE === true) { $pid = getmypid(); $cid = Swoole\Coroutine::getCid(); $pid = "{$pid}-{$cid}"; } else { $pid = posix_getpid(); } $appid = empty(APP_ID) ? '' : APP_ID; $content = "[{$appid} {$pid} {$now}] {$level}: {$message}\r\n"; return $content; } private function write($content) { $appid = empty(APP_ID) ? '' : APP_ID; $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '-' . $appid . '.log'; if ($this->mAppFileName != $log_file) { if ($this->mAppFile !== false) { fclose($this->mAppFile); } $this->mAppFileName = $log_file; $this->mAppFile = fopen($log_file, 'a+'); } if ($this->mAppFile !== false) { fwrite($this->mAppFile, $content); fflush($this->mAppFile); } } private function write_all($content) { $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log'; if ($this->mAllFileName != $log_file) { if ($this->mAllFile !== false) { fclose($this->mAllFile); } $this->mAllFileName = $log_file; $this->mAllFile = fopen($log_file, 'a+'); } if ($this->mAllFile !== false) { fwrite($this->mAllFile, $content); fflush($this->mAllFile); } } private function msg() { $debugInfo = debug_backtrace(); $stack = "\t["; foreach ($debugInfo as $key => $val) { if (array_key_exists("file", $val)) { $stack .= "\tfile:" . $val["file"]; } if (array_key_exists("line", $val)) { $stack .= ",line:" . $val["line"]; } if (array_key_exists("function", $val)) { $stack .= ",function:" . $val["function"]; } $stack .= "\r\n"; } $stack .= "]"; return $stack; } private function doRecordPath($content) { $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '_path.log'; if ($this->mPathFileName != $path_file) { if ($this->mPathFile !== false) { fclose($this->mPathFile); } $this->mPathFile = fopen($path_file, 'a'); } if($this->mPathFile !== false) { if (@flock($this->mPathFile, LOCK_EX)) { fwrite($this->mPathFile, $content); fwrite($this->mPathFile, "\r\n"); @flock($this->mPathFile, LOCK_UN); } } } public static function record_path($content) { $pThis = self::instance(); $pThis->doRecordPath($content); } }