分类
服务器

Magento2 自定义日志

/etc/di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magefan (support@magefan.com). All rights reserved.
 * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
 *
 * Glory to Ukraine! Glory to the heroes!
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <!--自定义日志写入-->
    <type name="Maijindou\Sales\Logger\Handler">
        <arguments>
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        </arguments>
    </type>
    <type name="Maijindou\Sales\Logger\Logger">
        <arguments>
            <argument name="name" xsi:type="string">Maijindou Log</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">Maijindou\Sales\Logger\Handler</item>
            </argument>
        </arguments>
    </type>
</config>
Logger/Handler
<?php
namespace Maijindou\Sales\Logger;

use Magento\Framework\Logger\Handler\Base;
use Monolog\Logger;

class Handler extends Base
{
    /**
     * Logging level
     * @var int
     */
    protected $loggerType = Logger::INFO;

    /**
     * File name
     * @var string
     */
    protected $fileName = '/var/log/Maijindou.log';
}
Logger/Logger
<?php
namespace Maijindou\Sales\Logger;

class Logger extends \Monolog\Logger
{
}
# 调用方法
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$Logger = $objectManager->get('Maijindou\Sales\Logger\Logger');
$Logger->info('写入日志,日志在/var/log/Maijindou.log 里面');

demo 下载