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