From 32dd2efe4dc768dc74c3916373fde5670884490b Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 3 Sep 2020 10:41:24 -0500 Subject: [PATCH 01/15] Add session logger --- Wa72/SimpleLogger/SessionLogger.php | 97 +++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Wa72/SimpleLogger/SessionLogger.php diff --git a/Wa72/SimpleLogger/SessionLogger.php b/Wa72/SimpleLogger/SessionLogger.php new file mode 100644 index 0000000..8ee42d5 --- /dev/null +++ b/Wa72/SimpleLogger/SessionLogger.php @@ -0,0 +1,97 @@ +min_level = $min_level; + $this->name = $name; + } + + private function &getSession(): array + { + if (empty($_SESSION[$this->name])) { + $_SESSION[$this->name] = []; + } + return $_SESSION[$this->name]; + } + + public function log($level, $message, array $context = array()) + { + if (!$this->min_level_reached($level)) { + return; + } + $this->getSession()[] = array( + 'timestamp' => date('Y-m-d H:i:s'), + 'level' => $level, + 'message' => $message, + 'context' => $context + ); + } + + /** + * Get all log entries + * + * @param callable|null $formatter An optional formatting function called on every log entry. + * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' + * and must return the new log entry. + * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context', + * unless the log entries are converted to something else by the $formatter parameter. + */ + public function get($formatter = null) + { + $r = $this->getSession(); + if (is_callable($formatter)) { + foreach ($r as $i => $a) { + $r[$i] = call_user_func($formatter, $a); + } + } + return $r; + } + + /** + * Get all log entries and clear the log + * + * @param callable|null $formatter An optional formatting function called on every log entry. + * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' + * and must return the new log entry. + * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context' + * unless the log entries are converted to something else by the $formatter parameter. + */ + public function getClear($formatter = null) + { + $r = $this->getSession(); + $this->clear(); + if (is_callable($formatter)) { + foreach ($r as $i => $a) { + $r[$i] = call_user_func($formatter, $a); + } + } + return $r; + } + + /** + * Clear the log + * + */ + public function clear() + { + unset($_SESSION[$this->name]); + } + + /** + * Formatter function that can be used as parameter for the get() and getClear() methods + * + * @param array $a + * @return string + */ + public function formatter(array $a) + { + return $this->format($a['level'], $a['message'], $a['context'], $a['timestamp']); + } +} From 4093186c9728948f835c074382d8406a7b29d032 Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 3 Sep 2020 10:46:05 -0500 Subject: [PATCH 02/15] Update README --- README.md | 21 ++++++++------------- Wa72/SimpleLogger/SessionLogger.php | 3 +++ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c585195..8c1630a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -Wa72SimpleLogger (collection of PHP logger classes) -=================================================== +# Wa72SimpleLogger (collection of PHP logger classes) -Wa72SimpleLogger is a collection of very simple logger classes for PHP 5.4 implementing \Psr\Log\LoggerInterface (PSR-3), +Wa72SimpleLogger is a collection of very simple logger classes for PHP 5.4 implementing \Psr\Log\LoggerInterface (PSR-3), the common logger interface standardized by the PHP Framework Interop Group (www.php-fig.org). Wa72SimpleLogger is intended for small projects or testing purposes if you don't need a full-featured logging solution @@ -9,8 +8,7 @@ like Monolog. If you just need to output a few log messages in a small PHP project but want to stick to the PSR-3 standard this package is for you. When your project grows you can simply replace it by a more advanced logging solution like Monolog. -Loggers -------- +## Loggers - \Wa72\SimpleLogger\EchoLogger: Just echo the log message @@ -18,17 +16,15 @@ Loggers - \Wa72\SimpleLogger\ArrayLogger: Keep log messages in an array for later use (e.g. display it to the user) -- \Wa72\SimpleLogger\ConsoleLogger: Log to the Symfony2 console => *DEPRECATED: use `Symfony\Component\Console\Logger\ConsoleLogger` instead* +- \Wa72\SimpleLogger\SessionLogger: Keep log messages in a session for later use (e.g. display it to the user on another page) +- \Wa72\SimpleLogger\ConsoleLogger: Log to the Symfony2 console => _DEPRECATED: use `Symfony\Component\Console\Logger\ConsoleLogger` instead_ -Installation ------------- +## Installation -- `composer require wa72/simplelogger` +- `composer require wa72/simplelogger` - -Usage ------ +## Usage ```php $logger = new \Wa72\SimpleLogger\FileLogger('/path/to/logfile'); @@ -52,4 +48,3 @@ In one of my projects there was a "fetcher" class that fetched some information - if called from the command line, it is given a ConsoleLogger - if called from the web interface, it is given an ArrayLogger. The output of this logger is then displayed to the user on the web page. - diff --git a/Wa72/SimpleLogger/SessionLogger.php b/Wa72/SimpleLogger/SessionLogger.php index 8ee42d5..e26333a 100644 --- a/Wa72/SimpleLogger/SessionLogger.php +++ b/Wa72/SimpleLogger/SessionLogger.php @@ -15,6 +15,9 @@ public function __construct($min_level = LogLevel::DEBUG, $name = 'Wa72\SimpleLo private function &getSession(): array { + if (session_status() == PHP_SESSION_NONE) { + session_start(); + } if (empty($_SESSION[$this->name])) { $_SESSION[$this->name] = []; } From 16af48630467352ff64005a4847012303c4b053d Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 3 Sep 2020 13:07:32 -0500 Subject: [PATCH 03/15] Change type to package --- composer.json | 53 +++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index 505276e..f8e717a 100644 --- a/composer.json +++ b/composer.json @@ -1,28 +1,31 @@ { - "name":"wa72/simplelogger", - "description":"Wa72SimpleLogger is a collection of very simple loggers implementing \\Psr\\Log\\LoggerInterface (PSR-3)", - "keywords":["log", "logger", "PSR-3"], - "homepage":"http://github.com/wasinger/simplelogger", - "type":"library", - "license":"MIT", - "authors":[ - { - "name":"Christoph Singer", - "email":"singer@webagentur72.de", - "homepage":"http://www.webagentur72.de" - } - ], - "require":{ - "php":">=5.4", - "psr/log":"^1.0.0" - }, - "provide": { - "psr/log-implementation": "1.0.0" - }, - "autoload":{ - "psr-0":{ - "Wa72\\SimpleLogger":"." - } + "name": "wa72/simplelogger", + "description": "Wa72SimpleLogger is a collection of very simple loggers implementing \\Psr\\Log\\LoggerInterface (PSR-3)", + "keywords": [ + "log", + "logger", + "PSR-3" + ], + "homepage": "http://github.com/wasinger/simplelogger", + "type": "package", + "license": "MIT", + "authors": [ + { + "name": "Christoph Singer", + "email": "singer@webagentur72.de", + "homepage": "http://www.webagentur72.de" } + ], + "require": { + "php": ">=5.4", + "psr/log": "^1.0.0" + }, + "provide": { + "psr/log-implementation": "1.0.0" + }, + "autoload": { + "psr-0": { + "Wa72\\SimpleLogger": "." + } + } } - From ed7c6648454aab349391d51ae56b08bd533de70e Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 3 Sep 2020 13:18:23 -0500 Subject: [PATCH 04/15] Change type to package --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f8e717a..f344333 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "psr/log-implementation": "1.0.0" }, "autoload": { - "psr-0": { + "psr-4": { "Wa72\\SimpleLogger": "." } } From ba5267a9d1d55e259ddafa8251d85d643c97b953 Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 3 Sep 2020 13:19:20 -0500 Subject: [PATCH 05/15] Change type to package --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f344333..da24692 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "autoload": { "psr-4": { - "Wa72\\SimpleLogger": "." + "Wa72\\SimpleLogger\\": "." } } } From 070445e63966739b0632d47b359d5ec0fbd91476 Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 3 Sep 2020 13:20:47 -0500 Subject: [PATCH 06/15] Change type to package --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index da24692..267a565 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "autoload": { "psr-4": { - "Wa72\\SimpleLogger\\": "." + "Wa72\\SimpleLogger\\": "./" } } } From 42ad64b080a4da9a77a98119f5e2f019595c2674 Mon Sep 17 00:00:00 2001 From: midwestE <6331501+midweste@users.noreply.github.com> Date: Thu, 17 Dec 2020 11:05:55 -0600 Subject: [PATCH 07/15] Update composer.json Break fork with unmaintained parent package --- composer.json | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 267a565..15e7879 100644 --- a/composer.json +++ b/composer.json @@ -1,19 +1,17 @@ { - "name": "wa72/simplelogger", - "description": "Wa72SimpleLogger is a collection of very simple loggers implementing \\Psr\\Log\\LoggerInterface (PSR-3)", + "name": "midweste/simplelogger", + "description": "SimpleLogger is a collection of very simple loggers implementing \\Psr\\Log\\LoggerInterface (PSR-3)", "keywords": [ "log", "logger", "PSR-3" ], - "homepage": "http://github.com/wasinger/simplelogger", + "homepage": "http://github.com/midwest/simplelogger", "type": "package", "license": "MIT", "authors": [ { - "name": "Christoph Singer", - "email": "singer@webagentur72.de", - "homepage": "http://www.webagentur72.de" + "name": "midweste" } ], "require": { @@ -25,7 +23,7 @@ }, "autoload": { "psr-4": { - "Wa72\\SimpleLogger\\": "./" + "Midweste\\SimpleLogger\\": "./" } } } From faa35ef17fe465bc369e9b8fb6c28fbd020fbd47 Mon Sep 17 00:00:00 2001 From: midwestE <6331501+midweste@users.noreply.github.com> Date: Thu, 17 Dec 2020 11:10:26 -0600 Subject: [PATCH 08/15] Update README.md Update readme --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8c1630a..7e4ff6d 100644 --- a/README.md +++ b/README.md @@ -1,40 +1,40 @@ -# Wa72SimpleLogger (collection of PHP logger classes) +# SimpleLogger (collection of PHP logger classes) - Forked from/Credits to wasinger/simplelogger https://github.com/wasinger/simplelogger -Wa72SimpleLogger is a collection of very simple logger classes for PHP 5.4 implementing \Psr\Log\LoggerInterface (PSR-3), +SimpleLogger is a collection of very simple logger classes for PHP 5.4 implementing \Psr\Log\LoggerInterface (PSR-3), the common logger interface standardized by the PHP Framework Interop Group (www.php-fig.org). -Wa72SimpleLogger is intended for small projects or testing purposes if you don't need a full-featured logging solution +SimpleLogger is intended for small projects or testing purposes if you don't need a full-featured logging solution like Monolog. If you just need to output a few log messages in a small PHP project but want to stick to the PSR-3 standard this package is for you. When your project grows you can simply replace it by a more advanced logging solution like Monolog. ## Loggers -- \Wa72\SimpleLogger\EchoLogger: Just echo the log message +- \Midweste\SimpleLogger\EchoLogger: Just echo the log message -- \Wa72\SimpleLogger\FileLogger: Log to a file +- \Midweste\SimpleLogger\FileLogger: Log to a file -- \Wa72\SimpleLogger\ArrayLogger: Keep log messages in an array for later use (e.g. display it to the user) +- \Midweste\SimpleLogger\ArrayLogger: Keep log messages in an array for later use (e.g. display it to the user) -- \Wa72\SimpleLogger\SessionLogger: Keep log messages in a session for later use (e.g. display it to the user on another page) +- \Midweste\SimpleLogger\SessionLogger: Keep log messages in a session for later use (e.g. display it to the user on another page) -- \Wa72\SimpleLogger\ConsoleLogger: Log to the Symfony2 console => _DEPRECATED: use `Symfony\Component\Console\Logger\ConsoleLogger` instead_ +- \Midweste\SimpleLogger\ConsoleLogger: Log to the Symfony2 console => _DEPRECATED: use `Symfony\Component\Console\Logger\ConsoleLogger` instead_ ## Installation -- `composer require wa72/simplelogger` +- `composer require Midweste/simplelogger` ## Usage ```php -$logger = new \Wa72\SimpleLogger\FileLogger('/path/to/logfile'); +$logger = new \Midweste\SimpleLogger\FileLogger('/path/to/logfile'); $logger->info('This is the first log message'); ``` **NEW**: it's now possible to set a minimum log level in the constructor of FileLogger, EchoLogger and ArrayLogger: ```php -$logger = new \Wa72\SimpleLogger\FileLogger('/path/to/logfile', \Psr\Log\LogLevel::ERROR); +$logger = new \Midweste\SimpleLogger\FileLogger('/path/to/logfile', \Psr\Log\LogLevel::ERROR); $logger->info('This is the first log message'); // this message will be discarded $logger->error('This is an error message'); // this message will be logged ``` From 4f23a3c9cde5fbb164d48f91191a7165a45d9f09 Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 17 Dec 2020 11:16:19 -0600 Subject: [PATCH 09/15] Switch namespace --- Wa72/SimpleLogger/AbstractSimpleLogger.php | 74 --------------- Wa72/SimpleLogger/ArrayLogger.php | 87 ------------------ Wa72/SimpleLogger/ConsoleLogger.php | 37 -------- Wa72/SimpleLogger/EchoLogger.php | 26 ------ Wa72/SimpleLogger/FileLogger.php | 38 -------- Wa72/SimpleLogger/SessionLogger.php | 100 --------------------- Wa72SimpleLogger.php | 14 --- composer.json | 2 +- 8 files changed, 1 insertion(+), 377 deletions(-) delete mode 100644 Wa72/SimpleLogger/AbstractSimpleLogger.php delete mode 100644 Wa72/SimpleLogger/ArrayLogger.php delete mode 100644 Wa72/SimpleLogger/ConsoleLogger.php delete mode 100644 Wa72/SimpleLogger/EchoLogger.php delete mode 100644 Wa72/SimpleLogger/FileLogger.php delete mode 100644 Wa72/SimpleLogger/SessionLogger.php delete mode 100755 Wa72SimpleLogger.php diff --git a/Wa72/SimpleLogger/AbstractSimpleLogger.php b/Wa72/SimpleLogger/AbstractSimpleLogger.php deleted file mode 100644 index 94aa943..0000000 --- a/Wa72/SimpleLogger/AbstractSimpleLogger.php +++ /dev/null @@ -1,74 +0,0 @@ -levels) >= \array_search($this->min_level, $this->levels); - } - - /** - * Interpolates context values into the message placeholders. - * - * @author PHP Framework Interoperability Group - * - * @param string $message - * @param array $context - * @return string - */ - protected function interpolate($message, array $context) - { - if (false === strpos($message, '{')) { - return $message; - } - - $replacements = array(); - foreach ($context as $key => $val) { - if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) { - $replacements["{{$key}}"] = $val; - } elseif ($val instanceof \DateTimeInterface) { - $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339); - } elseif (\is_object($val)) { - $replacements["{{$key}}"] = '[object '.\get_class($val).']'; - } else { - $replacements["{{$key}}"] = '['.\gettype($val).']'; - } - } - - return strtr($message, $replacements); - } - - /** - * @param string $level - * @param string $message - * @param array $context - * @param string|null $timestamp A Timestamp string in format 'Y-m-d H:i:s', defaults to current time - * @return string - */ - protected function format($level, $message, $context, $timestamp = null) - { - if ($timestamp === null) $timestamp = date('Y-m-d H:i:s'); - return '[' . $timestamp . '] ' . strtoupper($level) . ': ' . $this->interpolate($message, $context) . "\n"; - } -} \ No newline at end of file diff --git a/Wa72/SimpleLogger/ArrayLogger.php b/Wa72/SimpleLogger/ArrayLogger.php deleted file mode 100644 index d7dda9a..0000000 --- a/Wa72/SimpleLogger/ArrayLogger.php +++ /dev/null @@ -1,87 +0,0 @@ -min_level = $min_level; - } - - public function log($level, $message, array $context = array()) - { - if (!$this->min_level_reached($level)) { - return; - } - $this->memory[] = array( - 'timestamp' => date('Y-m-d H:i:s'), - 'level' => $level, - 'message' => $message, - 'context' => $context - ); - } - - /** - * Get all log entries - * - * @param callable|null $formatter An optional formatting function called on every log entry. - * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' - * and must return the new log entry. - * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context', - * unless the log entries are converted to something else by the $formatter parameter. - */ - public function get($formatter = null) - { - $r = $this->memory; - if (is_callable($formatter)) { - foreach ($r as $i => $a) { - $r[$i] = call_user_func($formatter, $a); - } - } - return $r; - } - - /** - * Get all log entries and clear the log - * - * @param callable|null $formatter An optional formatting function called on every log entry. - * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' - * and must return the new log entry. - * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context' - * unless the log entries are converted to something else by the $formatter parameter. - */ - public function getClear($formatter = null) - { - $r = $this->memory; - $this->clear(); - if (is_callable($formatter)) { - foreach ($r as $i => $a) { - $r[$i] = call_user_func($formatter, $a); - } - } - return $r; - } - - /** - * Clear the log - * - */ - public function clear() - { - $this->memory = array(); - } - - /** - * Formatter function that can be used as parameter for the get() and getClear() methods - * - * @param array $a - * @return string - */ - public function formatter(array $a) - { - return $this->format($a['level'], $a['message'], $a['context'], $a['timestamp']); - } -} diff --git a/Wa72/SimpleLogger/ConsoleLogger.php b/Wa72/SimpleLogger/ConsoleLogger.php deleted file mode 100644 index 0f7a96f..0000000 --- a/Wa72/SimpleLogger/ConsoleLogger.php +++ /dev/null @@ -1,37 +0,0 @@ -out = $out; - } - /** - * Logs with an arbitrary level. - * - * @param mixed $level - * @param string $message - * @param array $context - * @return null - */ - public function log($level, $message, array $context = array()) - { - - $message = '<'. $level . '>' . $message . ''; - $this->out->writeln($message); - } - -} diff --git a/Wa72/SimpleLogger/EchoLogger.php b/Wa72/SimpleLogger/EchoLogger.php deleted file mode 100644 index c79bb8e..0000000 --- a/Wa72/SimpleLogger/EchoLogger.php +++ /dev/null @@ -1,26 +0,0 @@ -min_level = $min_level; - } - - public function log($level, $message, array $context = array()) - { - if (!$this->min_level_reached($level)) { - return; - } - echo $this->format($level, $message, $context); - } -} diff --git a/Wa72/SimpleLogger/FileLogger.php b/Wa72/SimpleLogger/FileLogger.php deleted file mode 100644 index aa9cbe9..0000000 --- a/Wa72/SimpleLogger/FileLogger.php +++ /dev/null @@ -1,38 +0,0 @@ -logfile = $logfile; - $this->min_level = $min_level; - } - - public function log($level, $message, array $context = array()) - { - if (!$this->min_level_reached($level)) { - return; - } - $logline = $this->format($level, $message, $context); - file_put_contents($this->logfile, $logline, FILE_APPEND | LOCK_EX); - } -} diff --git a/Wa72/SimpleLogger/SessionLogger.php b/Wa72/SimpleLogger/SessionLogger.php deleted file mode 100644 index e26333a..0000000 --- a/Wa72/SimpleLogger/SessionLogger.php +++ /dev/null @@ -1,100 +0,0 @@ -min_level = $min_level; - $this->name = $name; - } - - private function &getSession(): array - { - if (session_status() == PHP_SESSION_NONE) { - session_start(); - } - if (empty($_SESSION[$this->name])) { - $_SESSION[$this->name] = []; - } - return $_SESSION[$this->name]; - } - - public function log($level, $message, array $context = array()) - { - if (!$this->min_level_reached($level)) { - return; - } - $this->getSession()[] = array( - 'timestamp' => date('Y-m-d H:i:s'), - 'level' => $level, - 'message' => $message, - 'context' => $context - ); - } - - /** - * Get all log entries - * - * @param callable|null $formatter An optional formatting function called on every log entry. - * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' - * and must return the new log entry. - * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context', - * unless the log entries are converted to something else by the $formatter parameter. - */ - public function get($formatter = null) - { - $r = $this->getSession(); - if (is_callable($formatter)) { - foreach ($r as $i => $a) { - $r[$i] = call_user_func($formatter, $a); - } - } - return $r; - } - - /** - * Get all log entries and clear the log - * - * @param callable|null $formatter An optional formatting function called on every log entry. - * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' - * and must return the new log entry. - * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context' - * unless the log entries are converted to something else by the $formatter parameter. - */ - public function getClear($formatter = null) - { - $r = $this->getSession(); - $this->clear(); - if (is_callable($formatter)) { - foreach ($r as $i => $a) { - $r[$i] = call_user_func($formatter, $a); - } - } - return $r; - } - - /** - * Clear the log - * - */ - public function clear() - { - unset($_SESSION[$this->name]); - } - - /** - * Formatter function that can be used as parameter for the get() and getClear() methods - * - * @param array $a - * @return string - */ - public function formatter(array $a) - { - return $this->format($a['level'], $a['message'], $a['context'], $a['timestamp']); - } -} diff --git a/Wa72SimpleLogger.php b/Wa72SimpleLogger.php deleted file mode 100755 index bb3ae81..0000000 --- a/Wa72SimpleLogger.php +++ /dev/null @@ -1,14 +0,0 @@ - Date: Thu, 17 Dec 2020 11:16:38 -0600 Subject: [PATCH 10/15] Move files to src --- src/AbstractSimpleLogger.php | 75 ++++++++++++++++++++++++++ src/ArrayLogger.php | 89 +++++++++++++++++++++++++++++++ src/ConsoleLogger.php | 40 ++++++++++++++ src/EchoLogger.php | 29 ++++++++++ src/FileLogger.php | 41 ++++++++++++++ src/SessionLogger.php | 100 +++++++++++++++++++++++++++++++++++ 6 files changed, 374 insertions(+) create mode 100644 src/AbstractSimpleLogger.php create mode 100644 src/ArrayLogger.php create mode 100644 src/ConsoleLogger.php create mode 100644 src/EchoLogger.php create mode 100644 src/FileLogger.php create mode 100644 src/SessionLogger.php diff --git a/src/AbstractSimpleLogger.php b/src/AbstractSimpleLogger.php new file mode 100644 index 0000000..f67e47d --- /dev/null +++ b/src/AbstractSimpleLogger.php @@ -0,0 +1,75 @@ +levels) >= \array_search($this->min_level, $this->levels); + } + + /** + * Interpolates context values into the message placeholders. + * + * @author PHP Framework Interoperability Group + * + * @param string $message + * @param array $context + * @return string + */ + protected function interpolate($message, array $context) + { + if (false === strpos($message, '{')) { + return $message; + } + + $replacements = array(); + foreach ($context as $key => $val) { + if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) { + $replacements["{{$key}}"] = $val; + } elseif ($val instanceof \DateTimeInterface) { + $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339); + } elseif (\is_object($val)) { + $replacements["{{$key}}"] = '[object ' . \get_class($val) . ']'; + } else { + $replacements["{{$key}}"] = '[' . \gettype($val) . ']'; + } + } + + return strtr($message, $replacements); + } + + /** + * @param string $level + * @param string $message + * @param array $context + * @param string|null $timestamp A Timestamp string in format 'Y-m-d H:i:s', defaults to current time + * @return string + */ + protected function format($level, $message, $context, $timestamp = null) + { + if ($timestamp === null) $timestamp = date('Y-m-d H:i:s'); + return '[' . $timestamp . '] ' . strtoupper($level) . ': ' . $this->interpolate($message, $context) . "\n"; + } +} diff --git a/src/ArrayLogger.php b/src/ArrayLogger.php new file mode 100644 index 0000000..9213ba1 --- /dev/null +++ b/src/ArrayLogger.php @@ -0,0 +1,89 @@ +min_level = $min_level; + } + + public function log($level, $message, array $context = array()) + { + if (!$this->min_level_reached($level)) { + return; + } + $this->memory[] = array( + 'timestamp' => date('Y-m-d H:i:s'), + 'level' => $level, + 'message' => $message, + 'context' => $context + ); + } + + /** + * Get all log entries + * + * @param callable|null $formatter An optional formatting function called on every log entry. + * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' + * and must return the new log entry. + * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context', + * unless the log entries are converted to something else by the $formatter parameter. + */ + public function get($formatter = null) + { + $r = $this->memory; + if (is_callable($formatter)) { + foreach ($r as $i => $a) { + $r[$i] = call_user_func($formatter, $a); + } + } + return $r; + } + + /** + * Get all log entries and clear the log + * + * @param callable|null $formatter An optional formatting function called on every log entry. + * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' + * and must return the new log entry. + * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context' + * unless the log entries are converted to something else by the $formatter parameter. + */ + public function getClear($formatter = null) + { + $r = $this->memory; + $this->clear(); + if (is_callable($formatter)) { + foreach ($r as $i => $a) { + $r[$i] = call_user_func($formatter, $a); + } + } + return $r; + } + + /** + * Clear the log + * + */ + public function clear() + { + $this->memory = array(); + } + + /** + * Formatter function that can be used as parameter for the get() and getClear() methods + * + * @param array $a + * @return string + */ + public function formatter(array $a) + { + return $this->format($a['level'], $a['message'], $a['context'], $a['timestamp']); + } +} diff --git a/src/ConsoleLogger.php b/src/ConsoleLogger.php new file mode 100644 index 0000000..2a4a69b --- /dev/null +++ b/src/ConsoleLogger.php @@ -0,0 +1,40 @@ +out = $out; + } + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * @return null + */ + public function log($level, $message, array $context = array()) + { + + $message = '<' . $level . '>' . $message . ''; + $this->out->writeln($message); + } +} diff --git a/src/EchoLogger.php b/src/EchoLogger.php new file mode 100644 index 0000000..1256ac4 --- /dev/null +++ b/src/EchoLogger.php @@ -0,0 +1,29 @@ +min_level = $min_level; + } + + public function log($level, $message, array $context = array()) + { + if (!$this->min_level_reached($level)) { + return; + } + echo $this->format($level, $message, $context); + } +} diff --git a/src/FileLogger.php b/src/FileLogger.php new file mode 100644 index 0000000..9844dbb --- /dev/null +++ b/src/FileLogger.php @@ -0,0 +1,41 @@ +logfile = $logfile; + $this->min_level = $min_level; + } + + public function log($level, $message, array $context = array()) + { + if (!$this->min_level_reached($level)) { + return; + } + $logline = $this->format($level, $message, $context); + file_put_contents($this->logfile, $logline, FILE_APPEND | LOCK_EX); + } +} diff --git a/src/SessionLogger.php b/src/SessionLogger.php new file mode 100644 index 0000000..b456beb --- /dev/null +++ b/src/SessionLogger.php @@ -0,0 +1,100 @@ +min_level = $min_level; + $this->name = $name; + } + + private function &getSession(): array + { + if (session_status() == PHP_SESSION_NONE) { + session_start(); + } + if (empty($_SESSION[$this->name])) { + $_SESSION[$this->name] = []; + } + return $_SESSION[$this->name]; + } + + public function log($level, $message, array $context = array()) + { + if (!$this->min_level_reached($level)) { + return; + } + $this->getSession()[] = array( + 'timestamp' => date('Y-m-d H:i:s'), + 'level' => $level, + 'message' => $message, + 'context' => $context + ); + } + + /** + * Get all log entries + * + * @param callable|null $formatter An optional formatting function called on every log entry. + * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' + * and must return the new log entry. + * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context', + * unless the log entries are converted to something else by the $formatter parameter. + */ + public function get($formatter = null) + { + $r = $this->getSession(); + if (is_callable($formatter)) { + foreach ($r as $i => $a) { + $r[$i] = call_user_func($formatter, $a); + } + } + return $r; + } + + /** + * Get all log entries and clear the log + * + * @param callable|null $formatter An optional formatting function called on every log entry. + * This formatting function gets an array parameter with keys 'timestamp', 'level', 'message', and 'context' + * and must return the new log entry. + * @return array Array of associative log entry arrays with keys 'timestamp', 'level', 'message', and 'context' + * unless the log entries are converted to something else by the $formatter parameter. + */ + public function getClear($formatter = null) + { + $r = $this->getSession(); + $this->clear(); + if (is_callable($formatter)) { + foreach ($r as $i => $a) { + $r[$i] = call_user_func($formatter, $a); + } + } + return $r; + } + + /** + * Clear the log + * + */ + public function clear() + { + unset($_SESSION[$this->name]); + } + + /** + * Formatter function that can be used as parameter for the get() and getClear() methods + * + * @param array $a + * @return string + */ + public function formatter(array $a) + { + return $this->format($a['level'], $a['message'], $a['context'], $a['timestamp']); + } +} From b2630b3cd2b8d10785c4c36cc936ed156f4fcbd2 Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 17 Dec 2020 11:19:15 -0600 Subject: [PATCH 11/15] Readme updates --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e4ff6d..dff9662 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ If you just need to output a few log messages in a small PHP project but want to ## Installation -- `composer require Midweste/simplelogger` +- `composer require midweste/simplelogger` ## Usage From 98c353e6e151f40614d9a1a8fb5ad314208c4a7e Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 17 Dec 2020 11:24:37 -0600 Subject: [PATCH 12/15] Update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b7f5a8c..bb73309 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.phar composer.lock vendor .idea +*.code-workspace From bd67cffce3fab27147fd825bec027c5371d57b92 Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 17 Dec 2020 12:10:19 -0600 Subject: [PATCH 13/15] Check for empty sessionlogger name --- src/SessionLogger.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SessionLogger.php b/src/SessionLogger.php index b456beb..4c11756 100644 --- a/src/SessionLogger.php +++ b/src/SessionLogger.php @@ -7,10 +7,10 @@ class SessionLogger extends AbstractSimpleLogger { - public function __construct($min_level = LogLevel::DEBUG, $name = 'Midweste\SimpleLogger\SessionLogger') + public function __construct(LogLevel $min_level = LogLevel::DEBUG, $name = '') { $this->min_level = $min_level; - $this->name = $name; + $this->name = (!empty($name) && is_string($name)) ? $name : 'Midweste\SimpleLogger\SessionLogger'; } private function &getSession(): array From 198e19fa44cc6f3cfbe22eb125397cc994f6bb64 Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 17 Dec 2020 12:44:10 -0600 Subject: [PATCH 14/15] prefix session logger name --- src/SessionLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SessionLogger.php b/src/SessionLogger.php index 4c11756..7e7aeef 100644 --- a/src/SessionLogger.php +++ b/src/SessionLogger.php @@ -10,7 +10,7 @@ class SessionLogger extends AbstractSimpleLogger public function __construct(LogLevel $min_level = LogLevel::DEBUG, $name = '') { $this->min_level = $min_level; - $this->name = (!empty($name) && is_string($name)) ? $name : 'Midweste\SimpleLogger\SessionLogger'; + $this->name = (!empty($name) && is_string($name)) ? 'Midweste\SimpleLogger\\' . $name : 'Midweste\SimpleLogger\SessionLogger'; } private function &getSession(): array From ad49a2a082724880d7a7647c9faabf48ce680306 Mon Sep 17 00:00:00 2001 From: midwestE Date: Thu, 17 Dec 2020 12:56:08 -0600 Subject: [PATCH 15/15] Remove param hint for min level --- src/SessionLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SessionLogger.php b/src/SessionLogger.php index 7e7aeef..52c4a9a 100644 --- a/src/SessionLogger.php +++ b/src/SessionLogger.php @@ -7,7 +7,7 @@ class SessionLogger extends AbstractSimpleLogger { - public function __construct(LogLevel $min_level = LogLevel::DEBUG, $name = '') + public function __construct($min_level = LogLevel::DEBUG, $name = '') { $this->min_level = $min_level; $this->name = (!empty($name) && is_string($name)) ? 'Midweste\SimpleLogger\\' . $name : 'Midweste\SimpleLogger\SessionLogger';