nyluje
nyluje

Reputation: 3783

Prestashop LOG: get DUMP of Object and Array

I am figuring out how Prestashop 1.7 works and I have some experience with Symfony.

In Symfony development mode [Symfony project url]/_profiler is useful, among other things, to check the dump($someVariable) of variables in a request.

With Prestashop 1.7 in the admin mode it is possible to do [Prestashop project url]/admin[some random chain of chars]/_profiler to display the Symfony _profiler and analyse what's going on in the requests concerning the admin mode.

But if outside the admin mode (in the virtual shop demo mode), [Prestashop project url]/_profiler or [Prestashop project url]/[language value]/_profiler does not display the Symfony _profiler.

I have tried Prestashop own profiler by activating define('_PS_DEBUG_PROFILING_', true); in [prestashop project]/config/defines.inc.php. It displays Prestashop profiler at the bottom of the "virtual shop demo mode" but this one does not include dump($someVariable) that could be used, for development and to understand Prestashop behaviour, in a hookAction[action name].

I've managed to get the Symfony dump($someVariable) with hookDisplay[display name] through the HTML generated but not in a hookAction[action name] which is what I am looking for.

UPDATE Looking at Prestashop 1.7 code I almost have the feeling that Symfony is only used on the Admin side, because I can see: $kernel = new AppKernel(_PS_MODE_DEV_?'dev':'prod', _PS_MODE_DEV_); in [Prestashop project url]/admin[some random chain of chars]/index.php but I don't see it in [Prestashop project url]/index.php.

Upvotes: 1

Views: 2106

Answers (1)

nyluje
nyluje

Reputation: 3783

Best solution I've found so far is to create a custom logger similar to the one explained here

I've created file: [Prestashop project]/modules/[my module]/classes/CustomLogger.php

<?php
class CustomLogger {
    const DEFAULT_LOG_FILE ="prestashop_system.log";
    public static function log($message, $level = 'debug', $fileName = null){
        $fileDir = _PS_ROOT_DIR_ . '/log/';
        $fileName=self::DEFAULT_LOG_FILE;
        if(is_array($message) || is_object($message)){$message = print_r($message, true);}
        $formatted_message=$level." -- ".date('Y/m/d - H:i:s').": ".$message."\r\n";
        return file_put_contents($fileDir . $fileName, $formatted_message, FILE_APPEND);
    }
}
?>

In '[Prestashop project]/modules/[my module]/[my module].php' it is declared at the top: include_once dirname(__FILE__).'/classes/CustomLogger.php';

And use CustomLogger::log($[some variable]); in your code.

Upvotes: 3

Related Questions