定制 senkevich33n/laravel-extended-logging 二次开发

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

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

senkevich33n/laravel-extended-logging

Composer 安装命令:

composer require senkevich33n/laravel-extended-logging

包简介

Extend Laravel and Lumen logging to better support containers

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

Laravel and Lumen Extended Logging

Provide some ready-made logging extensions to make it easier to deploy Laravel and Lumen into multiple containers.

The main features are:

  • A bunch of useful standard monolog processors.
  • Laravel user ID, if available.
  • Fully qualified class name of the job that is running.
  • A sequence number so logs can be reordered when they get mixed up.
  • Application name and subsystem name.
  • Logs written in structured JSON.

The features applied to the logging here are lightweight, opinionated, mainly non-configurable, and are what we have found to be very useful for our own projects. We are happy to accept PRs for additional features, though with the consideration that this package is not intended to be "all singing, all dancing", but rather a quick and easy install that gets an application logging container-ready with minimal effort.

Installation

For Laravel:

php composer require consilience/laravel-extended-logging

Lumen requires the provider to be registered in bootstrap/app.php so that the package can keep track of the name of the job currently running:

$app->register(Consilience\Laravel\ExtendedLogging\LoggingServiceProvider::class);

Configuration

The main configuration happens through the Laravel config/logging.php configuration script, by adding a channel.

<?php

use Monolog\Handler\StreamHandler;
use Monolog\Formatter\JsonFormatter;
use Consilience\Laravel\ExtendedLogging\Tap as ExtendedTap;

// ...

    'channels' => [
        'my-extended-logging-channel' => [
            //
            // monolog is the underlying driver.
            //
            'driver' => 'monolog',
            //
            // This is the handler to use within monolog, with any parameters to configure it.
            // Handlers can be found in \Monolog\Handler namespace.
            // Here send to a file stream.
            //
            'handler' => StreamHandler::class,
            //
            // Parameters for the monolog handler.
            // Here the file stream is stderr.
            //
            'with' => [
                'stream' => 'php://stderr',
            ],
            //
            // The custom tap to offer additional manipulation of the log output.
            // Other taps from other packages can be added here to extend further.
            //
            'tap' => [
                ExtendedTap::class,
            ],
            //
            // The output formatter.
            // The standard Monolog json formatter has a good structure that is easy to parse.
            //
            'formatter' => JsonFormatter::class,
            'formatter_with' => [],
        ],
    ],

A more compact version of the config entry, to go into the channels section of config/logging.php:

use Monolog\Handler\StreamHandler; // Most likely already present.
use Monolog\Formatter\JsonFormatter;
use Consilience\Laravel\ExtendedLogging\Tap as ExtendedTap;
    'channels' => [
    
        // Use this channel for running in a container.
        // Sends all logs to stderr in a structured form, with additional metadata.
        // Can be mixed in a stack with other channels.
        'container' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'with' => [
                'stream' => 'php://stderr',
            ],
            'tap' => [
                ExtendedTap::class,
            ],
            'formatter' => JsonFormatter::class,
            'formatter_with' => [],
        ],
        
        //...
    ],

Then set LOG_CHANNEL=container when running in a container to send all logs to stderr. Other channels may be more suitable for other environments.

Additional options are available by publishing the config file (laravel-extended-logging.php) for the package:

php artisan vendor:publish --tag=laravel-extended-logging-config

Two options are supported at this time:

  • json-pretty-print - set to true to format the JSON output to be more human readable
  • processor - a list of monolog processor classes

The list of processors, by default, will include the custom processors provided by this package, and a few of the processors that monolog provides. You can remove what you don't want, and add any others you may need.

If a processor accepts parameters, use this form:

Processor::class => [parameter1, parameter2, ...],

Only positional parameters are supported at this time.

The configuration file still accepts instantiated processor objects for legacy installs. You should change those to the uninstantiated Processor::class form to support configuration cacheing.

Configuration Upgrade

Since release 1.2.0 the processors to run have been located in the config file. You will need to publish the config file again to use the processors.

Example Usage

We run Laravel and Lumen applications in a Kubernetes/Docker environment, with all log entries being indexed by elastic search and presented by Kibana. This lumps all our logs from multiple applications, multiple pods and containers, and multiple jobs, into one big database.

To search and filter those log entries, it is vital for context information to be logged in a filterable way.

The PsrLogMessageProcessor, included in this log tap, makes it very easy to combine context data and log message construction. Logging looks like this as a result, with both a context array of data, and the log message with field replacements done:

Log::debug('Product {productId} added to category {categorySlug}', [
    'productId' => $product->id,
    'productName' => $product->name,
    'categorySlug' => $category->slug,
]);

The generated log message would look something like this, embedded into whatever you use to capture and wrap the log messages.

  "_source": {
    "@timestamp": "2020-03-17T11:45:15.573Z",
    "stream": "stderr",
    "time": "2020-03-17T11:45:15.57341869Z",
    "message": "Product 123 added to category good-stuff",
    "context": {
      "productId": 123,
      "productName": "A Nice Slice of Cake",
      "categorySlug": "good-stuff",
    },
    "level": 100,
    "level_name": "DEBUG",
    "channel": "development",
    "datetime": {
      "date": "2020-03-17 11:45:15.572810",
      "timezone_type": 3,
      "timezone": "UTC"
    },
    "extra": {
      "memory_usage": "24 MB",
      "process_id": 1,
      "uid": "58bcec3ef88a7ceb",
      "job_name": "App\\Jobs\\ImportProductCategories",
      "application": "great-shop",
      "subsystem": "admin-app"
    },
  },

TODO

  • Tests.

senkevich33n/laravel-extended-logging 适用场景与选型建议

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

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

围绕 senkevich33n/laravel-extended-logging 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-07