定制 moktech/mocklogger-sdk 二次开发

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

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

moktech/mocklogger-sdk

最新稳定版本:1.0.92

Composer 安装命令:

composer require moktech/mocklogger-sdk

包简介

Mocklogger SDK is a Laravel package designed to facilitate effortless integration with the Mocklogger logging tool for developers.

README 文档

README

MockLogger SDK offers functionality for logging request and response data of a Laravel application. This SDK provides methods to capture and log data pertaining to incoming HTTP requests and their associated responses.

With the MockLogger SDK, you can easily capture and log data from HTTP requests and responses, helping you monitor and analyze your application's interactions with external services and users actions.

Installation

To get started with the MockLogger SDK, follow these installation steps:

Install the SDK:

Install the MockLogger SDK using Composer:

composer require moktech/mocklogger-sdk

Publish Configuration:

Publish the MockLogger SDK configuration file using Artisan:

php artisan vendor:publish --tag=mocklogger-config

Register Service Provider:

Open the config/app.php file and add the service provider to the providers array:

'providers' => [
    // ...
    Moktech\MockLoggerSDK\MockloggerServiceProvider::class,
],

Set Environment Variables:

Set the following environment variables in your application's .env. You can obtain these values from your Mocklogger application:

MOCKLOGGER_HOST_URL=http://localhost:8000
MOCKLOGGER_APP_ID="My Application"
MOCKLOGGER_APP_KEY=c3ce75317d9c876d209a9f439b345345
MOCKLOGGER_APP_API_TOKEN=EomxCzUG0HFukdRWgKL26ThXuRstFTW

Usage

Once you have installed the MockLogger SDK and configured your environment, you can now use it to log request and response data. Here are two ways to use the SDK:

Method 1: Using sendLog

Use this in your Terminable Middleware (Sunctum protected URL).

use Moktech\MockLoggerSDK\MockLogger;

class TerminableMiddleware
{
    protected $logger;

    public function __construct(MockLogger $logger)
    {
        $this->logger = $logger;
    }

    public function handle(Request $request, Closure $next): Response
    {
        return $next($request);
    }

    public function terminate(Request $request, Response $response)
    {  
        try {
            $this->logger->sendLog($request, $response);
        } catch (\Throwable $th) {
            Log::info($th->getMessage());
        }
    }
}

Method 2: Using sendData

You can also manually assemble the request and response data and use the sendData method to log it.

use Moktech\MockLoggerSDK\MockLogger;

$data = [
    "request" => [
        'user' => [
            'name' => 'Kevin Muchwat',
            'email' => 'kevinmuchwat@gmail.com',
        ],
        'ip_address' => $request->ip(),
        'full_url' => $request->fullUrl(),
        'route_name' => $request->route()->getName(),
        'method' => $request->method(),
        'payload' => $request->all(),
        'agent' => $request->userAgent(),
    ],
    "response" => [
        'status_code' => $response->getStatusCode(),
        'content' => $response->getContent(),
        'format' => $response->headers->get('content-type'),
        'location' => $response->headers->get('location'),
    ],
];

$logger = new MockLogger();
$logger->sendData($data);

Server Health Monitoring

MockLogger SDK empowers you to maintain optimal server performance by allowing you to set usage limits on CPU, memory, and hard disk space. If any of these limits are exceeded, the SDK automatically sends a detailed log to administrator's email configured in config/mocklogger.php.

return [
    ...
    // Configure server health monitor.
    'monitor' => [
        // Specify the web server service running, e.g., 'nginx' or 'apache2'.
        'server_service' => 'nginx',

        // Set email configuarations, default is 4 emails per 30mins interval.
        'email' => [
            // Set time interval to get emails (minutes), default is 30 minutes
            'interval' => 30,

            // Set number of emails to be sent in an interval, default is 4 emails.
            'count'  => 4,
        ],

        // Configure thresholds for resources.
        'thresholds' => [
            // Set the CPU usage threshold (percentage).
            'cpu_usage' => env('MOCKLOGGER_CPU_THRESHOLD', 90),

            // Set the memory usage threshold (percentage). 
            'memory_usage' => env('MOCKLOGGER_MEMORY_THRESHOLD', 80),

            // Set the hard disk drive usage threshold (percentage).
            'hard_disk_space' => env('MOCKLOGGER_HDD_THRESHOLD', 80),
        ],
    ],
]

To start monitoring your server, run this command:

php artisan mocklogger:monitor

For continuous monitoring, you can schedule this command with a cron job. Keep your server in check effortlessly with this tool.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-21