Add Utils::log with enum LogLevel (prefix logs)

This commit is contained in:
Jérémy Munsch 2021-09-14 13:44:03 +02:00
parent 9aa309f57b
commit 8136ac38bd
4 changed files with 83 additions and 12 deletions

35
src/Enums/LogLevel.php Normal file
View file

@ -0,0 +1,35 @@
<?php
namespace LDC\LDCCRM\Enums;
use Kwaadpepper\Enum\BaseEnum;
/**
* @method static self EMERG() system is unusable
* @method static self ALERT() action must be taken immediately
* @method static self CRIT() critical conditions
* @method static self ERR() critical conditions
* @method static self WARNING() critical conditions
* @method static self NOTICE() normal, but significant, condition
* @method static self INFO() informational message
* @method static self DEBUG() debug-level message
*
* @package LDCCrmClient
* @subpackage enums
*/
class LogLevel extends BaseEnum
{
protected static function values()
{
return [
'EMERG' => 'LOG_EMERG',
'ALERT' => 'LOG_ALERT',
'CRIT' => 'LOG_CRIT',
'ERR' => 'LOG_ERR',
'WARNING' => 'LOG_WARNING',
'NOTICE' => 'LOG_NOTICE',
'INFO' => 'LOG_INFO',
'DEBUG' => 'LOG_DEBUG',
];
}
}

View file

@ -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());
}
}
}

View file

@ -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);
}
}

View file

@ -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
*