84 lines
2.1 KiB
PHP
Executable File
84 lines
2.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace log\core;
|
|
|
|
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];
|
|
}
|
|
|
|
}
|