Select Page

Save logs using PrestaShopLogger

Gajendra Singh
Published: October 7, 2022

In this blog, we are going to learn how to save logs in the PrestaShop log table.

Let’s see together how to do that with PrestaShop.

Sometimes, we need to store logs for critical processes such that failure occurs during the process execution.

PrestaShop has its own PrestaShopLogger class and its function addLog

You can call addLog function from anywhere in your file.

Searching for an experienced
Prestashop Company ?
Read More


Function Call:

PrestaShopLogger::addLog(
    $message, 
    $severity, 
    $errorCode, 
    $objectType, 
    $objectId, 
    $allowDuplicate, 
    $idEmployee
)

Let’s understand the above function parameters line by line:

$message: It is used for the log message that you want to save during your failure.

$severity: Its default value is 1 (Informative only) and is used for 4 types of severity.

1 for Informative only.

2 for Warning.

3 for Error.

4 for Major issues (crash).

$errorCode: Its default value is NULL and used for the error code.

You can pass the error code that you are getting from any third-party API

$objectType: Its default value is NULL and used for your class object on which you are getting errors/failures like Product, Cart, Customer, etc.

$objectId: Its default value is NULL and is used for your object id on which you are getting an error/failure.

$allowDuplicate: Its default value is false and is used if you want to store the same log again in the log table.

$idEmployee: Its default value is NULL and is used to save the logged-in employee ID.

Now we will write two logs using this function.

Product Object log:

PrestaShopLogger::addLog(
    'Test product log', // message
    1, // severity
    null, // error code
    'Product', // object type
    3, // id_product
    false, // allow duplicate
    1 // employee ID
);

Cart Object log:

PrestaShopLogger::addLog(
    'Test cart log', // message
    2, // severity
    null, // error code
    'Cart', // object type
    5, // id_product
    false, // allow duplicate
    1 // employee ID
);

You can see your added logs in PrestaShop Back-office:

Logs-•-PS1787

Also, this function checks your defined minimum severity level to send a log email on your defined email id.

If you define the minimum severity level as 4 (Major issue) then you will get emails for all 4 types of severity (Informative only, Warning, Error, and Major issue)

Logs_email

That’s all about this blog. Hope it will help you.

If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.

We would be happy to help.

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

For any doubt contact us at [email protected].

Source: webkul.com