log.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * 记录日志
  4. ***/
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class scope_trace
  7. {
  8. private $mTag;
  9. private $mStart;
  10. public function __construct($tag)
  11. {
  12. $this->mTag = $tag;
  13. $this->mStart = microtime(true);
  14. Log::record("{$this->mTag} begin----------------------------", Log::DEBUG);
  15. }
  16. public function __destruct()
  17. {
  18. $period = microtime(true) - $this->mStart;
  19. Log::record(sprintf("{$this->mTag} end time=%.6f----------------------",$period), Log::DEBUG);
  20. }
  21. }
  22. class Log
  23. {
  24. const SQL = 1;
  25. const INFO = 2;
  26. const DEBUG = 3;
  27. const WARING = 4;
  28. const ERR = 5;
  29. const RUN = 6;
  30. const WAIT_HANDLE = 10;
  31. private $mSqlog;
  32. private $mAppFileName;
  33. private $mAppFile;
  34. private $mOpenAll;
  35. private $mAllFileName;
  36. private $mAllFile;
  37. private $mCurLevel;
  38. private $mOpenSql;
  39. private $mPathFileName;
  40. private $mPathFile;
  41. private static $stInstance = null;
  42. private function __construct()
  43. {
  44. $this->mSqlog = false;
  45. $this->mAppFile = false;
  46. $this->mAppFileName = '';
  47. $this->mOpenAll = false;
  48. $this->mAllFile = false;
  49. $this->mAllFileName = '';
  50. $this->mCurLevel = self::DEBUG;
  51. $this->mOpenSql = true;
  52. $this->mPathFileName = '';
  53. $this->mPathFile = false;
  54. }
  55. public static function instance()
  56. {
  57. if (self::$stInstance == null) {
  58. self::$stInstance = new Log();
  59. }
  60. return self::$stInstance;
  61. }
  62. public static function start_sql_log()
  63. {
  64. $pThis = self::instance();
  65. $pThis->mSqlog = [];
  66. }
  67. public static function sql_log()
  68. {
  69. $pThis = self::instance();
  70. if (is_array($pThis->mSqlog)) {
  71. return $pThis->mSqlog;
  72. } else {
  73. return [];
  74. }
  75. }
  76. public static function end_sql_log()
  77. {
  78. $pThis = self::instance();
  79. $pThis->mSqlog = [];
  80. }
  81. private static function add_sql_log($log)
  82. {
  83. $pThis = self::instance();
  84. if (is_array($pThis->mSqlog)) {
  85. $pThis->mSqlog[] = $log;
  86. }
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  89. public static function set_level($level) {
  90. self::instance()->mCurLevel = $level;
  91. }
  92. public static function enable_sql(bool $enable) {
  93. self::instance()->mOpenSql = $enable;
  94. }
  95. public static function record($message, $lev = self::ERR)
  96. {
  97. self::instance()->doRecord($message, $lev);
  98. }
  99. private function doRecord($message, $lev = self::ERR)
  100. {
  101. $slevel = $this->get_level($lev);
  102. $content = $this->format_msg($message,$slevel);
  103. if ($lev == self::SQL && $this->mOpenSql) {
  104. $this->write($content);
  105. if($this->mOpenAll) $this->write_all($content);
  106. }
  107. elseif ($lev >= $this->mCurLevel && $lev <= self::RUN) {
  108. $this->write($content);
  109. if($this->mOpenAll) $this->write_all($content);
  110. }
  111. if ($lev == self::ERR) {
  112. $msg = $this->msg();
  113. $content = $this->format_msg($msg,$slevel);
  114. $this->write($content);
  115. if($this->mOpenAll) $this->write_all($content);
  116. }
  117. }
  118. private function get_level($lev)
  119. {
  120. if ($lev == self::SQL) return 'SQL';
  121. if ($lev == self::INFO) return 'INFO';
  122. if ($lev == self::DEBUG) return 'DEBUG';
  123. if ($lev == self::WARING) return 'WARING';
  124. if ($lev == self::ERR) return 'ERR';
  125. if ($lev == self::RUN) return 'RUN';
  126. return 'Unknown';
  127. }
  128. private function format_msg($message,$level)
  129. {
  130. [$micro,$time] = explode(' ',microtime());
  131. $now = @date('Y-m-d H:i:s', $time);
  132. $now .= sprintf(" %.6f",$micro);
  133. if(defined('USE_COROUTINE') && USE_COROUTINE === true) {
  134. $pid = getmypid();
  135. $cid = Swoole\Coroutine::getCid();
  136. $pid = "{$pid}-{$cid}";
  137. }
  138. else {
  139. $pid = posix_getpid();
  140. }
  141. $appid = empty(APP_ID) ? '' : APP_ID;
  142. $content = "[{$appid} {$pid} {$now}] {$level}: {$message}\r\n";
  143. return $content;
  144. }
  145. private function write($content)
  146. {
  147. $appid = empty(APP_ID) ? '' : APP_ID;
  148. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '-' . $appid . '.log';
  149. if ($this->mAppFileName != $log_file)
  150. {
  151. if ($this->mAppFile !== false) {
  152. fclose($this->mAppFile);
  153. }
  154. $this->mAppFileName = $log_file;
  155. $this->mAppFile = fopen($log_file, 'a+');
  156. }
  157. if ($this->mAppFile !== false) {
  158. fwrite($this->mAppFile, $content);
  159. }
  160. }
  161. private function write_all($content)
  162. {
  163. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  164. if ($this->mAllFileName != $log_file)
  165. {
  166. if ($this->mAllFile !== false) {
  167. fclose($this->mAllFile);
  168. }
  169. $this->mAllFileName = $log_file;
  170. $this->mAllFile = fopen($log_file, 'a+');
  171. }
  172. if ($this->mAllFile !== false) {
  173. fwrite($this->mAllFile, $content);
  174. }
  175. }
  176. private function msg()
  177. {
  178. $debugInfo = debug_backtrace();
  179. $stack = "\t[";
  180. foreach ($debugInfo as $key => $val) {
  181. if (array_key_exists("file", $val)) {
  182. $stack .= "\tfile:" . $val["file"];
  183. }
  184. if (array_key_exists("line", $val)) {
  185. $stack .= ",line:" . $val["line"];
  186. }
  187. if (array_key_exists("function", $val)) {
  188. $stack .= ",function:" . $val["function"];
  189. }
  190. $stack .= "\r\n";
  191. }
  192. $stack .= "]";
  193. return $stack;
  194. }
  195. private function doRecordPath($content)
  196. {
  197. $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '_path.log';
  198. if ($this->mPathFileName != $path_file)
  199. {
  200. if ($this->mPathFile !== false) {
  201. fclose($this->mPathFile);
  202. }
  203. $this->mPathFile = fopen($path_file, 'a');
  204. }
  205. if($this->mPathFile !== false)
  206. {
  207. if (@flock($this->mPathFile, LOCK_EX)) {
  208. fwrite($this->mPathFile, $content);
  209. fwrite($this->mPathFile, "\r\n");
  210. @flock($this->mPathFile, LOCK_UN);
  211. }
  212. }
  213. }
  214. public static function record_path($content)
  215. {
  216. $pThis = self::instance();
  217. $pThis->doRecordPath($content);
  218. }
  219. }