Skip to content

Instantly share code, notes, and snippets.

@jlnarvaez
Created January 31, 2022 07:56
Show Gist options
  • Save jlnarvaez/b34c3f25c5eae2f291318caaeb9356fb to your computer and use it in GitHub Desktop.
Save jlnarvaez/b34c3f25c5eae2f291318caaeb9356fb to your computer and use it in GitHub Desktop.
Create custom Logger file in Magento 2
<!-- Location: app/code/JLNarvaez/CustomLog/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="JLNarvaez\CustomLog\Logger\Handler">
<arguments>
<argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
</arguments>
</type>
<type name="JLNarvaez\CustomLog\Logger\Logger">
<arguments>
<!-- Nombre que tendrá el Logger (aparecerá en el fichero .log cuando se escriba algun log) -->
<argument name="name" xsi:type="string">myCustomLog</argument>
<argument name="handlers" xsi:type="array">
<!-- Namespace de nuestro Handler -->
<item name="system" xsi:type="object">JLNarvaez\CustomLog\Logger\Handler</item>
</argument>
</arguments>
</type>
<!-- ... -->
<!-- ... -->
<!-- ... -->
</config>
// Location: app/code/JLNarvaez/CustomLog/Logger/Handler.php
<?php
namespace JLNarvaez\CustomLog\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/jlnarvaez_custom.log';
}
// Location: app/code/JLNarvaez/CustomLog/Logger/Logger.php
<?php
namespace JLNarvaez\CustomLog\Logger;
class Logger extends \Monolog\Logger
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment