ServiceLog.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. list($this->_logModelName,$this->_logModel) = Yii::mapGet($this->_logModelName);
  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. /**
  60. * ServiceLog:保存serviceLog.
  61. */
  62. public function printServiceLog($log_info)
  63. {
  64. if ($this->isServiceLogDbPrint()) {
  65. $this->_logModel->getCollection()->save($log_info);
  66. }
  67. if ($this->isServiceLogHtmlPrint() || $this->isServiceLogDbPrintByParam()) {
  68. $str = '<br>#################################<br><table>';
  69. foreach ($log_info as $k=>$v) {
  70. if (is_array($v)) {
  71. $v = implode('<br>', $v);
  72. $str .= "<tr>
  73. <td>$k</td><td>$v</td>
  74. </tr>";
  75. } else {
  76. $str .= "<tr>
  77. <td>$k</td><td>$v</td>
  78. </tr>";
  79. }
  80. }
  81. $str .= '</table><br>#################################<br><br>';
  82. $this->serviceLogHtmlPrintStr .= $str;
  83. }
  84. }
  85. // 直接在前端打印service Log
  86. public function getServiceLogHtmlPrintStr(){
  87. if ($this->isServiceLogEnable()) {
  88. return $this->serviceLogHtmlPrintStr;
  89. } else {
  90. return '';
  91. }
  92. }
  93. /**
  94. * ServiceLog:if service log db print is enable.
  95. */
  96. protected function isServiceLogDbPrint()
  97. {
  98. if ($this->_isServiceLogDbPrint === null) {
  99. if (
  100. isset($this->log_config['services']['enable'])
  101. && $this->log_config['services']['enable']
  102. && isset($this->log_config['services']['dbprint'])
  103. && $this->log_config['services']['dbprint']
  104. ) {
  105. $this->_isServiceLogDbPrint = true;
  106. } else {
  107. $this->_isServiceLogDbPrint = false;
  108. }
  109. }
  110. return $this->_isServiceLogDbPrint;
  111. }
  112. /**
  113. * ServiceLog:在前台打印servicelog是否开启.
  114. */
  115. protected function isServiceLogHtmlPrint()
  116. {
  117. if ($this->_isServiceLogHtmlPrint === null) {
  118. if (
  119. isset($this->log_config['services']['enable'])
  120. && $this->log_config['services']['enable']
  121. && isset($this->log_config['services']['htmlprint'])
  122. && $this->log_config['services']['htmlprint']
  123. ) {
  124. $this->_isServiceLogHtmlPrint = true;
  125. } else {
  126. $this->_isServiceLogHtmlPrint = false;
  127. }
  128. }
  129. return $this->_isServiceLogHtmlPrint;
  130. }
  131. /**
  132. * ServiceLog:通过参数,在前台打印servicelog是否开启.
  133. */
  134. protected function isServiceLogDbPrintByParam()
  135. {
  136. if ($this->_isServiceLogDbPrintByParam === null) {
  137. $this->_isServiceLogDbPrintByParam = false;
  138. if (
  139. isset($this->log_config['services']['enable'])
  140. && $this->log_config['services']['enable']
  141. && isset($this->log_config['services']['htmlprintbyparam']['enable'])
  142. && $this->log_config['services']['htmlprintbyparam']['enable']
  143. && isset($this->log_config['services']['htmlprintbyparam']['paramVal'])
  144. && ($paramVal = $this->log_config['services']['htmlprintbyparam']['paramVal'])
  145. && isset($this->log_config['services']['htmlprintbyparam']['paramKey'])
  146. && ($paramKey = $this->log_config['services']['htmlprintbyparam']['paramKey'])
  147. ) {
  148. if (Yii::$app->request->get($paramKey) == $paramVal) {
  149. $this->_isServiceLogDbPrintByParam = true;
  150. }
  151. }
  152. }
  153. return $this->_isServiceLogDbPrintByParam;
  154. }
  155. /**
  156. * generate uuid .
  157. */
  158. protected function guid()
  159. {
  160. if (function_exists('com_create_guid')) {
  161. return com_create_guid();
  162. } else {
  163. mt_srand((float) microtime() * 10000); //optional for php 4.2.0 and up.
  164. $charid = strtoupper(md5(uniqid(rand(), true)));
  165. $hyphen = chr(45); // "-"
  166. $uuid = //chr(123)// "{"
  167. substr($charid, 0, 8).$hyphen
  168. .substr($charid, 8, 4).$hyphen
  169. .substr($charid, 12, 4).$hyphen
  170. .substr($charid, 16, 4).$hyphen
  171. .substr($charid, 20, 12)
  172. //.chr(125)// "}"
  173. ;
  174. return $uuid;
  175. }
  176. }
  177. }