承接 paysera/lib-logging-extra-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

paysera/lib-logging-extra-bundle

Composer 安装命令:

composer require paysera/lib-logging-extra-bundle

包简介

Symfony bundle for further Monolog, Sentry and Graylog integration

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Symfony bundle for further Monolog, Sentry and Graylog integration.

Why?

Monolog already offers integration with both Sentry and Graylog. This bundle re-uses those integrations, adding the following features:

  • clearer formatting for Graylog messages;
  • adds correlation_id to correlate messages in Sentry with messages in Graylog from the same process;
  • allows grouping some exceptions by their class, independently from where they were thrown at or what are their message;
  • removes root prefix from messages (usually included in some exception messages);
  • maps context to be available with logged sentry event.

Also recommended configuration is given to allow nice synergy between Graylog and Sentry.

Installation

composer require symfony/monolog-bundle sentry/sentry-symfony paysera/lib-logging-extra-bundle

Register installed bundles in your kernel or bundles.php file, if not installed automatically by flex.

Configuration

This is recommended configuration for all three bundles. This ensures that:

  • INFO and above level log messages always goes to Graylog;
  • in case ERROR and above level log message is received, all messages (even DEBUG, but maximum 50) goes to Graylog. This helps to debug any problems as you get much more information about what has happened;
  • ERROR level log messages goes to Sentry, so you can see any errors occurring in your application;
  • logging does not break or impact your application, even if your Sentry or Graylog servers are down:
    • Graylog uses UDP, so no need to wait for responses. Handler is also wrapped in a failsafe to avoid any errors on DNS resolution impacting your application;
    • Sentry uses HTTP, but has error handling by default, also sends the messages on process shutdown; configuration overrides default 3 retries to just one – if sentry is overwhelmed with requests already, no need to send any more messages 3 times in a row;
  • messages are visible on console. You can pass arguments to any command for further verbosity level to see INFO or even DEBUG log messages.
monolog:
    handlers:
        info:
            type: filter
            accepted_levels: [INFO, NOTICE, WARNING]
            handler: graylog_failsafe
        debug_and_errors:
            type: filter
            accepted_levels: [DEBUG, ERROR, CRITICAL, ALERT, EMERGENCY]
            handler: graylog_fingers_crossed
        console:
            type: console
            process_psr_3_messages: false
            channels: ["!event", "!doctrine"]
        sentry:
            type: service
            id: paysera_logging_extra.sentry_handler
        graylog_fingers_crossed:
            type: fingers_crossed
            action_level: error
            handler: graylog_failsafe
            stop_buffering: false
            buffer_size: 50
            nested: true
        graylog_failsafe:
            type: whatfailuregroup
            members: [graylog]
            nested: true
        graylog:
            type: gelf
            publisher:
                hostname: '%env(GRAYLOG_HOSTNAME)%'
                port: '%env(GRAYLOG_PORT)%'
                chunk_size: 8154
            formatter: paysera_logging_extra.formatter.gelf_message     # registered by the bundle
            nested: true

sentry:
    dsn: '%env(SENTRY_DSN)%'
    register_error_listener: false
    tracing:
        enabled: false # If using self-hosted Sentry version < v20.6.0
    options:
        environment: '%kernel.environment%'
        release: '%env(VERSION)%' # your app version, optional

paysera_logging_extra:
  application_name: app-something   # customise this to know which project message was sent from

Dual-write to stdout (VictoriaLogs)

To write every record to both Graylog and stdout (compact JSON Lines, collected by VictoriaLogs), add a stdout handler using paysera_logging_extra.formatter.stdout_json and make it a member of the existing graylog_failsafe whatfailuregroup. The whatfailuregroup isolates failures per member, so Graylog being unreachable cannot break stdout and vice-versa. No per-call-site changes are needed, and the existing Graylog delivery is untouched.

monolog:
    handlers:
        graylog_failsafe:
            type: whatfailuregroup
            members: [graylog, stdout]   # add stdout as a second dual-write target
            nested: true
        stdout:
            type: stream
            path: 'php://stdout'
            level: debug                 # upstream filters/fingers_crossed already gate what reaches the group
            formatter: paysera_logging_extra.formatter.stdout_json   # registered by the bundle
            nested: true

Each stdout line is one compact JSON object: timestamp (ISO-8601, microseconds + offset), application_name, channel, syslog level (DEBUG=7 … EMERGENCY=0), level_name, message, optional full_message (the raw original when the message is exception-shaped), optional context/extra, and a top-level correlation_id (hoisted from extra). Lines are capped at 32766 bytes; oversize records stay single-line, are flagged truncated, and are shrunk by dropping full_message first, then context/extra, then truncating message UTF-8-safely, and finally dropping correlation_id.

Exception-shaped messages (RuntimeException: boom in /app/src/Foo.php:42) are split into a short message and the raw full_message. Matching is case-insensitive, which differs from the canonical formatter in evp/lib-application-logging-bundle: that one only recognises a lowercase exception, so it leaves standard PHP exception messages unsplit and emits no full_message for them.

Usage

Log with INFO level and above to get messages in Graylog.

Log with ERROR level and above to get messages in Sentry.

Log with DEBUG level to get messages in Graylog in case any error occurs in the same request / process. To find those, start with error in Sentry and search messages in Graylog by provided correlation_id.

Semantic versioning

This bundle follows semantic versioning.

Public API of this bundle (in other words, you should only use these features if you want to easily update to new versions):

  • only services that are not marked as public="false";
  • only classes, interfaces and class methods that are marked with @api;
  • console commands;
  • supported DIC tags.

For example, if only class method is marked with @api, you should not extend that class, as constructor could change in any release.

See Symfony BC rules for basic information about what can be changed and what not in the API. Keep in mind, that in this bundle everything is @internal by default.

Running tests

composer update
composer test

Contributing

Feel free to create issues and give pull requests.

Running dependencies locally

cd example
docker-compose up -d
docker-compose exec sentry sentry upgrade

You'll find Graylog at http://localhost:9001/ and Sentry at http://localhost:9002/.

Open Graylog, login with admin admin, choose System -> Inputs -> GELF UDP -> Launch new input -> input any title and select the node -> Save.

Open Sentry, login with user created by the last command, choose Installation instructions -> Symfony2 -> copy the credentials part (protocol, hostname and port might be missing). Run following command, exchanging HERE_GOES_CREDENTIALS with real credentials:

export SENTRY_DSN=http://HERE_GOES_CREDENTIALS@localhost:9002/1

And run test PHP script:

php test.php

View logged data in Graylog and Sentry instances. Change the code for further test scenarios or just use Graylog and Sentry to set-up and test your real project.

Cleanup afterwards:

docker-compose down

paysera/lib-logging-extra-bundle 适用场景与选型建议

paysera/lib-logging-extra-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.5k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 paysera/lib-logging-extra-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-08