log.php 6.7 KB

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