diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2017-09-19 18:30:42 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2017-09-19 18:30:42 +0200 |
commit | b3b65743cdc534f7632c8e04e80d3489eec9677c (patch) | |
tree | 0edadd49942cb7647787ee9360a1382f4294b457 /src/Logger/EngelsystemLogger.php | |
parent | 20a6fa07f8a94a587aa930659d740b1d346dbb8c (diff) |
Added Logger
Diffstat (limited to 'src/Logger/EngelsystemLogger.php')
-rw-r--r-- | src/Logger/EngelsystemLogger.php | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/Logger/EngelsystemLogger.php b/src/Logger/EngelsystemLogger.php new file mode 100644 index 00000000..db46215c --- /dev/null +++ b/src/Logger/EngelsystemLogger.php @@ -0,0 +1,74 @@ +<?php + +namespace Engelsystem\Logger; + +use Psr\Log\AbstractLogger; +use Psr\Log\InvalidArgumentException; +use Psr\Log\LogLevel; + +class EngelsystemLogger extends AbstractLogger +{ + protected $allowedLevels = [ + LogLevel::ALERT, + LogLevel::CRITICAL, + LogLevel::DEBUG, + LogLevel::EMERGENCY, + LogLevel::ERROR, + LogLevel::INFO, + LogLevel::NOTICE, + LogLevel::WARNING, + ]; + + /** + * Logs with an arbitrary level. + * + * @TODO: Implement $context['exception'] + * + * @param mixed $level + * @param string $message + * @param array $context + * + * @throws InvalidArgumentException + */ + public function log($level, $message, array $context = []) + { + if (!$this->checkLevel($level)) { + throw new InvalidArgumentException(); + } + + $message = $this->interpolate($message, $context); + + LogEntry_create('Logger: ' . $level, $message); + } + + /** + * Interpolates context values into the message placeholders. + * + * @param string $message + * @param array $context + * @return string + */ + protected function interpolate($message, array $context = []) + { + foreach ($context as $key => $val) { + // check that the value can be casted to string + if (is_array($val) || (is_object($val) && !method_exists($val, '__toString'))) { + continue; + } + + // replace the values of the message + $message = str_replace('{' . $key . '}', $val, $message); + } + + return $message; + } + + /** + * @param string $level + * @return bool + */ + protected function checkLevel($level) + { + return in_array($level, $this->allowedLevels); + } +} |