-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from joaojacome/master
Split OneLog in two files, onr for static calls and another one PSR-3 compliant
- Loading branch information
Showing
8 changed files
with
220 additions
and
148 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace KoderHut\OnelogBundle\Helper; | ||
|
||
use KoderHut\OnelogBundle\OneLog; | ||
|
||
/** | ||
* Class OneLogStatic | ||
* | ||
* @author Joao Jacome <[email protected]> | ||
*/ | ||
class OneLogStatic | ||
{ | ||
/** | ||
* @var OneLog|null | ||
*/ | ||
private static $resolved; | ||
|
||
/** | ||
* OneLogStatic constructor | ||
*/ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Sets the OneLog instance | ||
* | ||
* @param OneLog $resolved | ||
*/ | ||
public static function setInstance(OneLog $resolved) | ||
{ | ||
self::$resolved = $resolved; | ||
} | ||
|
||
/** | ||
* Returns the OneLog instance | ||
* | ||
* @return OneLog | ||
*/ | ||
public static function instance(): OneLog | ||
{ | ||
if (self::$resolved) { | ||
return self::$resolved; | ||
} | ||
|
||
throw new \RuntimeException('OneLog is not properly instantiated!'); | ||
} | ||
|
||
/** | ||
* Unsets the instance | ||
*/ | ||
public static function destroy() | ||
{ | ||
self::$resolved = null; | ||
} | ||
|
||
/** | ||
* @example OneLog::debug(<string>'message', <array>context) | ||
* | ||
* @param string $level | ||
* @param mixed ...$params | ||
* | ||
* @return mixed | ||
*/ | ||
public static function __callStatic(string $level, $params) | ||
{ | ||
if (!static::$resolved instanceof OneLog) { | ||
throw new \RuntimeException('Logger is not properly instantiated!'); | ||
} | ||
|
||
return self::$resolved->{$level}(...$params); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -11,36 +11,12 @@ | |
* Class OneLog | ||
* | ||
* @author Denis-Florin Rendler <[email protected]> | ||
* | ||
* @method emergency() | ||
* @method static emergency() | ||
* @method alert() | ||
* @method static alert() | ||
* @method critical() | ||
* @method static critical() | ||
* @method error() | ||
* @method static error() | ||
* @method warning() | ||
* @method static warning() | ||
* @method notice() | ||
* @method static notice() | ||
* @method info() | ||
* @method static info() | ||
* @method debug() | ||
* @method static debug() | ||
* @method log() | ||
* @method static log() | ||
* | ||
* @property LoggerInterface $default | ||
*/ | ||
class OneLog | ||
{ | ||
public const DEFAULT_LOGGER = 'default'; | ||
use PSRLoggerTrait; | ||
|
||
/** | ||
* @var OneLog|null | ||
*/ | ||
private static $resolved; | ||
public const DEFAULT_LOGGER = 'default'; | ||
|
||
/** | ||
* @var LoggerInterface|NullLogger | ||
|
@@ -67,65 +43,6 @@ public function __construct(LoggerInterface $default = null, LoggerInterface ... | |
foreach ($logger as $loggerInstance) { | ||
$this->registerLogger($loggerInstance); | ||
} | ||
|
||
if (self::$resolved !== $this) { | ||
self::$resolved = null; | ||
self::$resolved = $this; | ||
} | ||
} | ||
|
||
/** | ||
* @example OneLog::debug(<string>'message', <array>context) | ||
* | ||
* @param string $level | ||
* @param mixed ...$params | ||
* | ||
* @return mixed | ||
*/ | ||
public static function __callStatic(string $level, $params) | ||
{ | ||
if (!static::$resolved instanceof self) { | ||
throw new \RuntimeException('Logger is not properly instantiated!'); | ||
} | ||
|
||
return self::$resolved->__call($level, $params); | ||
} | ||
|
||
/** | ||
* Returns the OneLog instance | ||
* | ||
* @return OneLog | ||
*/ | ||
public static function instance(): OneLog | ||
{ | ||
if (self::$resolved) { | ||
return self::$resolved; | ||
} | ||
|
||
throw new \RuntimeException('OneLog is not properly instantiated!'); | ||
} | ||
|
||
/** | ||
* Make sure we clear the static instance as well | ||
*/ | ||
public function __destruct() | ||
{ | ||
self::$resolved = null; | ||
} | ||
|
||
/** | ||
* Proxy for logger methods on default logger instance | ||
* | ||
* @example $instance->debug(<string>'message', <array>context) | ||
* | ||
* @param string $level | ||
* @param array $params | ||
* | ||
* @return bool | ||
*/ | ||
public function __call(string $level, array $params): bool | ||
{ | ||
return $this->defaultLogger->{$level}(...$params); | ||
} | ||
|
||
/** | ||
|
@@ -164,11 +81,11 @@ public function loggers(): array | |
public function registerLogger(LoggerInterface $logger, $name = null): void | ||
{ | ||
$loggerName = $name ?? spl_object_hash($logger); | ||
|
||
if (null === $name && ($logger instanceof Logger || $logger instanceof NamedLoggerInterface)) { | ||
$loggerName = $logger->getName(); | ||
} | ||
|
||
$this->loggers[$loggerName] = $logger; | ||
} | ||
} |
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,93 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace KoderHut\OnelogBundle; | ||
|
||
/** | ||
* Trait PSRLoggerTrait | ||
* | ||
* @author Joao Jacome <[email protected]> | ||
*/ | ||
trait PSRLoggerTrait | ||
{ | ||
/** | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function emergency($message, array $context = array()) | ||
{ | ||
$this->defaultLogger->emergency($message, $context); | ||
} | ||
|
||
/** | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function alert($message, array $context = array()) | ||
{ | ||
$this->defaultLogger->alert($message, $context); | ||
} | ||
|
||
/** | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function critical($message, array $context = array()) | ||
{ | ||
$this->defaultLogger->critical($message, $context); | ||
} | ||
|
||
/** | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function error($message, array $context = array()) | ||
{ | ||
$this->defaultLogger->error($message, $context); | ||
} | ||
|
||
/** | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function warning($message, array $context = array()) | ||
{ | ||
$this->defaultLogger->warning($message, $context); | ||
} | ||
|
||
/** | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function notice($message, array $context = array()) | ||
{ | ||
$this->defaultLogger->notice($message, $context); | ||
} | ||
|
||
/** | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function info($message, array $context = array()) | ||
{ | ||
$this->defaultLogger->info($message, $context); | ||
} | ||
|
||
/** | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function debug($message, array $context = array()) | ||
{ | ||
$this->defaultLogger->debug($message, $context); | ||
} | ||
|
||
/** | ||
* @param mixed $level | ||
* @param mixed $message | ||
* @param array $context | ||
*/ | ||
public function log($level, $message, array $context = array()) | ||
{ | ||
return $this->defaultLogger->log($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
Oops, something went wrong.