Skip to content

Commit 0485ee9

Browse files
committed
增加log工具类
1 parent 1882e5b commit 0485ee9

File tree

3 files changed

+364
-60
lines changed

3 files changed

+364
-60
lines changed

src/linktool/Json.php

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
1-
<?php
2-
3-
/**
4-
* LinkTool - A useful library for PHP
5-
*
6-
* @author Dong Nan <[email protected]>
7-
* @copyright (c) Dong Nan http://idongnan.cn All rights reserved.
8-
* @link https://github.com/dongnan/LinkTool
9-
* @license BSD (http://opensource.org/licenses/BSD-3-Clause)
10-
*/
11-
12-
namespace linktool;
13-
14-
/**
15-
* Json
16-
* Json 工具类
17-
*
18-
* @author Dong Nan <[email protected]>
19-
* @date 2015-9-1
20-
*/
21-
class Json {
22-
23-
/**
24-
* 数据编译为json字符串
25-
* @param array $data
26-
* @return string
27-
*/
28-
static public function encode($data) {
29-
if (!is_array($data)) {
30-
return $data;
31-
}
32-
33-
$array = Url::encode($data);
34-
$json = json_encode($array);
35-
//将urlendoce后的已经为\"的先转换为",防止被转成\\"
36-
$json = str_replace('%5C%22', '%22', $json);
37-
//将urlencode后的"替换为\"再入库,防止"未转义
38-
$json = str_replace('%22', '%5C%22', $json);
39-
$json = Url::decode($json);
40-
41-
return $json;
42-
}
43-
44-
/**
45-
* json字符串解码为数组
46-
* @param string $data
47-
* @return array
48-
*/
49-
public static function decode($data) {
50-
if (is_array($data)) {
51-
return $data;
52-
} elseif (is_string($data) && in_array($data[0], ['{', '['])) {
53-
$array = json_decode(str_replace(['#', "\r\n", "\r", "\n", "\t"], [':#', '##r#n##', '##r##', '##n##', '##t##'], $data), TRUE);
54-
return String::replace([ '##r#n##', '##r##', '##n##', '##t##', ':#'], [ "\r\n", "\r", "\n", "\t", '#'], $array);
55-
} else {
56-
return $data;
57-
}
58-
}
59-
60-
}
1+
<?php
2+
3+
/**
4+
* LinkTool - A useful library for PHP
5+
*
6+
* @author Dong Nan <[email protected]>
7+
* @copyright (c) Dong Nan http://idongnan.cn All rights reserved.
8+
* @link https://github.com/dongnan/LinkTool
9+
* @license BSD (http://opensource.org/licenses/BSD-3-Clause)
10+
*/
11+
12+
namespace linktool;
13+
14+
/**
15+
* Json 工具类
16+
*
17+
* @author Dong Nan <[email protected]>
18+
* @date 2015-9-1
19+
*/
20+
class Json {
21+
22+
/**
23+
* 数据编译为json字符串
24+
* @param array $data
25+
* @return string
26+
*/
27+
static public function encode($data) {
28+
if (!is_array($data)) {
29+
return $data;
30+
}
31+
32+
$array = Url::encode($data);
33+
$json = json_encode($array);
34+
//将urlendoce后的已经为\"的先转换为",防止被转成\\"
35+
$json = str_replace('%5C%22', '%22', $json);
36+
//将urlencode后的"替换为\"再入库,防止"未转义
37+
$json = str_replace('%22', '%5C%22', $json);
38+
$json = Url::decode($json);
39+
40+
return $json;
41+
}
42+
43+
/**
44+
* json字符串解码为数组
45+
* @param string $data
46+
* @return array
47+
*/
48+
public static function decode($data) {
49+
if (is_array($data)) {
50+
return $data;
51+
} elseif (is_string($data) && in_array($data[0], ['{', '['])) {
52+
$array = json_decode(str_replace(['#', "\r\n", "\r", "\n", "\t"], [':#', '##r#n##', '##r##', '##n##', '##t##'], $data), TRUE);
53+
return String::replace([ '##r#n##', '##r##', '##n##', '##t##', ':#'], [ "\r\n", "\r", "\n", "\t", '#'], $array);
54+
} else {
55+
return $data;
56+
}
57+
}
58+
59+
}

src/linktool/log/File.class.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
/**
4+
* LinkTool - A useful library for PHP
5+
*
6+
* @author Dong Nan <[email protected]>
7+
* @copyright (c) Dong Nan http://idongnan.cn All rights reserved.
8+
* @link https://github.com/dongnan/LinkTool
9+
* @license BSD (http://opensource.org/licenses/BSD-3-Clause)
10+
*/
11+
12+
namespace linktool\log;
13+
14+
use linktool\Json;
15+
use \Exception;
16+
17+
/**
18+
* File
19+
*
20+
* @author DongNan <[email protected]>
21+
* @date 2015-9-1
22+
*/
23+
class File extends Log
24+
{
25+
26+
/**
27+
* 日志名称
28+
* @var string
29+
*/
30+
protected $name;
31+
32+
/**
33+
* 日志文件地址
34+
* @var string
35+
*/
36+
protected $logfile;
37+
38+
/**
39+
* 文件句柄
40+
* @var resource
41+
*/
42+
protected $stream;
43+
44+
/**
45+
* 记录的最小日志等级
46+
* @var int
47+
*/
48+
protected $minlevel;
49+
50+
/**
51+
* 构造函数
52+
* @param string $name
53+
* @param string $path
54+
* @param int $level
55+
*/
56+
public function __construct($name, $path, $level = DEBUG)
57+
{
58+
$this->name = $name;
59+
$this->logfile = $path;
60+
$this->minlevel = $level;
61+
$dir = dirname($this->logfile);
62+
if (!is_dir($dir)) {
63+
mkdir($dir, 0777, true);
64+
}
65+
}
66+
67+
/**
68+
* 析构函数
69+
*/
70+
public function __destruct()
71+
{
72+
$this->close();
73+
}
74+
75+
/**
76+
* 关闭文件句柄
77+
*/
78+
public function close()
79+
{
80+
if (is_resource($this->stream)) {
81+
fclose($this->stream);
82+
}
83+
$this->stream = null;
84+
}
85+
86+
public function log($level, $message, $data = [])
87+
{
88+
//根据最小日志等级记录日志
89+
if ($level < $this->minlevel) {
90+
return;
91+
}
92+
if (!is_resource($this->stream)) {
93+
$this->stream = fopen($this->logfile, 'a');
94+
if (!is_resource($this->stream)) {
95+
throw new Exception("FILE '{$this->logfile}' OPEN ERROR");
96+
}
97+
}
98+
$datetime = date('Y-m-d H:i:s');
99+
$timestamp = microtime(true);
100+
$jsonData = Json::encode($data);
101+
$levelName = isset(self::$levels[$level]) ? self::$levels[$level] : $level;
102+
$output = "[{$datetime}] {$timestamp} {$this->name}.{$levelName}: {$message} {$jsonData}" . PHP_EOL;
103+
fwrite($this->stream, $output);
104+
}
105+
106+
public function alert($message, $data = [])
107+
{
108+
$this->log(ALERT, $message, $data);
109+
}
110+
111+
public function critical($message, $data = [])
112+
{
113+
$this->log(CRITICAL, $message, $data);
114+
}
115+
116+
public function debug($message, $data = [])
117+
{
118+
$this->log(DEBUG, $message, $data);
119+
}
120+
121+
public function emergency($message, $data = [])
122+
{
123+
$this->log(EMERGENCY, $message, $data);
124+
}
125+
126+
public function error($message, $data = [])
127+
{
128+
$this->log(ERROR, $message, $data);
129+
}
130+
131+
public function info($message, $data = [])
132+
{
133+
$this->log(INFO, $message, $data);
134+
}
135+
136+
public function notice($message, $data = [])
137+
{
138+
$this->log(NOTICE, $message, $data);
139+
}
140+
141+
public function warning($message, $data = [])
142+
{
143+
$this->log(WARNING, $message, $data);
144+
}
145+
146+
}

0 commit comments

Comments
 (0)