1

I am trying to print the log using logger method in magento 2, while printing i am getting 2 empty array brackets in the log. Can anybody say why it happens like this and how to remove those brackets?

sample log response Coveolog.INFO: {"message":"Source with name 'dss-coveo_m2-integration-b2c-product-en_us' already exists.","errorCode":"INVALID_SOURCE_CONFIGURATION","requestID":"47abdd9f-0ae6-4ed4-a19f-c995d4e62f2c"} [] []

1
  • Can you please share code where you are printing – Ankit Apr 8 at 9:20
0

First thing first, look at the class vendor/monolog/monolog/src/Monolog/Logger.php which is responsible with writing the log messages. This class implements all the functions to add debug, info, alert, notice, critical or error messages (as per PSR-3) and they all call the following method:

/**
 * Adds a log record at an arbitrary level.
 *
 * This method allows for compatibility with common interfaces.
 *
 * @param  mixed   $level   The log level
 * @param  string $message The log message
 * @param  array  $context The log context
 * @return bool   Whether the record has been processed
 */
public function log($level, $message, array $context = array())
{
    $level = static::toMonologLevel($level);

    return $this->addRecord($level, $message, $context);
}

Say I call the method like this:

$logger->log('ERROR','my custom error message', [['id'=>'1', 'name' => 'test'], ['var1'=>'value1']]);

This call will log in var/log/system.log the following content: [2021-04-08 11:52:56

] main.ERROR: my custom error message [{"id":"1","name":"test"},{"var1":"value1"}] []

I specified the message level ERROR, the message string AND I also printed an array with data also known as context. This is very useful when you also want to print which data was used when the error appeared, it helps a lot in the debug process.

As you can see, the array before last got populated with the JSON representation of my provided data array. You will also see this in exceptions handling where the exception can be passed as context.


As for the last array, I didn't see it in action so far, but I know it's the extra key that we see in addRecord() method from vendor/monolog/monolog/src/Monolog/Logger.php:

$record = array(
    'message' => (string) $message,
    'context' => $context,
    'level' => $level,
    'level_name' => $levelName,
    'channel' => $this->name,
    'datetime' => $ts,
    'extra' => array(),
);

How do I know this? By looking at the class vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php which is used to format the log lines, where the first thing you see is this:

const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";

Inside the format() method of this class, the record's content is processed and formatted and you can see that the 'context' and 'extra' keys are prepared to be written in the format given by the SIMPLE_FORMAT constant.

So all I can tell about the second array, is that this is the content from extra key, but I haven't worked with it, nor see it used so far. If you search for 'extra' in the vendor/monolog/monolog you will see other formatters and handlers using it, so there is a use to it, we just didn't see it in action in the logs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.