eonx-com/easy-logging 问题修复 & 功能扩展

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

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

eonx-com/easy-logging

Composer 安装命令:

composer require eonx-com/easy-logging

包简介

Create and configure Monolog Loggers easily

README 文档

README

---eonx_docs--- title: Introduction weight: 0 ---eonx_docs---

This package allows you to create and configure Monolog Loggers in centralised and reusable way:

  • Configure channels using PHP
  • Control Handlers and Processors order
  • Integration with popular frameworks (e.g. Laravel, Symfony)
  • Discover Handlers and Processors automatically in your application

Require package (Composer)

The recommended way to install this package is to use Composer:

$ composer require eonx-com/easy-logging

Usage

Here is a simple example on how to use the LoggerFactoryInterface to create loggers:

// Instantiate the logger factory manually or use DI ...

$default = $loggerFactory->create(); // Calling create without arguments will create logger for default channel

$console = $loggerFactory->create('console'); // Create logger for console channel specifically

Usage in Framework

The different integrations provided by this package will by default register the logger for your default channel in the service container under the following service ids:

  • Psr\Log\LoggerInterface
  • logger

You can then use dependency injection anywhere you like!

Thanks to Autowiring via setters in Symfony, you can use \EonX\EasyLogging\Logger\LoggerAwareTrait to simplify the injection of Psr\Log\LoggerInterface.


Logger Configuration

The LoggerFactoryInterface allows you to set different collections of "config providers", each config can define:

  • channels: if defined the config will be applied only to given channels, if null the config will be applied to all channels
  • priority: define the order each config must be set on the logger instance, higher the priority later the config will be added to the logger instance

HandlerConfig

The HandlerConfigInterface allows you to configure \Monolog\Handler\HandlerInterface to be set loggers created by the factory. Like other configs, it allows you to specify a list of channels this handler is for and, also a priority to control when the handler must be executed.

To tell the logger factory about your HandlerConfigInterface, you must use a HandlerConfigProviderInterface. The logger factory accepts a collection of providers via the setHandlerConfigProviders() method:

use EonX\EasyLogging\Factory\LoggerFactory;

$handlerConfigProviders = [];

// Add your own handler config providers to $handlerConfigProviders ...

$loggerFactory = new LoggerFactory();

// Set your handler config providers on the logger factory
$loggerFactory->setHandlerConfigProviders($handlerConfigProviders);

Here is a simple example of a HandlerConfigProviderInterface to register a StreamHandler:

namespace App\Logger;

use EonX\EasyLogging\Config\HandlerConfig;
use EonX\EasyLogging\Provider\HandlerConfigProviderInterface;
use Monolog\Handler\StreamHandler;

final class StreamHandlerConfigProvider implements HandlerConfigProviderInterface
{
    /**
     * @return iterable<\EonX\EasyLogging\Config\HandlerConfigInterface>
     */
    public function handlers(): iterable
    {
        /**
         * This method returns an iterable to make it easier to handle complex handler configs definition
         * But you can simply return an array if you want.
         */

        yield new HandlerConfig(new StreamHandler('php://stdout'));
    }
}

ProcessorConfig

The ProcessorConfigInterface allows you to configure \Monolog\Processor\ProcessorInterface to be set loggers created by the factory. Like other configs, it allows you to specify a list of channels this handler is for and, also a priority to control when the handler must be executed.

To tell the logger factory about your ProcessorConfigInterface, you must use a ProcessorConfigProviderInterface. The logger factory accepts a collection of providers via the setProcessorConfigProviders() method:

use EonX\EasyLogging\Factory\LoggerFactory;

$processorConfigProviders = [];

// Add your own processor config providers to $handlerConfigProviders ...

$loggerFactory = new LoggerFactory();

// Set your processor config providers on the logger factory
$loggerFactory->setProcessorConfigProviders($processorConfigProviders);

Here is a simple example of a ProcessorConfigProviderInterface to register a TagProcessor:

namespace App\Logger;

use EonX\EasyLogging\Config\ProcessorConfig;
use EonX\EasyLogging\Provider\ProcessorConfigProviderInterface;
use Monolog\Processor\TagProcessor;

final class TagProcessorConfigProvider implements ProcessorConfigProviderInterface
{
    /**
     * @return iterable<\EonX\EasyLogging\Config\ProcessorConfigInterface>
     */
    public function processors(): iterable
    {
        /**
         * This method returns an iterable to make it easier to handle complex processor configs definition
         * But you can simply return an array if you want.
         */

        yield new ProcessorConfig(new TagProcessor(['tag-1', 'tag-2']));
    }
}

LoggerConfigurator

The \Monolog\Logger class has methods allowing you to configure it even more (e.g. using microseconds). To deal with that, the logger factory accepts a collection of LoggerConfiguratorInterface.

To tell the logger factory about your LoggerConfiguratorInterface, you must call the setLoggerConfigurators() method:

use EonX\EasyLogging\Factory\LoggerFactory;

$loggerConfigurators = [];

// Add your own logger configurators to $loggerConfigurators ...

$loggerFactory = new LoggerFactory();

// Set your logger configurators on the logger factory
$loggerFactory->setLoggerConfigurators($loggerConfigurators);

Here is a simple example of a LoggerConfiguratorInterface to use microseconds:

namespace App\Logger;

use EonX\EasyLogging\Config\AbstractLoggingConfig;
use EonX\EasyLogging\Configurator\LoggerConfiguratorInterface;
use Monolog\Logger;

final class UseMicrosecondsLoggerConfigurator extends AbstractLoggingConfig implements LoggerConfiguratorInterface
{
    public function configure(Logger $logger) : void
    {
        $logger->useMicrosecondTimestamps(true);
    }
}

eonx-com/easy-logging 适用场景与选型建议

eonx-com/easy-logging 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.14M 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 eonx-com/easy-logging 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.14M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 20
  • 依赖项目数: 4
  • 推荐数: 6

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-26