Repository navigation
Expand file tree
/
Copy pathWpf_Logger.php
More file actions
145 lines (122 loc) · 3.54 KB
/
Copy pathWpf_Logger.php
File metadata and controls
145 lines (122 loc) · 3.54 KB
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
<?php
/**
* Created by PhpStorm.
* User: zhangjun
* Date: 13/07/2018
* Time: 11:48 AM
*/
class Wpf_Logger
{
private $_level = [
"info",
"warn",
"error",
"sql"
];
private $errorLevel = "error";
private $warnLevel = "warn";
private $infoLevel = "info";
private $sqlLevel = "sql";
private $logType = "";
private $debugMode;
public function __construct()
{
// 禁止在运行目录写Log
// $this->fileName = $this->fileName . "-" . date("Ymd") . ".log";
// $this->filePath = ZalyConfig::getConfig("logPath");
// if($this->filePath == ".") {
// $this->filePath = dirname(__DIR__)."/../logs/";
// }
// $this->filePath = $this->filePath . "/" . $this->fileName;
// $this->handler = fopen($this->filePath, "a+");
// require ChromePhp
// $path = WPF_LIB_DIR . "/ChromePhp/ChromePhp.php";
// require_once($path);
$this->debugMode = ZalyConfig::getConfig("debugMode");
}
/**
* info log
* @param $tag
* @param $infoMsg
*/
public function info($tag, $infoMsg)
{
$this->logType = $this->infoLevel;
$this->writeLog($tag, $infoMsg);
}
/**
* warn log
* @param $tag
* @param $infoMsg
*/
public function warn($tag, $infoMsg)
{
$this->logType = $this->warnLevel;
$this->writeLog($tag, $infoMsg);
}
/**
* error log
* @param $tag
* @param $infoMsg
*/
public function error($tag, $infoMsg)
{
$this->logType = $this->errorLevel;
$this->writeLog($tag, $infoMsg);
}
/**
* write log
* @param $tag
* @param $msg
*/
private function writeLog($tag, $msg)
{
if (!in_array($this->logType, $this->_level)) {
return;
}
if (is_array($msg)) {
$msg = json_encode($msg);
}
// $requestUri = $_SERVER["REQUEST_URI"];
$content = "$tag $msg \n";
$errorLevel = ($this->logType == $this->errorLevel) ? E_USER_WARNING : E_USER_NOTICE;
if($this->debugMode == true) {
// switch ($errorLevel) {
// case E_USER_WARNING:
// ChromePhp::warn($content);
// break;
// default:
// ChromePhp::log($content);
// break;
// }
trigger_error($content, $errorLevel);
} elseif ($this->logType == $this->errorLevel) {
trigger_error($content, $errorLevel);
}
}
public function writeSqlLog($tag, $sql, $params = [], $startTime = 0)
{
if (is_array($params)) {
$params = json_encode($params);
}
$this->logType = $this->sqlLevel;
$expendTime = microtime(true) - $startTime;
$expendTime = round($expendTime, 3) . "ms";
$content = "{$expendTime} $sql params=$params";
$this->writeLog($tag, $content);
}
public function dbLog($tag, $sql, $params = [], $startTime = 0, $result)
{
if (is_array($params)) {
$params = json_encode($params);
}
if (is_array($result)) {
$result = json_encode($result);
}
$this->logType = $this->sqlLevel;
$expendTime = microtime(true) - $startTime;
$expendTime = round($expendTime, 3) . "ms";
$content = "{$expendTime} $sql params=$params";
$this->writeLog($tag, $content);
}
}