ServiceLog.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * FecShop file.
  4. * @link http://www.fecshop.com/
  5. * @copyright Copyright (c) 2016 FecShop Software LLC
  6. * @license http://www.fecshop.com/license/
  7. */
  8. namespace fecshop\components;
  9. use Yii;
  10. use yii\base\BootstrapInterface;
  11. use yii\base\Component;
  12. /**
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class ServiceLog extends Component
  17. {
  18. public $log_config;
  19. protected $_serviceContent;
  20. protected $_serviceUid;
  21. protected $_isServiceLog;
  22. protected $_isServiceLogDbPrint;
  23. protected $_isServiceLogHtmlPrint;
  24. protected $_isServiceLogDbPrintByParam;
  25. protected $_logModelName = '\fecshop\models\mongodb\FecshopServiceLog';
  26. protected $_logModel;
  27. //public function init(){
  28. // parent::init();
  29. //
  30. //}
  31. /**
  32. * Log:get log uuid .
  33. */
  34. public function getLogUid()
  35. {
  36. if (!$this->_serviceUid) {
  37. $this->_serviceUid = $this->guid();
  38. }
  39. return $this->_serviceUid;
  40. }
  41. /**
  42. * ServiceLog:是否开启service log.
  43. */
  44. public function isServiceLogEnable()
  45. {
  46. if ($this->_isServiceLog === null) {
  47. if (
  48. isset($this->log_config['services']['enable'])
  49. && $this->log_config['services']['enable']
  50. ) {
  51. $this->_isServiceLog = true;
  52. } else {
  53. $this->_isServiceLog = false;
  54. }
  55. }
  56. return $this->_isServiceLog;
  57. }
  58. public $serviceLogHtmlPrintStr;
  59. public function initServiceLogDbPrint()
  60. {
  61. if (!$this->_logModel) {
  62. list($this->_logModelName,$this->_logModel) = Yii::mapGet($this->_logModelName);
  63. }
  64. }
  65. /**
  66. * ServiceLog:保存serviceLog.
  67. */
  68. public function printServiceLog($log_info)
  69. {
  70. if ($this->isServiceLogDbPrint()) {
  71. $this->initServiceLogDbPrint();
  72. $this->_logModel->getCollection()->save($log_info);
  73. }
  74. if ($this->isServiceLogHtmlPrint() || $this->isServiceLogDbPrintByParam()) {
  75. $str = '<br>#################################<br><table>';
  76. foreach ($log_info as $k=>$v) {
  77. if (is_array($v)) {
  78. $v = implode('<br>', $v);
  79. $str .= "<tr>
  80. <td>$k</td><td>$v</td>
  81. </tr>";
  82. } else {
  83. $str .= "<tr>
  84. <td>$k</td><td>$v</td>
  85. </tr>";
  86. }
  87. }
  88. $str .= '</table><br>#################################<br><br>';
  89. $this->serviceLogHtmlPrintStr .= $str;
  90. }
  91. }
  92. // 直接在前端打印service Log
  93. public function getServiceLogHtmlPrintStr(){
  94. if ($this->isServiceLogEnable()) {
  95. return $this->serviceLogHtmlPrintStr;
  96. } else {
  97. return '';
  98. }
  99. }
  100. /**
  101. * ServiceLog:if service log db print is enable.
  102. */
  103. protected function isServiceLogDbPrint()
  104. {
  105. if ($this->_isServiceLogDbPrint === null) {
  106. if (
  107. isset($this->log_config['services']['enable'])
  108. && $this->log_config['services']['enable']
  109. && isset($this->log_config['services']['dbprint'])
  110. && $this->log_config['services']['dbprint']
  111. ) {
  112. $this->_isServiceLogDbPrint = true;
  113. } else {
  114. $this->_isServiceLogDbPrint = false;
  115. }
  116. }
  117. return $this->_isServiceLogDbPrint;
  118. }
  119. /**
  120. * ServiceLog:在前台打印servicelog是否开启.
  121. */
  122. protected function isServiceLogHtmlPrint()
  123. {
  124. if ($this->_isServiceLogHtmlPrint === null) {
  125. if (
  126. isset($this->log_config['services']['enable'])
  127. && $this->log_config['services']['enable']
  128. && isset($this->log_config['services']['htmlprint'])
  129. && $this->log_config['services']['htmlprint']
  130. ) {
  131. $this->_isServiceLogHtmlPrint = true;
  132. } else {
  133. $this->_isServiceLogHtmlPrint = false;
  134. }
  135. }
  136. return $this->_isServiceLogHtmlPrint;
  137. }
  138. /**
  139. * ServiceLog:通过参数,在前台打印servicelog是否开启.
  140. */
  141. protected function isServiceLogDbPrintByParam()
  142. {
  143. if ($this->_isServiceLogDbPrintByParam === null) {
  144. $this->_isServiceLogDbPrintByParam = false;
  145. if (
  146. isset($this->log_config['services']['enable'])
  147. && $this->log_config['services']['enable']
  148. && isset($this->log_config['services']['htmlprintbyparam']['enable'])
  149. && $this->log_config['services']['htmlprintbyparam']['enable']
  150. && isset($this->log_config['services']['htmlprintbyparam']['paramVal'])
  151. && ($paramVal = $this->log_config['services']['htmlprintbyparam']['paramVal'])
  152. && isset($this->log_config['services']['htmlprintbyparam']['paramKey'])
  153. && ($paramKey = $this->log_config['services']['htmlprintbyparam']['paramKey'])
  154. ) {
  155. if (Yii::$app->request->get($paramKey) == $paramVal) {
  156. $this->_isServiceLogDbPrintByParam = true;
  157. }
  158. }
  159. }
  160. return $this->_isServiceLogDbPrintByParam;
  161. }
  162. /**
  163. * generate uuid .
  164. */
  165. protected function guid()
  166. {
  167. if (function_exists('com_create_guid')) {
  168. return com_create_guid();
  169. } else {
  170. mt_srand((float) microtime() * 10000); //optional for php 4.2.0 and up.
  171. $charid = strtoupper(md5(uniqid(rand(), true)));
  172. $hyphen = chr(45); // "-"
  173. $uuid = //chr(123)// "{"
  174. substr($charid, 0, 8).$hyphen
  175. .substr($charid, 8, 4).$hyphen
  176. .substr($charid, 12, 4).$hyphen
  177. .substr($charid, 16, 4).$hyphen
  178. .substr($charid, 20, 12)
  179. //.chr(125)// "}"
  180. ;
  181. return $uuid;
  182. }
  183. }
  184. }