enlitepro/enlite-monolog

Laminas 与 Monolog 集成

3.0.0 2021-07-13 01:16 UTC

README

通过具有出色日志系统的 monolog 与 Laminas 集成

安装

推荐通过命令行使用 composer 安装。

composer require enlitepro/enlite-monolog

用法

  1. EnliteMonolog 添加到您的 config/application.config.php 中以启用模块。
// usage over service locator
$serviceLocator->get('EnliteMonologService')->debug('hello world');

use EnliteMonolog\Service\MonologServiceAwareInterface,
    EnliteMonolog\Service\MonologServiceAwareTrait;

// usage in your services
class MyService implements MonologServiceAwareInterface
{
    use MonologServiceAwareTrait;

    public function whatever()
    {
        $this->getMonologService()->debug('hello world');
    }
}
  1. 将模块中的配置文件 config/monolog.global.php.dist 复制到您的项目的 config/autoload。

默认情况下,它将日志写入 data/logs/application.log。如果想要更改此行为,请按照以下配置添加您的设置

    'EnliteMonolog' => array(
        'EnliteMonologService' => array(
            // Logger name
            // 'name' => 'EnliteMonolog',

            // Handlers, it can be service locator alias(string) or config(array)
            'handlers' => array(
                // by config
                'default' => array(
                    'name' => 'Monolog\Handler\StreamHandler',
                    'args' => array(
                        'stream' => 'data/log/application.log',
                        'level' => \Monolog\Logger::DEBUG,
                        'bubble' => true
                    ),
                    'formatter' => array(
                        'name' => 'Monolog\Formatter\LogstashFormatter',
                        'args' => array(
                            'applicationName' => 'My Application',
                        ),
                    ),
                ),

                // by service locator
                'MyMonologHandler'
            )
        ),

        // you can specify another logger
        // for example ChromePHPHandler

        'MyChromeLogger' => array(
            'name' => 'MyName',
            'handlers' => array(
                array(
                    'name' => 'Monolog\Handler\ChromePHPHandler',
                )
            )
        )
    ),

现在您可以开始使用了

$serviceLocator->get('EnliteMonologService')->debug('hello world');
$serviceLocator->get('MyChromeLogger')->debug('hello world');

贡献

此库旨在遵守 PSR-1PSR-2PSR-4。如果您发现遵守上的疏忽,请通过 pull request 发送补丁。