log.php 6.3 KB

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