php-vn-lib/lib/log.php

69 lines
1.2 KiB
PHP
Raw Normal View History

2015-01-23 12:38:29 +00:00
<?php
namespace Vn\Lib;
class Log {
2015-01-23 12:38:29 +00:00
private static $fd = NULL;
private static $count;
private static $file;
static function init($file) {
self::close();
2015-01-23 12:38:29 +00:00
self::$file = $file;
self::$fd = fopen($file, 'a');
self::rename();
set_error_handler('Vn\Lib\Log::phpHandler', E_ALL);
2015-01-23 12:38:29 +00:00
}
static function phpHandler($no, $str, $file, $line, $context) {
self::write('PHP: %s:%d: %s', $file, $line, $str);
switch ($no) {
2015-01-23 12:38:29 +00:00
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
self::write('PHP: Could not continue, exiting.');
self::close();
exit(1);
2015-01-23 12:38:29 +00:00
}
return TRUE;
}
static function close() {
if (self::$fd != NULL) {
fclose(self::$fd);
2015-01-23 12:38:29 +00:00
self::$fd = NULL;
}
}
static function write() {
2015-01-23 12:38:29 +00:00
if (self::$fd == NULL)
return;
if (self::$count > 5000)
self::rename();
2015-01-23 12:38:29 +00:00
self::$count += fprintf(self::$fd, "%s: %s\n"
,strftime('%Y-%m-%d %T')
,call_user_func_array('sprintf', func_get_args())
2015-01-23 12:38:29 +00:00
);
}
static private function rename() {
if (filesize(self::$file) > 1000000) {
self::close();
2015-01-23 12:38:29 +00:00
$rename = self::$file.'.1';
if (file_exists($rename))
unlink($rename);
2015-01-23 12:38:29 +00:00
rename(self::$file, $rename);
self::$fd = fopen($file, 'a');
2015-01-23 12:38:29 +00:00
}
self::$count = 0;
}
}