定制 ostrolucky/semantic-monolog-handler 二次开发

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

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

ostrolucky/semantic-monolog-handler

最新稳定版本:0.1.1

Composer 安装命令:

composer require ostrolucky/semantic-monolog-handler

包简介

Opinionated Monolog FingersCrossed handler which bypasses fingers-crossed behaviour for some primary channels

README 文档

README

Latest Version on Packagist Software License Build Status

This is a FingersCrossed handler that fills a missing gap in Monolog configuration:

It allows to have "primary" channels. If log is being written to one of these "primary" channels, it skips fingers crossed behaviour. This is combined with support for a Log Level. So you can still apply fingers crossed behaviour to message from primary channel, if it has low severity.

Best real world scenario of this is to be able to rely on >= INFO "app" logs always being written, no matter if there was an error or not (and if there was an error, ensure these log entries are not written twice).

This is something that's impossible to configure without writing custom handler, unless you are fine with duplicate log entries.

Install

Via Composer

composer require ostrolucky/semantic-monolog-handler

Example configuration

Handler configured following way will write all the logs to standard error output if any of the logs reach "ERROR" level, which is a standard FingersCrossed behaviour.

However, on top of it, it will also immediately print logs from "app" channel, unless they are under "DEBUG" level - without triggering FingersCrossed handler (which would flush all the logs).

use Monolog\Level;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\StreamHandler;
use Ostrolucky\SemanticMonologHandler;

$logger = new SemanticLogHandler(
    innerHandler: new StreamHandler(STDERR),
    primaryChannels: ['app' => Level::Info],
    activationStrategy: new ErrorLevelActivationStrategy(Level::Error), 
);

Example Symfony Monolog configuration

Above PHP configuration can be reflected in Symfony like so:

monolog.yaml:

monolog:
  handlers:
    main:
      type: service
      id: Ostrolucky\SemanticMonologHandler\SemanticLogHandler

services:
  Ostrolucky\SemanticMonologHandler\SemanticLogHandler:
    autoconfigure: true
    arguments:
      - !service
          class: Monolog\Handler\StreamHandler
          arguments:
            - !php/const STDERR
      - app: !php/enum Psr\Log\LogLevel\Level::Error
      - !service
          class: Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy
          arguments: [!php/enum Psr\Log\LogLevel\Level::Error]

Licensing

MIT license. Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-25