From 037cdd39480c1a5acdcdd5c5b399ac736e06e0d2 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 11 Dec 2016 23:35:51 +0100 Subject: [PATCH] Implementation log:1.0 --- exporter/packages.json | 163 +++++++++--------- .../filedriver/1.0/core/FileDriver.php | 3 +- src/packages/log/1.0/core/Log.php | 80 ++++++++- src/packages/log/1.0/log/default.log | 1 + 4 files changed, 165 insertions(+), 82 deletions(-) create mode 100644 src/packages/log/1.0/log/default.log diff --git a/exporter/packages.json b/exporter/packages.json index 6e5fb4e..95447a0 100755 --- a/exporter/packages.json +++ b/exporter/packages.json @@ -1,80 +1,85 @@ { - "available": { - "session": { - "1.0": [] - }, - "filedriver": { - "1.0": [] - }, - "log": { - "1.0": [] - }, - "error": { - "1.0": [], - "2.0": [] - }, - "http": { - "1.0": [] - }, - "api": { - "1.0": { - "error": [ - "1.0" - ] - }, - "2.0": { - "error": [ - "2.0" - ] - }, - "2.2": { - "error": [ - "2.0" - ], - "http": [ - "1.0" - ] - } - }, - "orm": { - "0.8.1": { - "database": [ - "1.0" - ] - }, - "0.8.2": { - "database": [ - "2.0" - ] - } - }, - "database": { - "1.0": { - "error": [ - "1.0" - ] - }, - "2.0": { - "error": [ - "2.0" - ] - } - }, - "lightdb": { - "1.0": [] - }, - "router": { - "1.0": [], - "2.0": [] - } - }, - "installed": { - "api": "2.2", - "error": "2.0", - "http": "1.0", - "orm": "0.8.2", - "database": "2.0", - "router": "2.0", - "session": "1.0" - } -} + "available": { + "session": { + "1.0": [] + }, + "filedriver": { + "1.0": [] + }, + "log": { + "1.0": { + "filedriver": [ + "1.0" + ] + } + }, + "error": { + "1.0": [], + "2.0": [] + }, + "http": { + "1.0": [] + }, + "api": { + "1.0": { + "error": [ + "1.0" + ] + }, + "2.0": { + "error": [ + "2.0" + ] + }, + "2.2": { + "error": [ + "2.0" + ], + "http": [ + "1.0" + ] + } + }, + "orm": { + "0.8.1": { + "database": [ + "1.0" + ] + }, + "0.8.2": { + "database": [ + "2.0" + ] + } + }, + "database": { + "1.0": { + "error": [ + "1.0" + ] + }, + "2.0": { + "error": [ + "2.0" + ] + } + }, + "lightdb": { + "1.0": [] + }, + "router": { + "1.0": [], + "2.0": [] + } + }, + "installed": { + "log": "1.0", + "filedriver": "1.0", + "router": "2.0", + "api": "2.2", + "error": "2.0", + "http": "1.0", + "orm": "0.8.2", + "database": "2.0" + } +} \ No newline at end of file diff --git a/src/packages/filedriver/1.0/core/FileDriver.php b/src/packages/filedriver/1.0/core/FileDriver.php index f2b4462..95061f5 100644 --- a/src/packages/filedriver/1.0/core/FileDriver.php +++ b/src/packages/filedriver/1.0/core/FileDriver.php @@ -21,8 +21,7 @@ /* (1) Creates file */ try{ - fclose( fopen($file, 'w') ); - return true; + return fclose( fopen($file, 'w') ); }catch(\Exception $e){ return false; } } diff --git a/src/packages/log/1.0/core/Log.php b/src/packages/log/1.0/core/Log.php index 41d895e..6a2ef5a 100644 --- a/src/packages/log/1.0/core/Log.php +++ b/src/packages/log/1.0/core/Log.php @@ -2,7 +2,85 @@ namespace log\core; + use \filedriver\core\FileDriver; + class Log{ - + + private static $maxlen = 50; + + /* [1] Attributes + =========================================================*/ + private $label; // label of the log system + private $file; // absolute path to logfile + + + /* [2] Constructor + * + * @label 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 Content to log + * @tag [OPT] Tag of the log (category) + * + * @return outputname 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 Label of the logger you want + * + * @return instance 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]; + } + } diff --git a/src/packages/log/1.0/log/default.log b/src/packages/log/1.0/log/default.log new file mode 100644 index 0000000..9d0f8de --- /dev/null +++ b/src/packages/log/1.0/log/default.log @@ -0,0 +1 @@ +1481495257 | 2016-12-37 23:27:37 | [default] sample log