-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.class.php
More file actions
176 lines (161 loc) · 3.04 KB
/
log.class.php
File metadata and controls
176 lines (161 loc) · 3.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
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
<?php
/**
* Logger
* ----
*
*/
require_once 'log/handler.class.php';
/**
* Logger Class
*
* @see sample/sample.php
*/
class NyaaLog
{
const DEBUG = 1;
const INFO = 2;
const NOTICE = 4;
const WARNING = 8;
const ERROR = 16;
const ALL = 31;
private $name = 'default';
private $template = '[%name] %date | %type %log';
private $dateFormat = 'Y/m/d G-i-s';
private $reporting = self::ALL;
private $allowProp = array('reporting', 'name', 'template', 'dateFormat');
private $handlers = array( );
static $loggers = array( );
public function __construct( )
{
$this->typeNames = array(
self::DEBUG => 'debug',
self::INFO => 'info',
self::NOTICE => 'notice',
self::WARNING => 'warning',
self::ERROR => 'error'
);
}
static function addStack( $logger )
{
self::$loggers[] = $logger;
}
static function getStack( )
{
if(count(self::$loggers) > 0)
{
return self::$loggers[(count(self::$loggers))-1];
}
return false;
}
/**
* @params $lv, $handler
*/
public function addHandler( $lv, $handler )
{
$this->handlers[] = array($lv, $handler);
}
/**
* Set Propary
*
* @params $key, $value
*/
public function set( $key, $value )
{
if( in_array( $key, $this->allowProp ) )
{
$this->$key = $value;
}
}
/**
* Get Propary
*
* @params $key, $value
*/
public function get( $key )
{
if( in_array( $key, $this->allowProp ) )
{
return $this->$key;
}
}
/**
* Logging trigger
*
* @params $lv, $string
*/
public function log( $lv, $string )
{
if( 0 == ($lv & $this->reporting ) )
{
return false;
}
if(func_num_args() > 2)
{
$args = func_get_args();
$string = vsprintf( $string, array_slice( $args, 2 ) );
}
foreach( $this->handlers as $h )
{
$targLevel = $h[0];
$handler = $h[1];
if( $lv & $targLevel )
{
$handler->process( $lv, $string );
}
}
}
/**
* used for build log sentence
*
* @params $key, $string
*/
public function getLogParts( $key, $lv, $string )
{
switch( $key )
{
case 'name' : return $this->name;
case 'date' : return date($this->dateFormat);
case 'log' : return $string;
case 'type' : return isset($this->typeNames[$lv]) ?
$this->typeNames[$lv] : "undefind($lv)";
}
return '%'.$key;
}
/**
* @params $func string, $args array
*/
public function __call( $func, $args )
{
if( in_array( $func, array('debug', 'info', 'notice', 'warning', 'error') ) )
{
switch( $func )
{
case 'debug': $lv = self::DEBUG; break;
case 'info': $lv = self::INFO; break;
case 'notice': $lv = self::NOTICE; break;
case 'warning': $lv = self::WARNING; break;
case 'error': $lv = self::ERROR; break;
}
array_unshift($args, $lv);
call_user_func_array( array($this,'log'), $args);
}
}
/**
* Factory Of Log Handler
*
* @params $name
*/
public function createHandler( $name )
{
$Handler = NyaaLogHandler::factory( $name, $this );
return $Handler;
}
/**
* Clear All Handlers
*/
public function clearHandlers( )
{
$this->handlers = array( );
}
}
?>