定制 twentysix22/laraveleslogs 二次开发

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

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

twentysix22/laraveleslogs

Composer 安装命令:

composer require twentysix22/laraveleslogs

包简介

Elasticsearch structured logging for Laravel

README 文档

README

Latest Version on Packagist Total Downloads

Structured logging for Laravel into Elasticsearch. Log your Requests and Jobs easily alongside key contextual information to elastic search indexes for easy searching and analysis.

Take a look at contributing.md to see a to do list.

Installation

Via Composer

$ composer require twentysix22/laraveleslogs

Usage

Publish the provider in laravel:

php artisan vendor:publish --provider="Twentysix22\LaravelESLogs\LaravelESLogsServiceProvider"

Add the route middleware alias to the app/Http/Kernel.php

protected $routeMiddleware = [
...,
...,

'log' => \Twentysix22\LaravelESLogs\Services\Logging\Requests\LogRequest::class,

Configure .env

LOG_GROUP=some-group
LOG_ELASTICSEARCH_HOST=localhost:9200
LOG_ELASTICSEARCH_KEEP_DAYS=5
LOG_JOBS=true
LOG_JOB_ATTEMPTS=true
LOG_REQUESTS=true
LOG_REDACTION_INK=REDACTED
LOG_ELASTICSEARCH_AUTH_TYPE=url   [url or apikey]
LOG_ELASTICSEARCH_AUTH_API_ID=
LOG_ELASTICSEARCH_AUTH_API_KEY=
LOG_REQUEST_MAX_SIZE= (size in characters)
LOG_RESPONSE_MAX_SIZE= (size in characters)
LOG_ELASTICSEARCH_INDEX_DATE_PATTERN="Y.m.d"

Add the service provider to your providers array in config/app.php

\Twentysix22\LaravelESLogs\LaravelESLogsServiceProvider::class,

Initialise the Elasticsearch indices.

This step is not strictly necessary unless you wish to reset any current indices.

php artisan laraveleslogs:configure

Configure auto cleanup of logs by retention days.

You can run a daily command to clear up old logs which will use the LOG_ELASTICSEARCH_KEEP_DAYS env variable number of days retention.

php artisan laraveleslogs:tidy

Logging Requests

Apply the middleware 'log:{namespace}' to your routes to start logging requests. (you can omit the namespace param if you want to but its useful to specify for clarity in logs) eg:

Route::get('/', function () {
    return view('welcome');
})->middleware('log:home');

You can add this middleware to individual routes, groups, or even your global route middlware if you wish to simply log out every request/response. (but we recommend you be more specific in your logging :-) )

Adding Request Context

You can use the Trait use ReportsRequestContext; in your request controllers. This gives a coupld of options to apply context to your logs.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Twentysix22\LaravelESLogs\Services\Logging\Requests\ReportsRequestContext;

class TestController extends Controller
{
    use ReportsRequestContext;

    public function test()
    {
        $this->logContext([
            'key' => 'value',
            'key2' => 'value2',
        ]);

        $this->logGlobalContext([
            'globalkey' => 'value',
            'globalkey2' => 'value2',
        ]);

        return response()->json([
           'hello' => 'world',
        ]);
    }
}

Logging Jobs

Logging of jobs will be enabled by default if you configure your .env as above.

Logging Job Context

You can log out context to your jobs using similar trait to requests above - add use ReportsJobContext; to your Job. eg:

use Twentysix22\LaravelESLogs\Services\Logging\Jobs\ReportsJobContext;

class TestJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    use ReportsJobContext;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->logContext([
            'key' => 'value',
            'key2' => 'value2',
        ]);

        $this->logGlobalContext([
            'globalkey' => 'value',
            'globalkey2' => 'value2',
        ]);
    }
}

Setting custom context name.

You can set a custom context name using traits ReportsJobContext or ReportsRequestContext

$this->setContextName('custom-context-name');

Redaction of sensitive information from logs.

Its important that you do not log out sensitive information in logs for various data protection reasons. You can configure a list of sensitive keys that you would like to redact from your logs in your /config/laraveleslogs.php:

// Keys used to catch elements in logging array keys - will be replaced with redaction ink
    // and not displayed in logs.
    'redaction' => [
        'password',
        'password_confirmation',
        'authorization',
        'telephone_number',
        'email',
        'algolia_key',
        'access-token'
    ],

    // The ink used to redact sensitive keys in logs.
    'redaction_ink' => config('LOG_REDACTION_INK', '[--REDACTED--]'),

You can also specify the redaction ink string that will replace these sensitive keys by configuring the LOG_REDACTION_INK env variable.

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits

License

license. Please see the license file for more information.

twentysix22/laraveleslogs 适用场景与选型建议

twentysix22/laraveleslogs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.38k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 08 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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