-
Notifications
You must be signed in to change notification settings - Fork 0
/
TingSystemLog.class.php
55 lines (48 loc) · 1.36 KB
/
TingSystemLog.class.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
<?php
/**
* @file TingSystemLog.class.php
* @brief 通用的日志系统
* @author sunhuai([email protected])
* @version
* @date 2012-12-28 11:01:22
*/
class TingSystemLog {
private $parentDir = '';
private $logDir = '';
private $logFile = '';
private $handle = '';
public function __construct($fileDir) {
$this->parentDir = ROOT_PATH . '/data/ting/systemLog';
$this->logDir = $this->parentDir . '/' . $fileDir;
$this->logFile = $this->logDir . '/' . date('Y-m-d') . '.txt';
$this->createDir($this->parentDir);
$this->createDir($this->logDir);
$this->handle = fopen($this->logFile, 'ab');
}
public function createDir($dir) {
if (is_dir($dir)) {
return TRUE;
}
if (mkdir($dir)) {
return TRUE;
}
return FALSE;
}
public function writeLog($words = '', $opt=array()) {
$words = date('Y-m-d H:i:s') . "\t" . $words;
if (!empty($opt)) {
$words .= "\t" . json_encode($opt);
}
$words .= "\r\n";
return $this->_write($words);
}
private function _write($words) {
if (!$this->handle) {
return FALSE;
}
return fwrite($this->handle, $words);
}
public function __destruct() {
$this->handle ? fclose($this->handle) : '';
}
}