Last active
June 11, 2019 06:40
-
-
Save fosron/deaad9436184f27e993ce5bd1c542c80 to your computer and use it in GitHub Desktop.
Trait for Logging to both Console and A Log File (To use with Laravel Artisan Commands)
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 App\Traits; | |
use Monolog\Formatter\FormatterInterface; | |
use Monolog\Formatter\JsonFormatter; | |
use Monolog\Formatter\LineFormatter; | |
use Monolog\Handler\RotatingFileHandler; | |
use Monolog\Logger; | |
use Monolog\Processor\GitProcessor; | |
/** | |
* Trait LoggerTrait | |
* | |
* TODO: Add more methods (critical, emergency, etc...) | |
* | |
* @package App\Traits | |
*/ | |
trait LoggerTrait | |
{ | |
/** | |
* @var \Monolog\Logger|null | |
*/ | |
protected $logger = null; | |
/** | |
* Log Debug Type | |
* | |
* @usage $this->logDebug('Message', $var1, $var2) | |
* | |
* @param $message | |
* @param array $context | |
*/ | |
public function logDebug($message, ...$context): void | |
{ | |
if ($this->option('debug')) { | |
$this->info($message); | |
dump(...$context); | |
} | |
$this->logger->addDebug($message, $context); | |
} | |
/** | |
* Log Info Type | |
* | |
* @usage $this->logDebug('Message', $var1, $var2) | |
* | |
* @param $string | |
* @param array $context | |
*/ | |
public function logInfo($string, ...$context): void | |
{ | |
$this->info($string); | |
$this->logger->addInfo($string, $context); | |
} | |
/** | |
* Log Error Type | |
* | |
* @usage $this->logError('Message', $var1, $var2) | |
* | |
* @param $string | |
* @param array $context | |
*/ | |
public function logError($string, ...$context): void | |
{ | |
$this->error($string); | |
$this->logger->addError($string, $context); | |
} | |
/** | |
* | |
* @usage $this->logWarn('Message', $var1, $var2) | |
* | |
* @param $string | |
* @param array $context | |
*/ | |
public function logWarn($string, ...$context): void | |
{ | |
$this->warn($string); | |
$this->logger->addWarning($string, $context); | |
} | |
/** | |
* @param string $log | |
* @throws \Exception | |
*/ | |
protected function initMonolog($log = 'logger') | |
{ | |
if (!$this->logger) { | |
$formatter = $this->returnFormatter('JsonFormatter'); | |
$handler = new RotatingFileHandler(storage_path("logs/{$log}.log")); | |
$handler->setFormatter($formatter); | |
$this->logger = new Logger($log); | |
$this->logger->pushHandler($handler); | |
$this->logger->pushProcessor(new GitProcessor()); | |
} | |
} | |
/** | |
* @param string $type | |
* @return FormatterInterface | |
* @throws \Exception | |
*/ | |
protected function returnFormatter($type = 'JsonFormatter'): FormatterInterface | |
{ | |
switch ($type) { | |
case 'JsonFormatter': | |
return new JsonFormatter(); | |
break; | |
case 'LineFormatter': | |
return new LineFormatter(null, null, true, true); | |
break; | |
default: | |
throw new \Exception('Formatter type not recognized'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment