定制 mm-plazz/mezzio-monolog 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mm-plazz/mezzio-monolog

Composer 安装命令:

composer require mm-plazz/mezzio-monolog

包简介

Mezzio Monolog ErrorHandler

README 文档

README

Latest Stable Version Total Downloads Monthly Downloads Software License Build Status

This library enables Monolog as an ErrorHandler in Mezzio.

It catches all uncaught exceptions and logs them to the configured Monolog handlers.

What is Mezzio?

Mezzio is a PHP framework for building web applications and APIs. It is based on a middleware pipeline architecture, which makes it flexible and extensible.

What is Monolog?

Monolog is a popular logging library for PHP. It supports a wide variety of handlers, which allows you to send your logs to files, sockets, inboxes, databases and various web services.

Install

Install the library using composer:

composer require mm-plazz/mezzio-monolog

Configuration

First, you need to enable the component by adding the Plazz\Mezzio\Monolog\ConfigProvider to your config/config.php:

<?php

use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\ConfigAggregator\ArrayProvider;
use Laminas\ConfigAggregator\PhpFileProvider;

$cacheConfig = [
    'config_cache_path' => 'data/cache/config-cache.php',
];

$aggregator = new ConfigAggregator([
    \Plazz\Mezzio\Monolog\ConfigProvider::class, // <-- Add this line

    // ... other providers

], $cacheConfig['config_cache_path']);

return $aggregator->getMergedConfig();

Next, create a config/autoload/monolog.global.php file to configure the Monolog handlers:

<?php

declare(strict_types=1);

use Monolog\Level;

return [
    'monolog' => [
        // StreamHandler configuration
        'stream' => [
            'path' => 'data/log/myapp.log',
            'level' => Level::Debug,
        ],
        // SentryHandler configuration
        'sentry' => [
            'dsn' => 'https://xxxxx@sentry.io/12345',
            'level' => Level::Debug,
        ],
    ],
];

The Monolog ErrorHandler will be active only when Mezzio is in "production mode" ($config['debug'] is false). To switch to "production mode", you can use composer run development-disable. To switch back to "development mode", use composer run development-enable.

Handlers

This library supports the following Monolog handlers out of the box:

  • StreamHandler: Logs records into any PHP stream, which is ideal for log files.

    • path (string): The path to the log file. Defaults to data/log/app.log.
    • level (Monolog\Level): The minimum logging level at which this handler will be triggered. Defaults to Level::Debug.
  • SentryHandler: Logs records to Sentry.io. This handler requires the sentry/sdk package.

    • dsn (string): Your Sentry DSN.
    • level (Monolog\Level): The minimum logging level at which this handler will be triggered. Defaults to Level::Debug.
    • Any other options will be passed to the Sentry init() function.

Usage

This library automatically registers the Monolog listener with Mezzio's ErrorHandler. No further configuration is needed to make it work.

If you want to use the Monolog logger in your own classes, you can fetch the Plazz\Mezzio\Monolog\Listener\Listener service from the container. This class provides all the logging methods from the PSR-3 logger interface.

Here is an example of how to use it in a factory:

<?php

namespace App\Factory;

use App\MyService;
use Psr\Container\ContainerInterface;
use Plazz\Mezzio\Monolog\Listener\Listener as MonologListener;

class MyServiceFactory
{
    public function __invoke(ContainerInterface $container): MyService
    {
        $monologListener = $container->get(MonologListener::class);
        return new MyService($monologListener);
    }
}

And in your service:

<?php

namespace App;

use Plazz\Mezzio\Monolog\Listener\Listener as MonologListener;

class MyService
{
    private MonologListener $logger;

    public function __construct(MonologListener $logger)
    {
        $this->logger = $logger;
    }

    public function doSomething(): void
    {
        $this->logger->info('Doing something...');
        // ...
    }
}

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

mm-plazz/mezzio-monolog 适用场景与选型建议

mm-plazz/mezzio-monolog 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 88 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mm-plazz/mezzio-monolog 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mm-plazz/mezzio-monolog 我们能提供哪些服务?
定制开发 / 二次开发

基于 mm-plazz/mezzio-monolog 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 88
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2025-08-05