Created
January 31, 2019 21:39
-
-
Save kevyworks/de7fe56bb9b0e0f90e4c157f8a3ad984 to your computer and use it in GitHub Desktop.
Custom Laravel Logger Class
This file contains hidden or 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 My\Namespace\Libraries; | |
use InvalidArgumentException; | |
use Monolog\Formatter\LineFormatter; | |
use Monolog\Handler\RotatingFileHandler; | |
use Monolog\Logger; | |
use Monolog\Logger as MonologLogger; | |
use Monolog\Handler\StreamHandler; | |
/** | |
* Class CustomLogger | |
* | |
* @author Kevyworks <[email protected]> | |
* @package Modules\Ecs\Libraries | |
*/ | |
class CustomLogger | |
{ | |
/** | |
* The Monolog logger instance. | |
* | |
* @var \Monolog\Logger | |
*/ | |
protected $monolog; | |
/** | |
* Filename to write logs | |
* | |
* @var string | |
*/ | |
protected $filename; | |
/** | |
* Log Level | |
* | |
* @var string | |
*/ | |
protected $level; | |
/** | |
* Log Name | |
* | |
* @var string | |
*/ | |
protected $loggerName; | |
/** | |
* The Log levels. | |
* | |
* @var array | |
*/ | |
protected $levels = [ | |
'debug' => MonologLogger::DEBUG, | |
'info' => MonologLogger::INFO, | |
'notice' => MonologLogger::NOTICE, | |
'warning' => MonologLogger::WARNING, | |
'error' => MonologLogger::ERROR, | |
'critical' => MonologLogger::CRITICAL, | |
'alert' => MonologLogger::ALERT, | |
'emergency' => MonologLogger::EMERGENCY, | |
]; | |
/** | |
* CustomLogger constructor. | |
* | |
* @param string $path Log filename "mylog.log" saved at /storage/logs or absolute file path | |
* @param string|null $name Defaults to "Custom Log" | |
* @param string $level Level | |
* @param boolean $useDaily Set to daily file naming | |
* @param int $days Max Days | |
* | |
* @throws \Exception | |
*/ | |
public function __construct($path, $name = null, $level = 'debug', $useDaily = false, $days = 0) | |
{ | |
$this->loggerName = $name ?: ($useDaily ? 'Custom Daily Log' : 'Custom Log'); | |
$this->level = $this->parseLevel($level ?: 'debug'); | |
$this->filename = $this->normalizePath($path); | |
$this->monolog = new Logger($this->loggerName); | |
if ($useDaily) { | |
$this->useDailyFiles($days); | |
} else { | |
$this->useFiles(); | |
} | |
} | |
/** | |
* Register a file log handler. | |
* | |
* @return void | |
* @throws \Exception | |
*/ | |
protected function useFiles() | |
{ | |
$this->monolog->pushHandler( | |
$handler = new StreamHandler($this->filename, $this->level, false) | |
); | |
$handler->setFormatter($this->getDefaultFormatter()); | |
} | |
/** | |
* Register a daily file log handler. | |
* | |
* @param int $days Max Days | |
* | |
* @return void | |
*/ | |
protected function useDailyFiles($days = 0) { | |
$this->monolog->pushHandler( | |
$handler = new RotatingFileHandler($this->filename, $days, $this->level, false) | |
); | |
$handler->setFormatter($this->getDefaultFormatter()); | |
} | |
/** | |
* Log an emergency message to the logs. | |
* | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function emergency($message, array $context = []) | |
{ | |
return $this->writeLog(__FUNCTION__, $message, $context); | |
} | |
/** | |
* Log an alert message to the logs. | |
* | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function alert($message, array $context = []) | |
{ | |
return $this->writeLog(__FUNCTION__, $message, $context); | |
} | |
/** | |
* Log a critical message to the logs. | |
* | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function critical($message, array $context = []) | |
{ | |
return $this->writeLog(__FUNCTION__, $message, $context); | |
} | |
/** | |
* Log an error message to the logs. | |
* | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function error($message, array $context = []) | |
{ | |
return $this->writeLog(__FUNCTION__, $message, $context); | |
} | |
/** | |
* Log a warning message to the logs. | |
* | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function warning($message, array $context = []) | |
{ | |
return $this->writeLog(__FUNCTION__, $message, $context); | |
} | |
/** | |
* Log a notice to the logs. | |
* | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function notice($message, array $context = []) | |
{ | |
return $this->writeLog(__FUNCTION__, $message, $context); | |
} | |
/** | |
* Log an informational message to the logs. | |
* | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function info($message, array $context = []) | |
{ | |
return $this->writeLog(__FUNCTION__, $message, $context); | |
} | |
/** | |
* Log a debug message to the logs. | |
* | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function debug($message, array $context = []) | |
{ | |
return $this->writeLog(__FUNCTION__, $message, $context); | |
} | |
/** | |
* Log a message to the logs. | |
* | |
* @param string $level | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function log($level, $message, array $context = []) | |
{ | |
return $this->writeLog($level, $message, $context); | |
} | |
/** | |
* Dynamically pass log calls into the writer. | |
* | |
* @param string $level | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
public function write($level, $message, array $context = []) | |
{ | |
return $this->writeLog($level, $message, $context); | |
} | |
/** | |
* Write a message to Monolog. | |
* | |
* @param string $level | |
* @param string $message | |
* @param array $context | |
* @return Boolean Whether the record has been processed | |
*/ | |
protected function writeLog($level, $message, $context) | |
{ | |
return $this->monolog->{$level}($message, $context); | |
} | |
/** | |
* Check path is a absolute directory or just a filename | |
* | |
* @param $path | |
* | |
* @return string | |
*/ | |
protected function normalizePath($path) { | |
return !is_dir(dirname($path)) ? $path : storage_path("/logs/" . $path); | |
} | |
/** | |
* Parse the string level into a Monolog constant. | |
* | |
* @param string $level | |
* @return int | |
* | |
* @throws \InvalidArgumentException | |
*/ | |
protected function parseLevel($level) | |
{ | |
if (isset($this->levels[$level])) { | |
return $this->levels[$level]; | |
} | |
throw new InvalidArgumentException('Invalid log level.'); | |
} | |
/** | |
* Get a default Monolog formatter instance. | |
* | |
* @return \Monolog\Formatter\LineFormatter | |
*/ | |
protected function getDefaultFormatter() | |
{ | |
return new LineFormatter(null, null, true, true); | |
} | |
} |
This file contains hidden or 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 | |
$logger = new CustomLogger("my.log", "CustomLog", "info"); | |
$logger->info("Hello World!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment