定制 yapro/debug 二次开发

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

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

yapro/debug

Composer 安装命令:

composer require yapro/debug

包简介

Very useful extensions for Monolog

README 文档

README

The really useful Monolog`s extensions.

lib tests

Installation

Add as a requirement in your composer.json file or run

composer require yapro/monolog-ext dev-master

Configuration of Symfony >= 2.x

You can use the best way to handle your logs because it's the easiest way:

monolog:
    channels:
        - deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
    handlers:
        main:
            type: service
            id: YaPro\MonologExt\Handler\WiseHandler

and don't forget to register the handler as a service:

services:
  YaPro\MonologExt\Handler\WiseHandler: ~

You will get the features:

  • writing logs to stderr ( https://12factor.net/logs )
  • json representation
  • ignoring logs when the client sends an invalid http request (4xx)
  • log records of all levels in the application code (src dir, not in vendor dir)
  • log records of the NOTICE level and higher in libraries (vendor dir)
  • smart a record reducing (removing keys from the record context when the record size exceeds 8192 bytes)

and other features such as development mode.

You can also configure WiseHandler:

env var name default value example description
EH_DEV_MODE_PHP_FPM 0 1 print out important debugging information and stop the execution (comfortable development in php-fpm)
EH_DEV_MODE_PHP_CLI 0 1 print out important debugging information and stop the execution (comfortable development in php-cli)
EH_MAX_DUMP_LEVEL 5 3 the nesting level of the objects to be serialized (3 is less detailed level than 5)
EH_IGNORE_RECORD_LEVEL_BELOW 0 250 errors with a level below the specified one will be ignored (250 is less then NOTICE, 0 == all records)
EH_STOP_WHEN_RECORD_LEVEL_ABOVE 500 400 an error with a level higher than the specified one will stop the http request with the 500 status (Logger::CRITICAL)

You can also use the collection of monolog processors, that gives you the opportunity to handle and log different errors.

Add needed for you app to your config.yml

services:
  # Adds exception`s information to a log record
  YaPro\MonologExt\Processor\AddInformationAboutExceptionProcessor:
    class: YaPro\MonologExt\Processor\AddInformationAboutExceptionProcessor
    tags:
      - { name: monolog.processor, handler: main }

  # Adds a call stack of the log-record location
  YaPro\MonologExt\Processor\AddStackTraceOfCallPlaceProcessor:
    class: YaPro\MonologExt\Processor\AddStackTraceOfCallPlaceProcessor
    tags:
      - { name: monolog.processor, handler: main }

  # Stop execution when problems occur (very useful in tests)
  YaPro\MonologExt\Processor\StopExecutionWhenProblemProcessor:
    class: YaPro\MonologExt\Processor\StopExecutionWhenProblemProcessor
    tags:
      - { name: monolog.processor, handler: main }

  # Moves the contents of the content field to the field specified in the processor constructor + removes the context field
  YaPro\MonologExt\Processor\RenameContextProcessor:
    class: YaPro\MonologExt\Processor\RenameContextProcessor
    tags:
      - { name: monolog.processor, handler: main, priority: -1 }

  # Moves the contents of the content field to the location specified in the record field + removes the context field
  YaPro\MonologExt\Processor\MoveContextProcessor:
    class: YaPro\MonologExt\Processor\MoveContextProcessor
    tags:
      - { name: monolog.processor, handler: main, priority: -1 }

  # Adds a request as curl command to a log record
  # Old version - https://github.com/yapro/monolog-ext/blob/php5/src/Monolog/Processor/RequestAsCurl.php
  monolog.processor.request_as_curl:
    class: Debug\Monolog\Processor\RequestAsCurl
    arguments: [ "@request_stack" ]
    tags:
      - { name: monolog.processor, handler: main }

  # not implemented yet. Old version - https://github.com/yapro/monolog-ext/blob/php5/src/Monolog/Processor/Guzzle.php
  monolog.processor.guzzle:
    class: Debug\Monolog\Processor\Guzzle
    tags:
      - { name: monolog.processor, handler: main }
  # странная особенность - если не объявить, то возникает ошибка: Cannot autowire service, no such service exists. You
  # should maybe alias this class to one of these existing services: "monolog.formatter.json", "monolog.formatter.loggly".
  # создал вопрос: https://github.com/symfony/symfony/issues/36527
  Monolog\Formatter\JsonFormatter:
    class: Monolog\Formatter\JsonFormatter

then use logger, examples:

$logger->info('I just got the logger');
$logger->error('An error occurred');

Look up, variable $e will be transformed to string (Monolog`s functionality), and you will get: Message of Exception + Stack trace

$logger->warning('My warning', array(
   'my' => 'data',
   'exception' => $e,// now you can see the above written custom stack trace as a string
));
$logger->warning('My second warning', array($e));// the short variant of version which you can see the above

By default, \YaPro\MonologExt\VarHelper extract an extra data into string by standard depth's level which is equal to two. But, you can use any depth's level, example is equal a five:

$logger->error('An error occurred', [ 'my mixed type var' => (new VarHelper)->dump($myVar, 5) ] );

What is ExtraException

ExtraException is exception which you can to create as object, to add the extra data and throw away. After throwing the Monolog ExceptionProcessor will catches this exception and saves extra data to logs. Examples:

throw (new ExtraException())->setExtra('mixed data');

Recommendation

Add service json_formatter to file app/config/config.yml It will help you to format error in the json, and then you can use https://www.elastic.co/products/kibana for aggregate all errors.

services:
    json_formatter:
        class: Monolog\Formatter\JsonFormatter

And don`t forget to add a monolog formatter:

monolog:
    handlers:
        main:
            formatter: json_formatter

If you wish to collect some data of http request, you can add WebProcessor:

services:
    monolog.processor.web:
        class: Monolog\Processor\WebProcessor
        tags:
            - { name: monolog.processor, handler: main }

The configuration without Symfony framework.

Here is a configuration sample:

<?php

use Monolog\Logger;
use YaPro\MonologExt\Handler\WiseHandler;

$logger = new Logger('app');
$logger->pushHandler(new WiseHandler());

$logger->error('Payment error', ['order_id' => 42]);

If you want to handle errors:

<?php

use YaPro\MonologExt\PrudentErrorHandler;

new PrudentErrorHandler($logger);

Tests

docker build -t yapro/monolog-ext:latest -f ./Dockerfile ./
docker run --rm -v $(pwd):/app yapro/monolog-ext:latest bash -c "cd /app \
  && composer install --optimize-autoloader --no-scripts --no-interaction \
  && /app/vendor/bin/phpunit /app/tests"

Dev

docker build -t yapro/monolog-ext:latest -f ./Dockerfile ./
docker run -it --rm --user=$(id -u):$(id -g) --add-host=host.docker.internal:host-gateway -v $(pwd):/app -w /app yapro/monolog-ext:latest bash
composer install -o

Run tests with xdebug:

PHP_IDE_CONFIG="serverName=common" \
XDEBUG_SESSION=common \
XDEBUG_MODE=debug \
XDEBUG_CONFIG="max_nesting_level=200 client_port=9003 client_host=host.docker.internal" \
/app/vendor/bin/phpunit /app/tests

yapro/debug 适用场景与选型建议

yapro/debug 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.91k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 yapro/debug 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2015-09-14