Skip to content

Commit

Permalink
[ACCOUNT-2710] feat: add log level parameter (#437)
Browse files Browse the repository at this point in the history
* feat: add log level configuration parameter

* fix: log levels

* fix: remove obsolete logging condition

* refactor: avoid referencing constant from vendor

* refactor: make log_level parameter definition optional

* fix: default log level to error
  • Loading branch information
hschoenenberger authored Oct 25, 2024
1 parent e92be2b commit b1bde56
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 67 deletions.
4 changes: 3 additions & 1 deletion config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions config/config.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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

54 changes: 0 additions & 54 deletions src/Factory/PsAccountsLogger.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/Hook/ActionObjectShopDeleteBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Hook/ActionObjectShopUpdateAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
8 changes: 4 additions & 4 deletions src/Http/Client/Guzzle/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}
Expand Down
61 changes: 60 additions & 1 deletion src/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Repository/ConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function updateAccessToken($accessToken)
*/
public function fixMultiShopConfig()
{
Logger::getInstance()->error(__METHOD__);
Logger::getInstance()->info(__METHOD__);

if ($this->isMultishopActive()) {
$this->migrateToMultiShop();
Expand Down

0 comments on commit b1bde56

Please sign in to comment.