harbor/monolog-manager 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

harbor/monolog-manager

Composer 安装命令:

composer require harbor/monolog-manager

包简介

Manages multiple Monolog loggers

关键字:

README 文档

README

Monolog Manager allows you to easily create and use multiple logging channels. The manager utilizes Monolog Factory for easy, configuration-based, creation of the Loggers.

You can optionally use a PSR-11 dependency injection container for Handler, Processor, and Formatter resolution within your logger configuration.

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:

composer require harbor/monolog-manager

Usage

Adding logging channels

use Monolog\Formatter\HtmlFormatter;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\PsrLogMessageProcessor;
use Psr\Log\LogLevel;

$manager = new Harbor\MonologManager\Manager();

// Setup the default logger
$manager->add('app', [
    'default' => true,
    'handlers' => [
        'name' => StreamHandler::class,
        'params' => [
            'stream' => 'path/to/your.log',
            'level' => LogLevel::WARNING,
        ],
        'formatter' => [
            'name' => LineFormatter::class,
            'params' => [
                'allowInlineLineBreaks' => false,
                'ignoreEmptyContextAndExtra' => true,
            ],
        ],
    ],
    'processors' => [
        [
            'name' => PsrLogMessageProcessor::class,
        ]
    ],
]);

// Add an additional logging channel
$manager->add('alert', [
    'handlers' => [
        [
            'name' => NativeMailerHandler::class,
            'params' => [
                'to' => 'alerts@example.com',
                'subject' => 'Application Alert',
                'from' => 'noreply@example.com',
                'level' => Logger::ALERT,
            ],
            'formatter' => [
                'name' => HtmlFormatter::class,
            ],
        ],
    ],
    'processors' => [
        [
            'name' => PsrLogMessageProcessor::class,
        ]
    ],
]);

Logging to the default channel

The Manager implements the Psr\Log\LoggerInterface, and sends the log message to the default channel.

// These logs be sent to the `app` channel we configured above.
$manager->emergency('test emergency');
$manager->alert('test alert');
$manager->critical('test critical');
$manager->error('test error');
$manager->warning('test warning');
$manager->notice('test notice');
$manager->info('test info');
$manager->debug('test debug');
$manager->log(LogLevel::INFO, 'test log');

Additionally, you can call any method on the default channel's Mongolog\Logger instance:

$manager->getName(); // returns "app"

Logging to a specific channel

Using the channel(?string $name = null) method on Manager, you can get that channel's Mongolog\Logger instance:

/** @var \Mongolog\Logger */
$logger = $manager->channel('alert');
$logger->alert('Something important happened!');

Emergency Logger

By default, the Manager will return a default "emergency logger" if an error occurs while creating the Logger instance".

This is to prevent logging configuration/usage issues from stopping script execution.

The default emergency logger added via:

$manager->add(Manager::EMERGENCY_CHANNEL, [
    'handlers' => [
        [
            'name' => \Monolog\Handler\StreamHandler::class,
            'params' => [
                'stream' => 'php://stderr',
                'level' => \Psr\Log\LogLevel::DEBUG,
            ],
        ],
    ],
]);

Supplying your own emergency logger:

Simply add a channel, using Manager::EMERGENCY_CHANNEL as the channel name.

Disabling the emergency logger

$manager->useEmergencyChannel(false);

Using a Dependency Injection Container

To use a PSR-11 container to resolve dependencies:

Setup

use Harbor\MonologManager\Factory;
use Harbor\MonologManager\Manager;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface */
$container = getContainer();

// Create a new Factory, providing the container
$factory = new Factory($container);

// Provide your factory to the Manager
$manager = new Manager($factory);

Using the container in your channel config

The handlers, processors, and formatter are resolved using the following logic:

Note: The term callable refers to any value where is_callable($value) === true OR any class where method_exists($value, '__invoke')

  1. If value is an object of the correct type: $value = $value
  2. If value is a string, and $container->has($value): $value = $container->get($value)
  3. If value is a string, and is callable: $value = $value($container)
  4. If value is an array, and isset($value['formatter']): $value['formatter'] = $this->resolve($value['formatter'])

If the value cannot be resolved, an InvalidArgumentException is thrown (which gets sent to the emergency logger by default, see above)

Example of all possible values:

$manager->add('useless-logger', [
    'handlers' => [
        HandlerFactory::class,
        NoopHandler::class,
        new NoopHandler(),
        static fn () => new NoopHandler(),
        [
            'name' => ErrorLogHandler::class,
            'formatter' => LineFormatter::class,
        ],
        [
            'name' => ErrorLogHandler::class,
            'formatter' => new LineFormatter(),
        ],
    ],
    'processors' => [
        PsrLogMessageProcessor::class,
        new MemoryUsageProcessor(),
    ],
]);

License

Released under MIT License

harbor/monolog-manager 适用场景与选型建议

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

它主要适用于以下技术方向: 「monolog」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-17