فهرست منبع

added loggerExt class
new class checks params and adjusts if needed

CauseFX 4 سال پیش
والد
کامیت
e8b84c083f
2فایلهای تغییر یافته به همراه142 افزوده شده و 3 حذف شده
  1. 2 3
      api/classes/logger.class.php
  2. 140 0
      api/classes/loggerExt.class.php

+ 2 - 3
api/classes/logger.class.php

@@ -1,6 +1,5 @@
 <?php
 
-use Nekonomokochan\PhpJsonLogger\Logger;
 use Nekonomokochan\PhpJsonLogger\LoggerBuilder;
 
 class OrganizrLogger extends LoggerBuilder
@@ -23,13 +22,13 @@ class OrganizrLogger extends LoggerBuilder
 		$this->isReady = $readyStatus;
 	}
 	
-	public function build(): Logger
+	public function build(): OrganizrLoggerExt
 	{
 		if (!$this->isReady) {
 			$this->setChannel('Organizr');
 			$this->setLogLevel(self::DEBUG);
 			$this->setMaxFiles(1);
 		}
-		return new Logger($this);
+		return new OrganizrLoggerExt($this);
 	}
 }

+ 140 - 0
api/classes/loggerExt.class.php

@@ -0,0 +1,140 @@
+<?php
+
+use Nekonomokochan\PhpJsonLogger\Logger;
+
+class OrganizrLoggerExt extends Logger
+{
+	/**
+	 * @param $message
+	 * @param $context
+	 */
+	public function debug($message, $context = '')
+	{
+		$context = $this->formatParamToArray($context);
+		$this->addDebug($message, $context);
+	}
+	
+	/**
+	 * @param $message
+	 * @param $context
+	 */
+	public function info($message, $context = '')
+	{
+		$context = $this->formatParamToArray($context);
+		$this->addInfo($message, $context);
+	}
+	
+	/**
+	 * @param $message
+	 * @param $context
+	 */
+	public function notice($message, $context = '')
+	{
+		$context = $this->formatParamToArray($context);
+		$this->addNotice($message, $context);
+	}
+	
+	/**
+	 * @param $message
+	 * @param $context
+	 */
+	public function warning($message, $context = '')
+	{
+		$context = $this->formatParamToArray($context);
+		$this->addWarning($message, $context);
+	}
+	
+	/**
+	 * @param \Throwable $e
+	 * @param            $context
+	 */
+	public function error($e, $context = '')
+	{
+		$context = $this->formatParamToArray($context);
+		if ($this->isErrorObject($e) === false) {
+			throw new \InvalidArgumentException(
+				$this->generateInvalidArgumentMessage(__METHOD__)
+			);
+		}
+		$this->addError(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
+	}
+	
+	/**
+	 * @param \Throwable $e
+	 * @param            $context
+	 */
+	public function critical($e, $context = '')
+	{
+		$context = $this->formatParamToArray($context);
+		if ($this->isErrorObject($e) === false) {
+			throw new \InvalidArgumentException(
+				$this->generateInvalidArgumentMessage(__METHOD__)
+			);
+		}
+		$this->addCritical(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
+	}
+	
+	/**
+	 * @param \Throwable $e
+	 * @param            $context
+	 */
+	public function alert($e, $context = '')
+	{
+		$context = $this->formatParamToArray($context);
+		if ($this->isErrorObject($e) === false) {
+			throw new \InvalidArgumentException(
+				$this->generateInvalidArgumentMessage(__METHOD__)
+			);
+		}
+		$this->addAlert(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
+	}
+	
+	/**
+	 * @param \Throwable $e
+	 * @param            $context
+	 */
+	public function emergency($e, $context = '')
+	{
+		$context = $this->formatParamToArray($context);
+		if ($this->isErrorObject($e) === false) {
+			throw new \InvalidArgumentException(
+				$this->generateInvalidArgumentMessage(__METHOD__)
+			);
+		}
+		$this->addEmergency(get_class($e), $this->formatPhpJsonLoggerErrorsContext($e, $context));
+	}
+	
+	/**
+	 * @param $value
+	 * @return bool
+	 */
+	private function isErrorObject($value): bool
+	{
+		if ($value instanceof \Exception || $value instanceof \Error) {
+			return true;
+		}
+		return false;
+	}
+	
+	/**
+	 * @param $value
+	 * @return array
+	 */
+	private function formatParamToArray($value): array
+	{
+		if (is_array($value)) {
+			return $value;
+		} else {
+			return (empty($value)) ? [] : ['context' => $value];
+		}
+	}
+	
+	/**
+	 * @param string $method
+	 * @return string
+	 */
+	private function generateInvalidArgumentMessage(string $method): string
+	{
+		return 'Please give the exception class to the ' . $method;
+	}
+}