Implementation log:1.0

This commit is contained in:
xdrm-brackets 2016-12-11 23:35:51 +01:00
parent 5676d618ae
commit 037cdd3948
4 changed files with 165 additions and 82 deletions

View File

@ -7,7 +7,11 @@
"1.0": [] "1.0": []
}, },
"log": { "log": {
"1.0": [] "1.0": {
"filedriver": [
"1.0"
]
}
}, },
"error": { "error": {
"1.0": [], "1.0": [],
@ -69,12 +73,13 @@
} }
}, },
"installed": { "installed": {
"log": "1.0",
"filedriver": "1.0",
"router": "2.0",
"api": "2.2", "api": "2.2",
"error": "2.0", "error": "2.0",
"http": "1.0", "http": "1.0",
"orm": "0.8.2", "orm": "0.8.2",
"database": "2.0", "database": "2.0"
"router": "2.0",
"session": "1.0"
} }
} }

View File

@ -21,8 +21,7 @@
/* (1) Creates file */ /* (1) Creates file */
try{ try{
fclose( fopen($file, 'w') ); return fclose( fopen($file, 'w') );
return true;
}catch(\Exception $e){ return false; } }catch(\Exception $e){ return false; }
} }

View File

@ -2,7 +2,85 @@
namespace log\core; namespace log\core;
use \filedriver\core\FileDriver;
class Log{ class Log{
private static $maxlen = 50;
/* [1] Attributes
=========================================================*/
private $label; // label of the log system
private $file; // absolute path to logfile
/* [2] Constructor
*
* @label<String> Label of the logger
*
=========================================================*/
private function __construct($label){
/* (1) Set logfile and label */
$this->label = $label;
$this->file = __BUILD__."/log/log/$label.log";
/* (2) Create file if doesn't exist already */
if( !is_file($this->file) )
FileDriver::create($this->file);
}
/* [3] Writes a new log to the file
*
* @content<String> Content to log
* @tag<String> [OPT] Tag of the log (category)
*
* @return outputname<outputtype> outputdesc
*
=========================================================*/
public function log($content="...", $tag="default"){
/* (1) Get time data */
$timestamp = time();
$date = date('Y-m-s H:i:s', $timestamp);
/* (2) Create beginning string */
$body = "$timestamp | $date | [$tag] ";
$headerl = strlen($body);
/* (3) Split content by length */
$body .= substr($content, 0, self::$maxlen);
for( $i = self::$maxlen, $l = strlen($content) ; $i < $l ; $i += self::$maxlen )
$body .= "\n".str_repeat(" ", $headerl).substr($content, $i, self::$maxlen);
/* (4) Append to log file */
FileDriver::append($this->file, $body);
}
/* [4] Multiton attributes
=========================================================*/
private static $instance = []; // instances
/* [5] Multiton Manager
*
* @label<String> Label of the logger you want
*
* @return instance<Log> instance of the wanted logger
*
=========================================================*/
public static function get($label="default"){
/* (1) Create if doesn't exist */
if( !isset(self::$instance[$label]) )
self::$instance[$label] = new Log($label);
/* (2) Return instance for all cases */
return self::$instance[$label];
}
} }

1
src/packages/log/1.0/log/default.log vendored Normal file
View File

@ -0,0 +1 @@
1481495257 | 2016-12-37 23:27:37 | [default] sample log