Log.class.php
4.04 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
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think;
/**
* 日志处理类
*/
class Log
{
// 日志级别 从上到下,由低到高
/** 严重错误: 导致系统崩溃无法使用 */
const EMERG = 'EMERG';
/** 警戒性错误: 必须被立即修改的错误 */
const ALERT = 'ALERT';
/** 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样 */
const CRIT = 'CRIT';
/** 一般错误: 一般性错误 */
const ERR = 'ERR';
/** 警告性错误: 需要发出警告的错误 */
const WARN = 'WARN';
/** 通知: 程序可以运行但是还不够完美的错误 */
const NOTICE = 'NOTIC';
/** 信息: 程序输出信息 */
const INFO = 'INFO';
/** 调试: 调试信息 */
const DEBUG = 'DEBUG';
/** SQL:SQL语句 注意只在调试模式开启时有效 */
const SQL = 'SQL';
/** 日志信息 */
protected static $log = array();
/** 日志存储 */
protected static $storage = null;
/**
* 日志初始化
* @param array $config
*/
static public function init($config = array())
{
$type = isset($config['type']) ? $config['type'] : 'File';
$class = strpos($type, '\\') ? $type : 'Think\\Log\\Driver\\' . ucwords(strtolower($type));
unset($config['type']);
self::$storage = new $class($config);
}
/**
* 记录日志 并且会过滤未经设置的级别
*
* @static
*
* @access public
* @param string $message 日志信息
* @param string $level 日志级别
* @param boolean $record 是否强制记录
* @return void
*/
static function record($message, $level = self::ERR, $record = false)
{
if ($record || false !== strpos(C('LOG_LEVEL'), $level)) {
self::$log[] = "{$level}: {$message}\r\n";
}
}
/**
* 日志保存
*
* @static
*
* @access public
* @param integer $type
* 日志记录方式
* @param string $destination
* 写入目标
* @return void
*/
static function save($type = '', $destination = '')
{
if (empty(self::$log)) {
return;
}
if (empty($destination)) {
$destination = C('LOG_PATH') . date('y_m_d') . '.log';
}
if (! self::$storage) {
$type = $type ? : C('LOG_TYPE');
$class = 'Think\\Log\\Driver\\' . ucwords($type);
self::$storage = new $class();
}
$message = implode('', self::$log);
self::$storage->write($message, $destination);
// 保存后清空日志缓存
self::$log = array();
}
/**
* 日志直接写入
*
* @static
*
* @access public
* @param string $message
* 日志信息
* @param string $level
* 日志级别
* @param integer $type
* 日志记录方式
* @param string $destination
* 写入目标
* @return void
*/
static function write($message, $level = self::ERR, $type = '', $destination = '')
{
if (! self::$storage) {
$type = $type ? : C('LOG_TYPE');
$class = 'Think\\Log\\Driver\\' . ucwords($type);
self::$storage = new $class();
}
if (empty($destination)) {
$destination = C('LOG_PATH') . date('y_m_d') . '.log';
}
self::$storage->write("{$level}: {$message}", $destination);
}
}