Skip to content

Commit

Permalink
Merge pull request #249 from Matt75/fix-logs
Browse files Browse the repository at this point in the history
Fix logs
  • Loading branch information
Matt75 authored Jan 9, 2020
2 parents 1f0d52b + 070c94c commit 6f48327
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 12 deletions.
21 changes: 21 additions & 0 deletions _dev/src/pages/Debug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,26 @@
<li>Shop ID: <b>{{ shopId }}</b></li>
<li>Rounding config: <b>{{ roundingSettingsIsCorrect }}</b></li>
</ul>
<PSSwitch id="hostedFieldsAvailability" v-model="debugLogsEnabled">
<template v-if="debugLogsEnabled">
Logs enabled
</template>
<template v-else>
Logs disabled
</template>
</PSSwitch>
</b-card-body>
</b-card>
</b-container>
</template>

<script>
import PSSwitch from '@/components/form/switch';

export default {
components: {
PSSwitch,
},
name: 'Debug',
computed: {
moduleVersion() {
Expand All @@ -50,6 +63,14 @@
roundingSettingsIsCorrect() {
return this.$store.getters.roundingSettingsIsCorrect;
},
debugLogsEnabled: {
get() {
return this.$store.state.configuration.debugLogsEnabled;
},
set(payload) {
this.$store.dispatch('toggleDebugLogs', payload);
},
},
},
};
</script>
13 changes: 13 additions & 0 deletions _dev/src/store/modules/configuration/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,17 @@ export default {
return Promise.resolve(payload);
});
},

toggleDebugLogs({commit, getters}, payload) {
return ajax({
url: getters.adminController,
action: 'ToggleDebugLogs',
data: {
status: payload ? 1 : 0,
},
}).then(() => {
commit(types.UPDATE_DEBUG_LOGS, payload);
return Promise.resolve(payload);
});
},
};
1 change: 1 addition & 0 deletions _dev/src/store/modules/configuration/mutation-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export const UPDATE_PAYMENT_CARD_AVAILABILITY = 'UPDATE_PAYMENT_CARD_AVAILABILIT
export const UPDATE_EC_ORDER_PAGE = 'UPDATE_EC_ORDER_PAGE';
export const UPDATE_EC_CHECKOUT_PAGE = 'UPDATE_EC_CHECKOUT_PAGE';
export const UPDATE_EC_PRODUCT_PAGE = 'UPDATE_EC_PRODUCT_PAGE';
export const UPDATE_DEBUG_LOGS = 'UPDATE_DEBUG_LOGS';
3 changes: 3 additions & 0 deletions _dev/src/store/modules/configuration/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ export default {
[types.UPDATE_EC_PRODUCT_PAGE](state, payload) {
state.expressCheckout.productPage = payload;
},
[types.UPDATE_DEBUG_LOGS](state, payload) {
state.debugLogsEnabled = payload;
},
};
21 changes: 12 additions & 9 deletions classes/Api/GenericClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,21 @@ protected function post(array $options = [])
{
$response = $this->getClient()->post($this->getRoute(), $options);

/**
* @var \Ps_checkout
*/
$module = \Module::getInstanceByName('ps_checkout');
$logger = $module->getLogger();
$logger->debug('route ' . $this->getRoute());
$logger->debug('options ' . var_export($options, true));

$responseHandler = new ResponseApiHandler();

$response = $responseHandler->handleResponse($response);
$logger->debug('response ' . var_export($response, true));

// If response is not successful only
if (\Configuration::get('PS_CHECKOUT_DEBUG_LOGS_ENABLED') && !$response['status']) {
/**
* @var \Ps_checkout
*/
$module = \Module::getInstanceByName('ps_checkout');
$logger = $module->getLogger();
$logger->debug('route ' . $this->getRoute());
$logger->debug('options ' . var_export($options, true));
$logger->debug('response ' . var_export($response, true));
}

return $response;
}
Expand Down
8 changes: 6 additions & 2 deletions classes/Factory/CheckoutLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@

class CheckoutLogger
{
const MAX_FILES = 30;
const MAX_FILES = 15;

public static function create()
{
$path = version_compare(_PS_VERSION_, '1.7.4', '>=')
? _PS_ROOT_DIR_ . '/var/logs/ps_checkout'
: _PS_ROOT_DIR_ . '/app/logs/ps_checkout'
;
$rotatingFileHandler = new RotatingFileHandler(
_PS_ROOT_DIR_ . '/var/logs/ps_checkout',
$path,
static::MAX_FILES
);
$logger = new Logger('ps_checkout');
Expand Down
1 change: 1 addition & 0 deletions classes/Presenter/Store/Modules/ConfigurationModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function present()
'captureMode' => \Configuration::get('PS_CHECKOUT_INTENT'),
'paymentMode' => \Configuration::get('PS_CHECKOUT_MODE'),
'cardIsEnabled' => (bool) \Configuration::get('PS_CHECKOUT_CARD_PAYMENT_ENABLED'),
'debugLogsEnabled' => (bool) \Configuration::get('PS_CHECKOUT_DEBUG_LOGS_ENABLED'),
'expressCheckout' => [
'orderPage' => (bool) \Configuration::get('PS_CHECKOUT_EC_ORDER_PAGE'),
'checkoutPage' => (bool) \Configuration::get('PS_CHECKOUT_EC_CHECKOUT_PAGE'),
Expand Down
11 changes: 11 additions & 0 deletions controllers/admin/AdminAjaxPrestashopCheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,15 @@ public function ajaxProcessToggleECProductPage()
Tools::getValue('status') ? 1 : 0
);
}

/**
* AJAX: Toggle logs for debug
*/
public function ajaxProcessToggleDebugLogs()
{
Configuration::updateValue(
'PS_CHECKOUT_DEBUG_LOGS_ENABLED',
Tools::getValue('status') ? 1 : 0
);
}
}
7 changes: 6 additions & 1 deletion ps_checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function __construct()
'AdminAjaxPrestashopCheckout',
'AdminPaypalOnboardingPrestashopCheckout',
];
$this->logger = CheckoutLogger::create();
}

/**
Expand Down Expand Up @@ -899,6 +898,12 @@ public function addCheckoutPaymentForAllActivatedCountries()
*/
public function getLogger()
{
if (null !== $this->logger) {
return $this->logger;
}

$this->logger = CheckoutLogger::create();

return $this->logger;
}
}
8 changes: 8 additions & 0 deletions tests/PaymentCreateOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public function debug($msg)
}
}

class Configuration
{
public static function get($key)
{
return $key;
}
}

class PaymentCreateOrderTest extends TestCase
{
public function testCreateOrderDefaultScenario()
Expand Down
2 changes: 2 additions & 0 deletions tests/phpstan/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ parameters:
- classes
- controllers
- ps_checkout.php
dynamicConstantNames:
- _PS_VERSION_
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '#Cannot assign offset "merchant…" to string|true.#'
Expand Down

0 comments on commit 6f48327

Please sign in to comment.