You do not need to customize or try to extend Magento2's logging. As you said it's using Monolog with only slight customization. It is sufficient to write your own logger extending Monolog with very little effort.
Assuming your module is in YourNamespace/YourModule
:
1) Write Logger class in Logger/Logger.php
:
<?php
namespace YourNamespace\YourModule\Logger;
class Logger extends \Monolog\Logger
{
}
2) Write Handler class in Logger/Handler.php
:
<?php
namespace YourNamespace\YourModule\Logger;
use Monolog\Logger;
class Handler extends \Magento\Framework\Logger\Handler\Base
{
/**
* Logging level
* @var int
*/
protected $loggerType = Logger::INFO;
/**
* File name
* @var string
*/
protected $fileName = '/var/log/myfilename.log';
}
Note: This is the only step that uses the Magento code. \Magento\Framework\Logger\Handler\Base
extends Monolog's StreamHandler
and e.g. prepends the $fileName attribute with the Magento base path.
3) Register Logger in Dependency Injection etc/di.xml
:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="YourNamespace\YourModule\Logger\Handler">
<arguments>
<argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
</arguments>
</type>
<type name="YourNamespace\YourModule\Logger\Logger">
<arguments>
<argument name="name" xsi:type="string">myLoggerName</argument>
<argument name="handlers" xsi:type="array">
<item name="system" xsi:type="object">YourNamespace\YourModule\Logger\Handler</item>
</argument>
</arguments>
</type>
</config>
Note: This is not strictly required but allows the DI to pass specific arguments to the constructor. If you do not do this step, then you need to adjust the constructor to set the handler.
4) Use the logger in your Magento classes:
This is done by Dependency Injection. Below you will find a dummy class which only writes a log entry:
<?php
namespace YourNamespace\YourModule\Model;
class MyModel
{
/**
* Logging instance
* @var \YourNamespace\YourModule\Logger\Logger
*/
protected $_logger;
/**
* Constructor
* @param \YourNamespace\YourModule\Logger\Logger $logger
*/
public function __construct(
\YourNamespace\YourModule\Logger\Logger $logger
) {
$this->_logger = $logger;
}
public function doSomething()
{
$this->_logger->info('I did something');
}
}
Yes I faced the problem before, the only way I managed to get it working was by forcing the log by setting the 4th parameter of Mage::log
to true so in your case I suggest you use the following code
Mage::log('Something went wrong.', null, 'modulename_setup.log', true);
Best Answer
In case you want to define your own logger using monolog you can follow https://magento.stackexchange.com/a/75954/9169.
If you want to change default debug log file you can rewrite
Magento\Framework\Logger\Handler\Debug
in your custom module and change filename there.Module's
di.xml
codeCustomLogger.php
codeNow your debug logs will be logged in
customfile.log
.Example:
Similarly you can overwrite other default log files
exception.log
andsystem.log
using preference. Other log files are defined in\Magento\Framework\Logger\Handler\Exception
,\Magento\Framework\Logger\Handler\System