1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Kore : Simple And Minimal Framework |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Kore; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Log class |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class Log |
14
|
|
|
{ |
15
|
|
|
const LEVEL_DEBUG = 10; |
16
|
|
|
const LEVEL_INFO = 20; |
17
|
|
|
const LEVEL_WARN = 30; |
18
|
|
|
const LEVEL_ERROR = 40; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* log name |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected static $logName; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* log level |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected static $logLevel; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize |
36
|
|
|
* |
37
|
|
|
* @param string $logName log name, the default is 'app' |
38
|
|
|
* @param int $logLevel log level, the default is LEVEL_DEBUG |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public static function init($logName = 'app', $logLevel = self::LEVEL_DEBUG) |
42
|
|
|
{ |
43
|
|
|
self::$logName = $logName; |
44
|
|
|
self::$logLevel = $logLevel; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Debug output |
49
|
|
|
* |
50
|
|
|
* @param string $msg message |
51
|
|
|
* @param mixed $obj object |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public static function debug($msg, $obj = null) |
55
|
|
|
{ |
56
|
|
|
if (self::$logLevel <= self::LEVEL_DEBUG) { |
57
|
|
|
static::write('DEBUG', $msg, $obj); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Information output |
63
|
|
|
* |
64
|
|
|
* @param string $msg message |
65
|
|
|
* @param mixed $obj object |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public static function info($msg, $obj = null) |
69
|
|
|
{ |
70
|
|
|
if (self::$logLevel <= self::LEVEL_INFO) { |
71
|
|
|
static::write('INFO', $msg, $obj); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Warning output |
77
|
|
|
* |
78
|
|
|
* @param string $msg message |
79
|
|
|
* @param mixed $obj object |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public static function warn($msg, $obj = null) |
83
|
|
|
{ |
84
|
|
|
if (self::$logLevel <= self::LEVEL_WARN) { |
85
|
|
|
static::write('WARN', $msg, $obj); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Error output |
91
|
|
|
* |
92
|
|
|
* @param string $msg message |
93
|
|
|
* @param mixed $obj object |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public static function error($msg, $obj = null) |
97
|
|
|
{ |
98
|
|
|
if (self::$logLevel <= self::LEVEL_ERROR) { |
99
|
|
|
static::write('ERROR', $msg, $obj); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Write logs |
105
|
|
|
* |
106
|
|
|
* @param string $level level name |
107
|
|
|
* @param string $msg message |
108
|
|
|
* @param mixed $obj object |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
protected static function write($level, $msg, $obj) |
112
|
|
|
{ |
113
|
|
|
$logfile = LOGS_DIR.'/'.self::$logName.'-'.date("Y-m-d").'.log'; |
114
|
|
|
$log = static::buildLog($level, $msg, $obj); |
115
|
|
|
file_put_contents($logfile, $log, FILE_APPEND); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Build logs |
120
|
|
|
* |
121
|
|
|
* @param string $level level name |
122
|
|
|
* @param string $msg message |
123
|
|
|
* @param mixed $obj object |
124
|
|
|
* @return string logs |
125
|
|
|
*/ |
126
|
|
|
protected static function buildLog($level, $msg, $obj) |
127
|
|
|
{ |
128
|
|
|
$msg .= PHP_EOL; |
129
|
|
|
if ($obj !== null) { |
130
|
|
|
$msg .= var_export($obj, true).PHP_EOL; |
131
|
|
|
} |
132
|
|
|
// php5.4 or later |
133
|
|
|
//$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
134
|
|
|
$trace = debug_backtrace(false); |
|
|
|
|
135
|
|
|
$caller = isset($trace[3]['class']) ? sprintf('%s%s%s', $trace[3]['class'], $trace[3]['type'], $trace[3]['function']) : ''; |
136
|
|
|
$microtime = explode('.', (string)microtime(true)); |
137
|
|
|
$time = date('Y-m-d H:i:s', (int)$microtime[0]); |
138
|
|
|
if (isset($microtime[1])) { |
139
|
|
|
$time .= '.'.$microtime[1]; |
140
|
|
|
} |
141
|
|
|
$log = sprintf('[%s][%s][%s][%s]%s', $time, getmypid(), $level, $caller, $msg); |
142
|
|
|
return $log; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|