log.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 record($message, $lev = self::ERR)
  90. {
  91. self::instance()->doRecord($message, $lev);
  92. }
  93. private function doRecord($message, $lev = self::ERR)
  94. {
  95. $slevel = $this->get_level($lev);
  96. $content = $this->format_msg($message,$slevel);
  97. if ($lev == self::SQL && $this->mOpenSql) {
  98. $this->write($content);
  99. if($this->mOpenAll) $this->write_all($content);
  100. }
  101. elseif ($lev >= $this->mCurLevel && $lev <= self::RUN) {
  102. $this->write($content);
  103. if($this->mOpenAll) $this->write_all($content);
  104. }
  105. if ($lev == self::ERR) {
  106. $msg = $this->msg();
  107. $content = $this->format_msg($msg,$slevel);
  108. $this->write($content);
  109. if($this->mOpenAll) $this->write_all($content);
  110. }
  111. }
  112. private function get_level($lev)
  113. {
  114. if ($lev == self::SQL) return 'SQL';
  115. if ($lev == self::INFO) return 'INFO';
  116. if ($lev == self::DEBUG) return 'DEBUG';
  117. if ($lev == self::WARING) return 'WARING';
  118. if ($lev == self::ERR) return 'ERR';
  119. if ($lev == self::RUN) return 'RUN';
  120. return 'Unknown';
  121. }
  122. private function format_msg($message,$level)
  123. {
  124. [$micro,$time] = explode(' ',microtime());
  125. $now = @date('Y-m-d H:i:s', $time);
  126. $now .= sprintf(" %.6f",$micro);
  127. if(defined('USE_COROUTINE') && USE_COROUTINE === true) {
  128. $pid = getmypid();
  129. $cid = Swoole\Coroutine::getCid();
  130. $pid = "{$pid}-{$cid}";
  131. }
  132. else {
  133. $pid = posix_getpid();
  134. }
  135. $appid = empty(APP_ID) ? '' : APP_ID;
  136. $content = "[{$appid} {$pid} {$now}] {$level}: {$message}\r\n";
  137. return $content;
  138. }
  139. private function write($content)
  140. {
  141. $appid = empty(APP_ID) ? '' : APP_ID;
  142. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '-' . $appid . '.log';
  143. if ($this->mAppFileName != $log_file)
  144. {
  145. if ($this->mAppFile !== false) {
  146. fclose($this->mAppFile);
  147. }
  148. $this->mAppFileName = $log_file;
  149. $this->mAppFile = fopen($log_file, 'a+');
  150. }
  151. if ($this->mAppFile !== false) {
  152. fwrite($this->mAppFile, $content);
  153. }
  154. }
  155. private function write_all($content)
  156. {
  157. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  158. if ($this->mAllFileName != $log_file)
  159. {
  160. if ($this->mAllFile !== false) {
  161. fclose($this->mAllFile);
  162. }
  163. $this->mAllFileName = $log_file;
  164. $this->mAllFile = fopen($log_file, 'a+');
  165. }
  166. if ($this->mAllFile !== false) {
  167. fwrite($this->mAllFile, $content);
  168. }
  169. }
  170. private function msg()
  171. {
  172. $debugInfo = debug_backtrace();
  173. $stack = "\t[";
  174. foreach ($debugInfo as $key => $val) {
  175. if (array_key_exists("file", $val)) {
  176. $stack .= "\tfile:" . $val["file"];
  177. }
  178. if (array_key_exists("line", $val)) {
  179. $stack .= ",line:" . $val["line"];
  180. }
  181. if (array_key_exists("function", $val)) {
  182. $stack .= ",function:" . $val["function"];
  183. }
  184. $stack .= "\r\n";
  185. }
  186. $stack .= "]";
  187. return $stack;
  188. }
  189. private function doRecordPath($content)
  190. {
  191. $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '_path.log';
  192. if ($this->mPathFileName != $path_file)
  193. {
  194. if ($this->mPathFile !== false) {
  195. fclose($this->mPathFile);
  196. }
  197. $this->mPathFile = fopen($path_file, 'a');
  198. }
  199. if($this->mPathFile !== false)
  200. {
  201. if (@flock($this->mPathFile, LOCK_EX)) {
  202. fwrite($this->mPathFile, $content);
  203. fwrite($this->mPathFile, "\r\n");
  204. @flock($this->mPathFile, LOCK_UN);
  205. }
  206. }
  207. }
  208. public static function record_path($content)
  209. {
  210. $pThis = self::instance();
  211. $pThis->doRecordPath($content);
  212. }
  213. }