Created
October 19, 2015 15:22
-
-
Save toby-griffiths/ada114b66b6a09b80201 to your computer and use it in GitHub Desktop.
PSR-3 Logger trait
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
<?php | |
namespace CubicMushroom; | |
use Psr\Log\LoggerInterface; | |
/** | |
* Class LoggerTrait | |
* | |
* @package LIMTool\PaymentsBundle | |
*/ | |
trait LoggerTrait | |
{ | |
/** | |
* @var LoggerInterface | |
*/ | |
protected $logger; | |
/** | |
* System is unusable. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logEmergency($message, array $context = []) | |
{ | |
$this->logger->emergency($message, $context); | |
} | |
/** | |
* Action must be taken immediately. | |
* | |
* Example: Entire website down, database unavailable, etc. This should | |
* trigger the SMS alerts and wake you up. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logAlert($message, array $context = []) | |
{ | |
$this->logger->alert($message, $context); | |
} | |
/** | |
* Critical conditions. | |
* | |
* Example: Application component unavailable, unexpected exception. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logCritical($message, array $context = []) | |
{ | |
$this->logger->critical($message, $context); | |
} | |
/** | |
* Runtime errors that do not require immediate action but should typically | |
* be logged and monitored. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logError($message, array $context = []) | |
{ | |
$this->logger->error($message, $context); | |
} | |
/** | |
* Exceptional occurrences that are not errors. | |
* | |
* Example: Use of deprecated APIs, poor use of an API, undesirable things | |
* that are not necessarily wrong. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logWarning($message, array $context = []) | |
{ | |
$this->logger->warning($message, $context); | |
} | |
/** | |
* Normal but significant events. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logNotice($message, array $context = []) | |
{ | |
$this->logger->notice($message, $context); | |
} | |
/** | |
* Interesting events. | |
* | |
* Example: User logs in, SQL logs. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logInfo($message, array $context = []) | |
{ | |
$this->logger->info($message, $context); | |
} | |
/** | |
* Detailed debug information. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logDebug($message, array $context = []) | |
{ | |
$this->logger->debug($message, $context); | |
} | |
/** | |
* Logs with an arbitrary level. | |
* | |
* @param mixed $level | |
* @param string $message | |
* @param array $context | |
* | |
* @return null | |
*/ | |
protected function logLog($level, $message, array $context = []) | |
{ | |
$this->logger->log($level, $message, $context); | |
} | |
// ----------------------------------------------------------------------------------------------------------------- | |
// Setters | |
// ----------------------------------------------------------------------------------------------------------------- | |
/** | |
* @param LoggerInterface $logger | |
*/ | |
public function setLogger(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment