diff --git a/config/common.yml b/config/common.yml index 43ffa7e22..adf37dd99 100644 --- a/config/common.yml +++ b/config/common.yml @@ -17,7 +17,9 @@ services: ps_accounts.logger: class: PrestaShop\Module\PsAccounts\Vendor\Monolog\Logger public: true - factory: [ 'PrestaShop\Module\PsAccounts\Factory\PsAccountsLogger', 'create' ] + factory: [ 'PrestaShop\Module\PsAccounts\Log\Logger', 'create' ] +# arguments: +# - '%ps_accounts.log_level%' PrestaShop\Module\PsAccounts\Provider\OAuth2\ShopProvider: class: PrestaShop\Module\PsAccounts\Provider\OAuth2\ShopProvider diff --git a/config/config.yml.dist b/config/config.yml.dist index 7b68dedc9..5ec883625 100644 --- a/config/config.yml.dist +++ b/config/config.yml.dist @@ -32,3 +32,7 @@ parameters: # Login page testimonials url ps_accounts.testimonials_url: 'https://assets.prestashop3.com/dst/accounts/assets/testimonials.json' + # optional log level (defaults to ERROR) + #ps_accounts.log_level: !php/const PrestaShop\Module\PsAccounts\Log\Logger::ERROR + ps_accounts.log_level: ERROR + diff --git a/src/Factory/PsAccountsLogger.php b/src/Factory/PsAccountsLogger.php deleted file mode 100644 index d4e6bd8d9..000000000 --- a/src/Factory/PsAccountsLogger.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ - -namespace PrestaShop\Module\PsAccounts\Factory; - -use PrestaShop\Module\PsAccounts\Vendor\Monolog\Handler\RotatingFileHandler; -use PrestaShop\Module\PsAccounts\Vendor\Monolog\Logger; - -class PsAccountsLogger -{ - const MAX_FILES = 15; - - /** - * Create logger. - * - * @return Logger - */ - public static function create() - { - $path = _PS_ROOT_DIR_ . '/var/logs/ps_accounts'; - - if (version_compare(_PS_VERSION_, '1.7', '<')) { - $path = _PS_ROOT_DIR_ . '/log/ps_accounts'; - } elseif (version_compare(_PS_VERSION_, '1.7.4', '<')) { - $path = _PS_ROOT_DIR_ . '/app/logs/ps_accounts'; - } - - $rotatingFileHandler = new RotatingFileHandler( - $path, - static::MAX_FILES - ); - $logger = new Logger('ps_accounts'); - $logger->pushHandler($rotatingFileHandler); - - return $logger; - } -} diff --git a/src/Hook/ActionObjectShopDeleteBefore.php b/src/Hook/ActionObjectShopDeleteBefore.php index ebc169dfc..ec672429e 100644 --- a/src/Hook/ActionObjectShopDeleteBefore.php +++ b/src/Hook/ActionObjectShopDeleteBefore.php @@ -38,17 +38,17 @@ public function execute(array $params = []) $response = $this->commandBus->handle(new DeleteUserShopCommand($params['object']->id)); if (!$response) { - $this->module->getLogger()->debug( + $this->module->getLogger()->error( 'Error trying to DELETE shop : No $response object' ); } elseif (true !== $response['status']) { - $this->module->getLogger()->debug( + $this->module->getLogger()->error( 'Error trying to DELETE shop : ' . $response['httpCode'] . ' ' . print_r($response['body']['message'], true) ); } } catch (Exception $e) { - $this->module->getLogger()->debug( + $this->module->getLogger()->error( 'Error while trying to DELETE shop : ' . print_r($e->getMessage(), true) ); } diff --git a/src/Hook/ActionObjectShopUpdateAfter.php b/src/Hook/ActionObjectShopUpdateAfter.php index 4db0367c1..4a6c60364 100644 --- a/src/Hook/ActionObjectShopUpdateAfter.php +++ b/src/Hook/ActionObjectShopUpdateAfter.php @@ -71,14 +71,14 @@ protected function updateUserShop(\Shop $shop) ]))); if (!$response) { - $this->module->getLogger()->debug('Error trying to PATCH shop : No $response object'); + $this->module->getLogger()->error('Error trying to PATCH shop : No $response object'); } elseif (true !== $response['status']) { - $this->module->getLogger()->debug('Error trying to PATCH shop : ' . $response['httpCode'] . + $this->module->getLogger()->error('Error trying to PATCH shop : ' . $response['httpCode'] . ' ' . print_r(isset($response['body']['message']) ? $response['body']['message'] : '', true) ); } } catch (Exception $e) { - $this->module->getLogger()->debug('Error trying to PATCH shop: ' . $e->getMessage()); + $this->module->getLogger()->error('Error trying to PATCH shop: ' . $e->getMessage()); } } } diff --git a/src/Http/Client/Guzzle/GuzzleClient.php b/src/Http/Client/Guzzle/GuzzleClient.php index 19ec0214f..6e1455a18 100644 --- a/src/Http/Client/Guzzle/GuzzleClient.php +++ b/src/Http/Client/Guzzle/GuzzleClient.php @@ -213,14 +213,14 @@ public function setRoute($route) private function logResponseError(array $response, array $options) { // If response is not successful only - if (\Configuration::get('PS_ACCOUNTS_DEBUG_LOGS_ENABLED') && !$response['status']) { + if (!$response['status']) { /** @var \Ps_accounts $module */ $module = \Module::getInstanceByName('ps_accounts'); try { $logger = $module->getLogger(); - $logger->debug('route ' . $this->getRoute()); - $logger->debug('options ' . var_export($options, true)); - $logger->debug('response ' . var_export($response, true)); + $logger->error('route ' . $this->getRoute()); + $logger->error('options ' . var_export($options, true)); + $logger->error('response ' . var_export($response, true)); } catch (\Exception $e) { } } diff --git a/src/Log/Logger.php b/src/Log/Logger.php index ab936f186..e14be4890 100644 --- a/src/Log/Logger.php +++ b/src/Log/Logger.php @@ -20,12 +20,52 @@ namespace PrestaShop\Module\PsAccounts\Log; +use PrestaShop\Module\PsAccounts\Vendor\Monolog\Handler\RotatingFileHandler; +use PrestaShop\Module\PsAccounts\Vendor\Monolog\Logger as MonoLogger; use Ps_accounts; class Logger { + const DEBUG = 'DEBUG'; + const INFO = 'INFO'; + const NOTICE = 'NOTICE'; + const WARNING = 'WARNING'; + const ERROR = 'ERROR'; + const CRITICAL = 'CRITICAL'; + const ALERT = 'ALERT'; + const EMERGENCY = 'EMERGENCY'; + const MAX_FILES = 15; + + /** + * @param string|null $logLevel + * + * @return MonoLogger + */ + public static function create($logLevel = null) + { + $logLevel = self::getLevel($logLevel); + $monologLevel = MonoLogger::toMonologLevel($logLevel); + if (!is_int($monologLevel)) { + $monologLevel = MonoLogger::ERROR; + } + + $path = _PS_ROOT_DIR_ . '/var/logs/ps_accounts'; + + if (version_compare(_PS_VERSION_, '1.7', '<')) { + $path = _PS_ROOT_DIR_ . '/log/ps_accounts'; + } elseif (version_compare(_PS_VERSION_, '1.7.4', '<')) { + $path = _PS_ROOT_DIR_ . '/app/logs/ps_accounts'; + } + + $rotatingFileHandler = new RotatingFileHandler($path, static::MAX_FILES, $monologLevel); + $logger = new MonoLogger('ps_accounts'); + $logger->pushHandler($rotatingFileHandler); + + return $logger; + } + /** - * @return \PrestaShop\Module\PsAccounts\Vendor\Monolog\Logger + * @return Monologger */ public static function getInstance() { @@ -34,4 +74,23 @@ public static function getInstance() return $psAccounts->getLogger(); } + + /** + * @param string|null $logLevel + * @param string $parameter + * + * @return mixed + */ + public static function getLevel($logLevel, $parameter = 'ps_accounts.log_level') + { + if ($logLevel === null) { + /** @var Ps_accounts $psAccounts */ + $psAccounts = \Module::getInstanceByName('ps_accounts'); + if ($psAccounts->hasParameter($parameter)) { + $logLevel = $psAccounts->getParameter($parameter); + } + } + + return $logLevel; + } } diff --git a/src/Repository/ConfigurationRepository.php b/src/Repository/ConfigurationRepository.php index 53f440698..d808e5169 100644 --- a/src/Repository/ConfigurationRepository.php +++ b/src/Repository/ConfigurationRepository.php @@ -371,7 +371,7 @@ public function updateAccessToken($accessToken) */ public function fixMultiShopConfig() { - Logger::getInstance()->error(__METHOD__); + Logger::getInstance()->info(__METHOD__); if ($this->isMultishopActive()) { $this->migrateToMultiShop();