1

I want all my log files to be send to a certain server by using this snippet in a custom module

use Monolog\Logger;
use Monolog\Handler\SyslogUdpHandler;
$log = new Logger('Test');
$log->pushHandler(new SyslogUdpHandler("MyLogServer", 5000));
$log->addError("ERRORRRRRR::");

Is there a common place where I can register this new logger, so that every new log is processed by this handler and not written into files?

Thanks

1 Answer 1

2

Try to set preference for LoggerInterface in VendorName/ModuleName/etc/di.xml. This will make your logger default systemwide:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Psr\Log\LoggerInterface" type="VendorName\ModuleName\Model\CustomLogger" />
</config>

Note that CustomLogger must implement LoggerInterface in this case.

6
  • but is this stil valid when I rely on Monolog?
    – xhallix
    Dec 14, 2015 at 14:57
  • It is up to you what will be the implementation. It should be ok to extend Monolog\Logger like Magento does \Magento\Framework\Logger\Monolog. Dec 14, 2015 at 15:06
  • I just made a preference and extended \Magento\Framework\Logger\Monolog and used my own handlers in the constructor, and its working fine:)
    – xhallix
    Dec 20, 2015 at 7:43
  • @xhalli : I am also trying to extend \Magento\Framework\Logger\Monolog by using preference. But I am getting error InvalidArgumentException: Cache frontend 'default' is not recognized. Please help me out. Jul 14, 2017 at 10:50
  • @AbhimanyuSingh it would be best to open a new question with more detailed information where you display what you've done so far, to help you as best as possible
    – xhallix
    Jul 14, 2017 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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