-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new EchoLogger, new min_level, ConsoleLogger deprecated
- Loading branch information
Christoph Singer
committed
Mar 7, 2018
1 parent
4af1608
commit 99857ca
Showing
7 changed files
with
151 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
namespace Wa72\SimpleLogger; | ||
|
||
|
||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\LogLevel; | ||
|
||
abstract class AbstractSimpleLogger extends AbstractLogger | ||
{ | ||
protected $min_level = LogLevel::DEBUG; | ||
protected $levels = [ | ||
LogLevel::DEBUG, | ||
LogLevel::INFO, | ||
LogLevel::NOTICE, | ||
LogLevel::WARNING, | ||
LogLevel::ERROR, | ||
LogLevel::CRITICAL, | ||
LogLevel::ALERT, | ||
LogLevel::EMERGENCY | ||
]; | ||
|
||
/** | ||
* @param string $level | ||
* @return boolean | ||
*/ | ||
protected function min_level_reached($level) | ||
{ | ||
return \array_search($level, $this->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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* This is a logger implementing \Psr\Log\LoggerInterface (PSR-3) that just echos the log messages | ||
* | ||
* | ||
* @author Christoph Singer | ||
* @license MIT | ||
*/ | ||
namespace Wa72\SimpleLogger; | ||
use Psr\Log\LogLevel; | ||
|
||
class EchoLogger extends AbstractSimpleLogger | ||
{ | ||
public function __construct($min_level = LogLevel::DEBUG) | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters