log.php 6.9 KB

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