-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.php
198 lines (176 loc) · 4.29 KB
/
Logger.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
<?php
namespace PhpLog;
use PhpLog\Library\Common;
use PhpLog\Library\Constant;
use PhpLog\Library\Content;
use PhpLog\Library\Formatter;
use PhpLog\Virtual\ILog;
use PhpLog\Storage\FileHandle;
use PhpLog\Storage\RedisHandle;
use PhpLog\Virtual\IHandle;
date_default_timezone_set('PRC');
defined('PHP_LOG_ROOT_DIR') or define('PHP_LOG_ROOT_DIR',dirname(__FILE__).'/');
require_once PHP_LOG_ROOT_DIR.'init.php';
/**
* User: ambi
* Date: 2017/4/9
*/
class Logger implements ILog {
private $formatter;
private $storage;
private $startTransaction = false;
private $level = 0;
public function __construct($filePath){
$this->storage = new Storage($filePath);
$this->formatter = new Formatter("[%date%][%point%][%pid%] %message%");
$this->formatter->setDateFormat('Y-m-d H:i:s');
}
/**
* @param Formatter $formatter
* 设置格式
*/
public function setFormatter(Formatter $formatter){
$this->formatter = $formatter;
}
/**
* @param $type
* @return bool
* 判断当前日志级别是否可写
*/
private function isWrite($type){
return $this->level < Constant::LEVEL[$type];
}
/**
* @param $message
* @param $type
* @return bool
* 预处理消息体
*/
private function preWrite($message, $type){
if(!$this->isWrite($type)){
return true;
}
$content = new Content($this->formatter);
$data = array(
'point'=>Common::getRecordPoint(),
'type'=>$type,
'pid'=>Common::getProcessId(),
'message'=>$message,
'date'=>date($this->formatter->getDateFormat())
);
$content->setData($data);
$this->storage->push($content);
$this->write();
return true;
}
/**
* 写入载体
*/
private function write(){
if(!$this->startTransaction){
$this->storage->write();
}
}
/**
* @param $level
* 设置日志级别 此级别以上日志会被记录
*/
public function setLevel($level){
$this->level = $level;
}
/**
* @param $message
* write a log which type is debug
*/
public function debug($message){
$this->preWrite($message, Constant::DEBUG);
}
/**
* @param $message
* write a log which type is info
*/
public function info($message){
$this->preWrite($message,Constant::INFO);
}
/**
* @param $message
* write a log which type is warning
*/
public function warning($message)
{
$this->preWrite($message,Constant::WARNING);
}
/**
* @param $message
* write a log which type is error
*/
public function error($message){
$this->preWrite($message,Constant::ERROR);
}
/**
* @param $message
* write a log which type is exception
*/
public function exception($message)
{
$this->preWrite($message,Constant::EXCEPTION);
}
/**
* @param $message
* write a log which type is alert
*/
public function alert($message){
$this->preWrite($message,Constant::ALERT);
}
/**
* start a transaction
*/
public function startTransaction(){
$this->startTransaction = true;
}
/**
* submit current transaction
*/
public function submit(){
$this->startTransaction = false;
$this->storage->write();
}
/**
* cancel current transaction
*/
public function cancel(){
$this->storage->clear();
}
}
class Storage implements IHandle{
private $handle;
public function __construct($name){
switch (strtolower(PHP_LOG_STORAGE_TYPE)){
case 'redis':
$this->handle = new RedisHandle($name);
break;
default :
$this->handle = new FileHandle($name);
break;
}
}
/**
* clear stream on storage
*/
public function clear(){
$this->handle->clear();
}
/**
* @param $message
* push a log stream to sotorage
*/
public function push($message){
$this->handle->push($message);
}
/**
* write log
*/
public function write(){
$this->handle->write();
}
}