monolog/logger error magento
-
30-09-2020 - |
Question
can anybody help on this :
(Psr\Log\InvalidArgumentException): Level "" is not defined, use one of: 100, 200, 250, 300, 400, 500, 550, 600
vendor\monolog\monolog\src\Monolog\Logger.php(256): Monolog\Logger::getLevelName(NULL) lib\internal\Magento\Framework\Logger\Monolog.php(24): Monolog\Logger->addRecord(NULL, 'Tagalys Request...', Array)
This is how i have used logger
protected $logger;
public function __construct(
\Psr\Log\LoggerInterface $logger,
) {
$this->logger = $logger;
}
public function xyz(){
$this->logger->log(null, 'Tagalys Request Object: '.json_encode($payload));
}
I am not getting any log files
Solution
When you use the \Psr\Log\LoggerInterface
the first parameter must be a level of log.
Different levels can be found under Psr\Log\LogLevel
:
class LogLevel
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
}
So if you want to log for example a debug information you'll have to call:
$this->logger->log(\Psr\Log\LogLevel::DEBUG, 'Tagalys Request Object: '.json_encode($payload));
To avoid having to specify the level of log, the class provides one method per level so instead of the code above, you can also do:
$this->logger->debug('Tagalys Request Object: '.json_encode($payload));
The other methods available are:
emergency()
alert()
critical()
error()
warning()
notice()
info()
Using this class you cannot specify a custom log file.
If you want to do so you can use the following code:
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylogfile.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');