diff --git a/src/Enums/LogLevel.php b/src/Enums/LogLevel.php new file mode 100644 index 0000000..72bf28c --- /dev/null +++ b/src/Enums/LogLevel.php @@ -0,0 +1,35 @@ + 'LOG_EMERG', + 'ALERT' => 'LOG_ALERT', + 'CRIT' => 'LOG_CRIT', + 'ERR' => 'LOG_ERR', + 'WARNING' => 'LOG_WARNING', + 'NOTICE' => 'LOG_NOTICE', + 'INFO' => 'LOG_INFO', + 'DEBUG' => 'LOG_DEBUG', + ]; + } +} diff --git a/src/Transport/Response.php b/src/Transport/Response.php index 7934cca..ea51c33 100644 --- a/src/Transport/Response.php +++ b/src/Transport/Response.php @@ -4,6 +4,7 @@ namespace LDC\LDCCRM\Transport; use LDC\LDCCRM\Enums\ApiError; use LDC\LDCCRM\Enums\ApiStatus; +use LDC\LDCCRM\Enums\LogLevel; use LDC\LDCCRM\Exceptions\CrmException; use LDC\LDCCRM\Utils; use Psr\Http\Message\ResponseInterface; @@ -165,10 +166,7 @@ class Response $version, $packageInfo['version'] ); - if (\class_exists('\Illuminate\Support\Facades\Log')) { - \Illuminate\Support\Facades\Log::warning($message); - } - \syslog(\LOG_WARNING, $message); + Utils::log($message, LogLevel::WARNING()); } } } diff --git a/src/Utils.php b/src/Utils.php index 7eda121..0316df5 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -4,6 +4,7 @@ namespace LDC\LDCCRM; use Dotenv\Dotenv; use LDC\LDCCRM\Enums\HttpMethods; +use LDC\LDCCRM\Enums\LogLevel; use LDC\LDCCRM\Exceptions\ValidationException; use LDC\LDCCRM\Transport\Request; use Respect\Validation\Validator; @@ -336,4 +337,45 @@ class Utils } } } + + /** + * Undocumented function + * + * @param string $message + * @return void + */ + public static function log(string $message, LogLevel $level) + { + $message = "LDC CRM: $message"; + if (\class_exists('\Illuminate\Support\Facades\Log')) { + switch ($level) { + case LogLevel::EMERG(): + \Illuminate\Support\Facades\Log::emergency($message); + break; + case LogLevel::ALERT(): + \Illuminate\Support\Facades\Log::alert($message); + break; + case LogLevel::CRIT(): + \Illuminate\Support\Facades\Log::critical($message); + break; + case LogLevel::ERR(): + \Illuminate\Support\Facades\Log::error($message); + break; + case LogLevel::WARNING(): + \Illuminate\Support\Facades\Log::warning($message); + break; + case LogLevel::NOTICE(): + \Illuminate\Support\Facades\Log::notice($message); + break; + case LogLevel::INFO(): + \Illuminate\Support\Facades\Log::info($message); + break; + case LogLevel::DEBUG(): + \Illuminate\Support\Facades\Log::debug($message); + break; + } + \Illuminate\Support\Facades\Log::warning($message); + } + \syslog(\constant($level), $message); + } } diff --git a/src/WordPress/WPCrm.php b/src/WordPress/WPCrm.php index 3263e54..8d709f5 100644 --- a/src/WordPress/WPCrm.php +++ b/src/WordPress/WPCrm.php @@ -15,12 +15,14 @@ namespace LDC\LDCCRM\WordPress; use Exception; use LDC\LDCCRM\CrmApiV2; use LDC\LDCCRM\Enums\ApiError; +use LDC\LDCCRM\Enums\LogLevel; use LDC\LDCCRM\Exceptions\ApiErrorException; use LDC\LDCCRM\Exceptions\CrmException; use LDC\LDCCRM\Models\ArcadiaMailDefault; use LDC\LDCCRM\Models\ArcadiaMailGame; use LDC\LDCCRM\Models\Customer; use LDC\LDCCRM\Models\SingleOptin; +use LDC\LDCCRM\Utils; use ReflectionClass; // phpcs:ignore Squiz.PHP.CommentedOutCode.Found @@ -443,21 +445,15 @@ class WPCrm case ApiError::apiAuthFailed(): throw $e; default: - self::logError($e->getMessage()); + Utils::log($e->getMessage(), LogLevel::NOTICE()); throw $e; } } catch (CrmException $e) { - self::logError($e->getMessage()); + Utils::log($e->getMessage(), LogLevel::EMERG()); return false; } } - private static function logError(string $message): void - { - // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Found - error_log($message); - } - /** * Boot the php session if needed *