Pluf Framework

Pluf Framework Commit Details


Date:2010-02-03 06:16:36 (14 years 10 months ago)
Author:Loic d'Anterroches
Branch:develop, master
Commit:184787dad4902a7b4dc7e43293b950f4e056b058
Parents: a9718072eae01b28a09ca035d02da11bab1ddbe7
Message:Added the log system.

Changes:

File differences

src/Pluf/Log.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2009 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* High performance logging infrastructure.
*
* Logging while keeping a high performance in production is hard, it
* is even harder if we want to track the point in the code where the
* log information is generated, for example the file name and line
* number. PHP offers the assert statement which, used in a not so
* conventional way can get everything in a very efficient way.
*
* Note that the messages do not need to be strings. You can log
* whatever you want. How the message is then stored in your logs is
* up to the writer you are using. This can be for example a JSON
* fragment.
*
* The removal of constraints on the log message simplify the log
* system as you can push into it categories or extra informations.
*
* In the log stack, each log message is microtimed together with the
* log level as integer. You can convert the integer to string at
* write time.
*
*/
class Pluf_Log
{
/**
* The log stack.
*
* A logger function is just pushing the data in the log stack,
* the writers are then called to write the data later.
*/
public static $stack = array();
/**
* Different log levels.
*/
const ALL = 1;
const DEBUG = 5;
const INFO = 6;
const WARN = 7;
const ERROR = 8;
const FATAL = 9;
const OFF = 10;
/**
* Used to reverse the log level to the string.
*/
public static $reverse = array(1 => 'ALL',
5 => 'DEBUG',
6 => 'INFO',
7 => 'WARN',
8 => 'ERROR',
9 => 'FATAL');
/**
* Current log level.
*
* By default, set to 6, which is the INFO level.
*/
public static $level = 6;
/**
* Current message in the assert log.
*/
public static $assert_mess = null;
/**
* Current level of the message in the assert log.
*/
public static $assert_level = 6;
/**
* Log the information in the stack.
*
* Flush the information if needed.
*
* @param $level Level to log
* @param $message Message to log
*/
private static function _log($level, $message)
{
if (self::$level >= $level and self::$level != 10) {
self::$stack[] = array(microtime(true), $level, $message);
if (!Pluf::f('log_delayed', false)) {
self::flush();
}
}
}
/**
* Base assert logger.
*
* The assert logging is a two step process as one need to go
* through the assertion callback.
*
* @param $level Level to log
* @param $message Message to log
* @return bool false
*/
private static function _alog($level, $message)
{
self::$assert_level = $level;
self::$assert_mess = $message;
return false; // This will trigger the assert handler.
}
/**
* Log at the ALL level.
*
* @param $message Message to log
*/
public static function log($message)
{
return self::_log(self::ALL, $message);
}
/**
* Log at the DEBUG level.
*
* @param $message Message to log
*/
public static function debug($message)
{
self::_log(self::DEBUG, $message);
}
public static function info($message)
{
self::_log(self::INFO, $message);
}
public static function warn($message)
{
self::_log(self::WARN, $message);
}
public static function error($message)
{
self::_log(self::ERROR, $message);
}
public static function fatal($message)
{
self::_log(self::FATAL, $message);
}
/**
* Assert log at the ALL level.
*
* @param $message Message to log
*/
public static function alog($message)
{
return self::_alog(self::ALL, $message);
}
/**
* Assert log at the DEBUG level.
*
* @param $message Message to log
*/
public static function adebug($message)
{
self::_alog(self::DEBUG, $message);
}
public static function ainfo($message)
{
self::_alog(self::INFO, $message);
}
public static function awarn($message)
{
self::_alog(self::WARN, $message);
}
public static function aerror($message)
{
self::_alog(self::ERROR, $message);
}
public static function afatal($message)
{
self::_alog(self::FATAL, $message);
}
/**
* Flush the data to the writer.
*
* This reset the stack.
*/
public static function flush()
{
$writer = Pluf::f('log_handler', 'Pluf_Log_File');
call_user_func(array($writer, 'write'), self::$stack);
self::$stack = array();
}
/**
* Signal handler to flush the log.
*
* The name of the signal and the parameters are not used.
*
* @param $signal Name of the signal
* @param &$params Parameters
*/
public static function flushHandler($signal, &$params)
{
self::flush();
}
/**
* Activation of the low impact logging.
*
* When called, it enabled the assertions for debugging.
*/
public static function activeAssert()
{
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
assert_options(ASSERT_CALLBACK, 'Pluf_Log_assert');
}
}
/**
* Assertion handler.
*
* @param $file Name of the file where the assert is called
* @param $line Line number of the file where the assert is called
* @param $code Code evaluated by the assert call
*/
function Pluf_Log_assert($file, $line, $code)
{
if (Pluf_Log::$level >= Pluf_Log::$assert_level and
Pluf_Log::$level != 10) {
Pluf_Log::$stack[] = array(
microtime(true),
Pluf_Log::$assert_level,
Pluf_Log::$assert_mess,
$file, $line, $code);
if (!Pluf::f('log_delayed', false)) {
Pluf_Log::flush();
}
}
Pluf_Log::$assert_level = 6;
Pluf_Log::$assert_mess = null;
}
src/Pluf/Log/File.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
56
57
58
59
60
61
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Log to a file.
*
* This is the simplest logger. You can use it as a base to create
* more complex loggers. The logger interface is really simple and use
* some helper functions from the main <code>Pluf_Log</code> class.
*
* The only required static method of a log writer is
* <code>write</code>, which takes the stack to write as parameter.
*
* The only configuration variable of the file writer is the path to
* the log file 'pluf_log_file'. By default it creates a
* <code>pluf.log</code> in the configured tmp folder.
*
*/
class Pluf_Log_File
{
/**
* Flush the stack to the disk.
*
* @param $stack Array
*/
public static function write($stack)
{
$file = Pluf::f('pluf_log_file',
Pluf::f('tmp_folder', '/tmp').'/pluf.log');
$out = array();
foreach ($stack as $elt) {
$out[] = date(DATE_ISO8601, (int) $elt[0]).' '.
Pluf_Log::$reverse[$elt[1]].': '.
(string) $elt[2];
}
$fp = fopen($file, 'a');
flock($fp, LOCK_EX); // Blocking call.
fputs($fp, implode(PHP_EOL, $out).PHP_EOL);
fclose($fp) ; // release the lock
}
}
src/Pluf/Log/Remote.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
56
57
58
59
60
61
62
63
64
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Log to a server via a POST request.
*
* Fire a POST request agains a server with the payload being the
* content of the log. The log is serialized as JSON. It is always
* containing the current stack, so an array of log "lines".
*
* The configuration keys are:
*
* - 'log_remote_server' (localhost)
* - 'log_remote_path' (/)
* - 'log_remote_port' (8000)
* - 'log_remote_headers' (array())
*
*/
class Pluf_Log_Remote
{
/**
* Flush the stack to the remote server.
*
* @param $stack Array
*/
public static function write($stack)
{
$payload = json_encode($stack);
$out = 'POST '.Pluf::f('log_remote_path', '/').' HTTP/1.1'."\r\n";
$out.= 'Host: '.Pluf::f('log_remote_server', 'localhost')."\r\n";
$out.= 'Host: localhost'."\r\n";
$out.= 'Content-Length: '.strlen($payload)."\r\n";
foreach (Pluf::f('log_remote_headers', array()) as $key=>$val) {
$out .= $key.': '.$val."\r\n";
}
$out.= 'Connection: Close'."\r\n\r\n";
$out.= $payload;
$fp = fsockopen(Pluf::f('log_remote_server', 'localhost'),
Pluf::f('log_remote_port', 8000),
$errno, $errstr, 5);
fwrite($fp, $out);
fclose($fp);
}
}
src/Pluf/Tests/Log/TestLog.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
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
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Tests_Log_TestLog extends UnitTestCase
{
protected $logfile = '';
function __construct()
{
parent::__construct('Test the logger.');
}
function setUp()
{
$GLOBALS['_PX_config']['log_delayed'] = false;
Pluf_Log::$stack = array();
$this->logfile = Pluf::f('pluf_log_file',
Pluf::f('tmp_folder', '/tmp').'/pluf.log');
@unlink($this->logfile);
}
function tearDown()
{
@unlink($this->logfile);
}
function testSimple()
{
$GLOBALS['_PX_config']['log_delayed'] = true;
Pluf_Log::log('hello');
$this->assertEqual(count(Pluf_Log::$stack), 1);
$GLOBALS['_PX_config']['log_delayed'] = false;
Pluf_Log::log('hello');
$this->assertEqual(count(Pluf_Log::$stack), 0);
$this->assertEqual(2, count(file($this->logfile)));
}
function testAssertLog()
{
Pluf_Log::activeAssert();
$GLOBALS['_PX_config']['log_delayed'] = true;
assert('Pluf_Log::alog("hello")');
$this->assertEqual(count(Pluf_Log::$stack), 1);
$GLOBALS['_PX_config']['log_delayed'] = false;
assert('Pluf_Log::alog("hello")');
$this->assertEqual(count(Pluf_Log::$stack), 0);
$this->assertEqual(2, count(file($this->logfile)));
}
/**
function testPerformance()
{
$start = microtime(true);
$GLOBALS['_PX_config']['log_delayed'] = false;
for ($i=0;$i<100;$i++) {
Pluf_Log::log('hello'.$i);
}
$end = microtime(true);
print "File: ".($end-$start)."s\n";
$start = microtime(true);
$GLOBALS['_PX_config']['log_delayed'] = true;
for ($i=0;$i<100;$i++) {
Pluf_Log::log('hello'.$i);
}
Pluf_Log::flush();
$end = microtime(true);
print "File delayed: ".($end-$start)."s\n";
}
**/
}
src/Pluf/Tests/Log/TestRemote.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
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
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Tests_Log_TestRemote extends UnitTestCase
{
function __construct()
{
parent::__construct('Test the remote logger.');
}
function setUp()
{
$GLOBALS['_PX_config']['log_delayed'] = true;
$GLOBALS['_PX_config']['log_handler'] = 'Pluf_Log_Remote';
$GLOBALS['_PX_config']['log_remote_server'] = '127.0.0.1';
Pluf_Log::$stack = array();
}
function tearDown()
{
$GLOBALS['_PX_config']['log_handler'] = 'Pluf_Log_File';
$GLOBALS['_PX_config']['log_delayed'] = false;
}
function testSimple()
{
$GLOBALS['_PX_config']['log_delayed'] = true;
Pluf_Log::log('hello');
$this->assertEqual(count(Pluf_Log::$stack), 1);
$GLOBALS['_PX_config']['log_delayed'] = false;
Pluf_Log::log('hello');
$this->assertEqual(count(Pluf_Log::$stack), 0);
}
function testAssertLog()
{
Pluf_Log::activeAssert();
$GLOBALS['_PX_config']['log_delayed'] = true;
assert('Pluf_Log::alog("hello")');
$this->assertEqual(count(Pluf_Log::$stack), 1);
$GLOBALS['_PX_config']['log_delayed'] = false;
assert('Pluf_Log::alog("hello")');
$this->assertEqual(count(Pluf_Log::$stack), 0);
}
/**
function testPerformance()
{
$start = microtime(true);
$GLOBALS['_PX_config']['log_delayed'] = false;
for ($i=0;$i<100;$i++) {
Pluf_Log::log('hello'.$i);
}
$end = microtime(true);
print "Remote: ".($end-$start)."s\n";
$start = microtime(true);
$GLOBALS['_PX_config']['log_delayed'] = true;
for ($i=0;$i<100;$i++) {
Pluf_Log::log('hello'.$i);
}
Pluf_Log::flush();
$end = microtime(true);
print "Remote delayed: ".($end-$start)."s\n";
}
**/
}

Archive Download the corresponding diff file

Branches

Tags

Number of commits:
Page rendered in 0.09223s using 13 queries.