It’s a recognized greatest follow to maintain logs for crucial processes in order that at any time when a failure happens, we are able to verify logs and discover the explanation of the failure. PrestaShop has its personal FileLogger system that we are able to reuse to generate logs in module. Right here we’ll focus on learn how to generate module logs with varied severity ranges and creating report by parsing the log file.
Generate logs with PrestaShop FileLogger
Let’s dive in immediately on learn how to use FileLogger in module, then we’ll focus on every level afterwards.
// Create an object of PrestaShop FileLogger $logger = new FileLogger(); // Present file path to write down logs $filePath = _PS_MODULE_DIR_.'myexamplemodule/log/course of.log'; $logger->setFilename($filePath); $logger->logDebug('Beginning instance logger course of.'); attempt { $warning = false; /** * Your course of code right here, set warning if any */ if ($warning) { // Log warning message to file $logger->logWarning($warning); } else { // Log a hit message to file $logger->logInfo('The method was accomplished with out error.'); } } catch (Exception $e) { // Log error message to file $logger->logError('Exception: '.$e->getMessage()); }
Lets perceive above code line by line:
- Create a FileLogger object: The FileLogger class is already loaded in PrestaShop atmosphere, so you may immediately use it with out together with its file or class in module.
- Present a file path for logs: We have to present a file path during which FileLogger writes the logs. FileLogger will create the file if not exists. Nonetheless, the folder should exist with writable permissions.
- Creating logs: We are able to generate 4 varieties (severity) of log messages : DEBUG, WARNING, INFO and ERROR.
- logDebug: This technique writes DEBUG kind log. This sort of logs are simply data in between the method. Instance:
- “*DEBUG* 2022/09/22 – 12:10:49: Beginning instance logger course of.”
- logWarning: This writes the given warning message to log file, instance:
- “*WARNING* 2022/09/22 – 12:10:49: This can be a warning message”
- logInfo: This technique writes informative (like a hit data) messages to log file. Instance:
- “*INFO* 2022/09/22 – 12:10:49: The method was accomplished with out error.”
- logError: That is used to write down error degree logs to file. Instance:
- “*ERROR* 2022/09/22 – 12:10:49: Exception: that is an exception message”
- logDebug: This technique writes DEBUG kind log. This sort of logs are simply data in between the method. Instance:
The log file seems like this after writing logs:

Parse log file to generate report:
Since FileLogger makes use of a constant format for every log line, due to this fact we are able to parse the log file and generate a log report in type of a array additionally. The best means to do that is to separate every line and extract needed data. For instance:
$filePath = _PS_MODULE_DIR_.'myexamplemodule/log/course of.log'; $logDetail = file($filePath); if ($logDetail) { $logArray = array(); foreach ($logDetail as $knowledge) { $logParts = explode(": ", $knowledge); $logDateTime = explode("* ", $logParts[0]); $message = $logParts[1]; $logArray[] = array( 'standing' => Instruments::substr($logDateTime[0], 1),// INFO, ERROR and so forth 'date' => trim($logDateTime[1]), 'msg' => trim($message), ); } return $logArray; }
The above code will give us the log particulars in following format:

That’s all about learn how to use PrestaShop FileLogger to generate logs. If any challenge or doubt within the above course of, please be happy to tell us within the remark part.
I’d be completely satisfied to assist.
Additionally, you may discover our PrestaShop Improvement Companies and a wide variety of high quality PrestaShop Modules.
For any doubt contact us at [email protected].