123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- /**
- * 记录日志
- ***/
- defined('InShopNC') or exit('Access Invalid!');
- class scope_trace
- {
- private $mTag;
- private $mStart;
- public function __construct($tag)
- {
- $this->mTag = $tag;
- $this->mStart = microtime(true);
- Log::record("{$this->mTag} begin----------------------------", Log::DEBUG);
- }
- public function __destruct()
- {
- $period = microtime(true) - $this->mStart;
- Log::record(sprintf("{$this->mTag} end time=%.6f----------------------",$period), Log::DEBUG);
- }
- }
- class Log
- {
- //ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
- 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 $mShortName;
- 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;
- $this->mShortName = empty(APP_ID) ? '' : APP_ID;
- }
- public static function instance()
- {
- if (self::$stInstance == null) {
- self::$stInstance = new Log();
- }
- return self::$stInstance;
- }
- public static function short_name($name)
- {
- $pThis = self::instance();
- $pThis->mShortName = $name;
- }
- 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 set_level($level) {
- self::instance()->mCurLevel = $level;
- }
- public static function enable_sql(bool $enable) {
- self::instance()->mOpenSql = $enable;
- }
- 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)
- {
- [$micro,$time] = explode(' ',microtime());
- $now = @date('Y-m-d H:i:s', $time);
- $now .= sprintf(" %.6f",$micro);
- if(defined('USE_COROUTINE') && USE_COROUTINE === true) {
- $pid = getmypid();
- $cid = Swoole\Coroutine::getCid();
- $pid = "{$pid}-{$cid}";
- }
- else {
- $pid = posix_getpid();
- }
- $content = "[{$this->mShortName} {$pid} {$now}] {$level}: {$message}\r\n";
- return $content;
- }
- private function write($content)
- {
- $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '-' . $this->mShortName . '.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);
- }
- else {
- exit();
- }
- }
- 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);
- }
- }
- 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);
- }
- }
|