|
|
|
|
@ -4,163 +4,64 @@ namespace BSR\Utils\Logger;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
class LoggerTest extends TestCase{
|
|
|
|
|
const QUIET = 0;
|
|
|
|
|
const NORMAL = 1;
|
|
|
|
|
const VERBOSE = 2;
|
|
|
|
|
|
|
|
|
|
private static $start;
|
|
|
|
|
private static $data = array();
|
|
|
|
|
private static $log = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the ip address from server variables if applicable
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private static function ip()
|
|
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
|
|
|
|
$ip = '(n-a)';
|
|
|
|
|
Logger::start(array());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
|
|
$ip = array_shift(
|
|
|
|
|
array_map('trim', explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']))
|
|
|
|
|
);
|
|
|
|
|
} else if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
|
}
|
|
|
|
|
public function tearDown()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return $ip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function start($data = array())
|
|
|
|
|
public function testStartReturnsDefault()
|
|
|
|
|
{
|
|
|
|
|
self::$start = microtime(true);
|
|
|
|
|
|
|
|
|
|
self::$data = $data + array(
|
|
|
|
|
'ip' => self::ip(),
|
|
|
|
|
'date' => date('d.m.Y H:i:s'),
|
|
|
|
|
'func' => '(none)',
|
|
|
|
|
'version' => '(none)',
|
|
|
|
|
'error' => ''
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(is_array(Logger::data()),true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function info($info, $key = null)
|
|
|
|
|
public function testInfoWithKeyParamOverwritesDataKeyValue()
|
|
|
|
|
{
|
|
|
|
|
if(is_null($key)) {
|
|
|
|
|
self::$data = array_merge(self::$data, $info);
|
|
|
|
|
} else {
|
|
|
|
|
self::$data[$key] = $info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log a message that will be displayed in the logs if the configuration
|
|
|
|
|
* says so.
|
|
|
|
|
*
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param int $verbosity
|
|
|
|
|
*/
|
|
|
|
|
public static function log($message, $verbosity = Logger::VERBOSE) {
|
|
|
|
|
if(Configuration::get('log.verbosity') < $verbosity) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$log .= $message."\n";
|
|
|
|
|
public function testInfoWithoutKeyParamAndIntKeysGetRenumberedAndDontOverwrite()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function stop($data = null) {
|
|
|
|
|
if(! is_null($data)) {
|
|
|
|
|
self::info($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$time = (microtime(true) - self::$start) * 1000;
|
|
|
|
|
self::$data['time'] = round($time, 2).'ms';
|
|
|
|
|
|
|
|
|
|
if(Configuration::get('log.verbosity') > Logger::QUIET) {
|
|
|
|
|
$format = Configuration::get('log.format');
|
|
|
|
|
|
|
|
|
|
$patterns = array_map(function($p) { return "%$p%"; }, array_keys(self::$data));
|
|
|
|
|
$msg = str_replace($patterns, array_values(self::$data), $format)."\n";
|
|
|
|
|
|
|
|
|
|
if(strlen(self::$log) > 0) {
|
|
|
|
|
$msg .= self::$log;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$file = Configuration::get('log.file');
|
|
|
|
|
if(! file_exists($file)) {
|
|
|
|
|
mkdir(dirname($file), 0777, true);
|
|
|
|
|
touch($file);
|
|
|
|
|
} else {
|
|
|
|
|
$mtime = filemtime($file);
|
|
|
|
|
// start of the current day
|
|
|
|
|
$start = strtotime("midnight");
|
|
|
|
|
|
|
|
|
|
// log rotate if the last entry in the log is from yesterday
|
|
|
|
|
if($mtime < $start) {
|
|
|
|
|
$files = glob($file.'.?');
|
|
|
|
|
natsort($files);
|
|
|
|
|
$files = array_reverse($files);
|
|
|
|
|
$files[] = $file;
|
|
|
|
|
|
|
|
|
|
// we count before adding the next file
|
|
|
|
|
$len = count($files);
|
|
|
|
|
|
|
|
|
|
$next = intval(substr($files[0], -1)) + 1;
|
|
|
|
|
$next = $next == 1 ? "$file.1" : substr($files[0], 0, -1).$next;
|
|
|
|
|
|
|
|
|
|
array_unshift($files, $next);
|
|
|
|
|
for($i = 0; $i < $len; ++$i) {
|
|
|
|
|
// move all the log files to the next name
|
|
|
|
|
rename($files[$i + 1], $files[$i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// delete all files with a number bigger than 9
|
|
|
|
|
$files = glob($file.'.??');
|
|
|
|
|
array_map('unlink', $files);
|
|
|
|
|
|
|
|
|
|
// recreate the new log file
|
|
|
|
|
touch($file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file_put_contents($file, $msg, FILE_APPEND | LOCK_EX);
|
|
|
|
|
}
|
|
|
|
|
public function testLogDoesNotLogMessagesAccordingToVerbosityParamWhenSmallerThanConfig()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function data() {
|
|
|
|
|
return self::$data;
|
|
|
|
|
public function testLogLogsMessagesWhenVerbosityParamIsGreaterThanConfigVerbosity()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getLastLogs($offset = null) {
|
|
|
|
|
$file = Configuration::get('log.file');
|
|
|
|
|
if(! file_exists($file)) {
|
|
|
|
|
return 'No log yet !';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$f = fopen($file, 'r');
|
|
|
|
|
|
|
|
|
|
$len = 8192;
|
|
|
|
|
public function testStopComputesElapsedTimeProperly()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek($f, 0, SEEK_END);
|
|
|
|
|
$size = ftell($f);
|
|
|
|
|
if(is_null($offset) || $offset > $size) {
|
|
|
|
|
$offset = $size - $len;
|
|
|
|
|
}
|
|
|
|
|
$offset = max(0, $offset);
|
|
|
|
|
public function testStopDoesNotLogAnythingWhenConfigVerbosityIsQuiet()
|
|
|
|
|
{
|
|
|
|
|
$this->assertEquals(Configuration::get('log.verbosity'), Logger::QUIET);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek($f, $offset);
|
|
|
|
|
public function testStopSavesLogFileWhenConfigVerbosityIsNotQuiet()
|
|
|
|
|
{
|
|
|
|
|
$this->assertNotEquals(Configuration::get('log.verbosity'), Logger::QUIET);
|
|
|
|
|
$this->assertNotEquals('No log yet !', Logger::getLastLogs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// remove the first line that may be incomplete
|
|
|
|
|
$buffer = fread($f, $len);
|
|
|
|
|
$buffer = explode("\n", $buffer);
|
|
|
|
|
array_shift($buffer);
|
|
|
|
|
$buffer = implode("\n", $buffer);
|
|
|
|
|
// continue reading until the end of the file
|
|
|
|
|
while(! feof($f)) {
|
|
|
|
|
$buffer .= fread($f, $len);
|
|
|
|
|
}
|
|
|
|
|
public function testDataReturnsArray()
|
|
|
|
|
{
|
|
|
|
|
$this->assertEquals(is_array(Logger::data()), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose($f);
|
|
|
|
|
public function testGetLastLogsReturnsNoLogsWhenNoFileWithConfigLogFileNameExists()
|
|
|
|
|
{
|
|
|
|
|
$this->assertEquals('No log yet !', Logger::getLastLogs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $buffer;
|
|
|
|
|
public function testGetLastLogsReturnsALogBufferWhenFileWithConfigLogFileNameExists()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|