I am having trouble logging a "TypeError" with log4php. I suspect this is because I recently upgraded from php 5.5 to 7.1.
Usually, my syntax looks like this:
<?
use Logger;
class MyClass
{
/** @var Logger */
private $logger;
function __construct(array $configParams)
{
Logger::configure('logger.xml');
$this->logger = Logger::getLogger(__CLASS__);
}
public function dostuff()
{
try
{
// ...
}
catch (Exception $ex)
{
$this->logger->error("ERROR CAUGHT", $ex);
}
}
}
?>
The above syntax will print lots of info to the log file, including a stack trace. However after reading the latest php7 docs, I believe I'm meant to handle the \Throwable interface, in order to catch both errors and exceptions (which is great). So I replace the above catch with the following:
catch (\Throwable $ex)
This still prints stack-trace information for my exceptions, but when a "TypeError" is caught, nothing gets printed to the log file.
I assume that this is due to log4php not knowing how to log errors. How can I log errors using log4php in a universal way?
Thanks,
I think you should use
$ex->getMessage()for error() method your code should beFor logging trace you should use trace