Select Page

PrestaShop FileLogger for writing module logs

Ram Chandra
Published: September 22, 2022

It is a known best practice to keep logs for critical processes so that whenever a failure occurs, we can check logs and find the reason of the failure. PrestaShop has its own FileLogger system that we can reuse to generate logs in module. Here we will discuss how to generate module logs with various severity levels and creating report by parsing the log file.

Generate logs with PrestaShop FileLogger

Let’s dive in directly on how to use FileLogger in module, then we will discuss each point afterwards.

// Create an object of PrestaShop FileLogger
$logger = new FileLogger();
// Provide file path to write logs
$filePath = _PS_MODULE_DIR_.'myexamplemodule/log/process.log';
$logger->logDebug('Starting example logger process.');
try {
    $warning = false;
     * Your process code here, set warning if any
    if ($warning) {
        // Log warning message to file
    } else {
        // Log a success message to file
        $logger->logInfo('The process was completed without error.');
} catch (Exception $e) {
    // Log error message to file
    $logger->logError('Exception: '.$e->getMessage());

Lets understand above code line by line:

  • Create a FileLogger object: The FileLogger class is already loaded in PrestaShop environment, so you can directly use it without including its file or class in module.
  • Provide a file path for logs: We need to provide a file path in which FileLogger writes the logs. FileLogger will create the file if not exists. However, the folder must exist with writable permissions.
  • Creating logs: We can generate 4 types (severity) of log messages : DEBUG, WARNING, INFO and ERROR.
    • logDebug: This method writes DEBUG type log. This type of logs are just info in between the process. Example:
      • “*DEBUG* 2022/09/22 – 12:10:49: Starting example logger process.”
    • logWarning: This writes the given warning message to log file, example:
      • “*WARNING* 2022/09/22 – 12:10:49: This is a warning message”
    • logInfo: This method writes informative (like a success info) messages to log file. Example:
      • “*INFO* 2022/09/22 – 12:10:49: The process was completed without error.”
    • logError: This is used to write error level logs to file. Example:
      • “*ERROR* 2022/09/22 – 12:10:49: Exception: this is an exception message”

The log file looks like this after writing logs:


Parse log file to generate report:

Since FileLogger uses a consistent format for each log line, therefore we can parse the log file and generate a log report in form of a array also. The simplest way to do this is to split each line and extract necessary information. For example:

Searching for an experienced
Prestashop Company ?
Read More

$filePath = _PS_MODULE_DIR_.'myexamplemodule/log/process.log';
$logDetail = file($filePath);
if ($logDetail) {
    $logArray = array();
    foreach ($logDetail as $data) {
        $logParts = explode(": ", $data);
        $logDateTime = explode("* ", $logParts[0]);
        $message = $logParts[1];
        $logArray[] = array(
            'status' => Tools::substr($logDateTime[0], 1),// INFO, ERROR etc
            'date' => trim($logDateTime[1]),
            'msg' => trim($message),
    return $logArray;

The above code will give us the log details in following format:

PrestaShop FileLogger report

That’s all about how to use PrestaShop FileLogger to generate logs. If any issue or doubt in the above process, please feel free to let us know in the comment section.

I would be happy to help.

Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

For any doubt contact us at [email protected].