usekamori/kamori-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

usekamori/kamori-php

Composer 安装命令:

composer require usekamori/kamori-php

包简介

PHP SDK for Kamori — self-hosted log ingestion

README 文档

README

PHP 8.1+ SDK for Kamori — self-hosted log ingestion.

Sends structured log events to a Kamori ingest server over HTTP. No curl required — uses PHP's built-in fopen stream context for explicit response header access. Supports batching, automatic retry with exponential back-off, and drop callbacks. The client auto-flushes buffered events in __destruct.

Installation

composer require usekamori/kamori-php

Direct usage

use Kamori\KamoriClient;

$client = new KamoriClient(
    url: 'https://your-kamori-server.com',
    token: 'your-log-token',   // matches INGEST_TOKEN on the server
    batchSize: 50,             // flush automatically every 50 events
);

$client->log([
    'level'   => 'info',
    'message' => 'User signed in',
    'user_id' => 42,
]);

// Always flush at the end of a request or script
$client->flush();

Flush on shutdown (CLI scripts)

register_shutdown_function([$client, 'flush']);

Monolog 3 handler

composer require monolog/monolog
use Kamori\Monolog\KamoriHandler;
use Monolog\Logger;
use Monolog\Level;

$logger = new Logger('app');
$logger->pushHandler(new KamoriHandler(
    url: 'https://your-kamori-server.com',
    token: 'your-log-token',
    batchSize: 50,
    level: Level::Debug,
));

$logger->info('Hello from Monolog', ['user_id' => 7]);
$logger->error('Something went wrong', ['exception' => 'RuntimeException']);

// Flush when the handler is closed (called automatically by Monolog on __destruct)
// Or flush explicitly:
$logger->getHandlers()[0]->getClient()->flush();

Context and extra fields are forwarded to Kamori as-is so all structured data is full-text-searchable.

Laravel (zero-config)

Auto-discovery is enabled via composer.json. After composer require usekamori/kamori-php, add to your .env:

KAMORI_URL=https://your-kamori-server.com
INGEST_TOKEN=your-log-token

Optionally publish the config file:

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

This creates config/kamori.php where you can adjust batch_size.

The KamoriClient singleton is automatically flushed at the end of every request via app()->terminating(). No additional setup is required.

Resolve the client manually

use Kamori\KamoriClient;

$client = app(KamoriClient::class);
$client->log(['level' => 'debug', 'message' => 'Manual log entry']);

Configuration reference

Option Default Description
url Base URL of your Kamori server (required)
token null Auth token (sent as Authorization: Bearer). Leave null to skip auth.
batchSize 50 Number of events buffered before auto-flush
maxBuffer 0 Max events in the in-memory buffer. 0 = unlimited. New events are passed to onDrop and discarded when the limit is reached.
onDrop null Callable invoked with the batch when all retries fail

Scoped clients

Add default fields to every log call without repeating them:

class ScopedKamoriClient
{
    public function __construct(
        private KamoriClient $client,
        private array $defaults = [],
    ) {}

    public function log(array $event): void
    {
        $this->client->log(array_merge($this->defaults, $event));
    }

    public function flush(): void
    {
        $this->client->flush();
    }
}

$requestLog = new ScopedKamoriClient($client, [
    'service'    => 'api',
    'request_id' => 'abc-123',
    'user_id'    => 42,
]);

$requestLog->log(['level' => 'info', 'message' => 'Request started']);
$requestLog->log(['level' => 'error', 'message' => 'Validation failed', 'field' => 'email']);

Retry behaviour

Failed requests are retried up to three times with exponential back-off. PHP has no async I/O, so retries block the current process with usleep():

Attempt Delay
1st retry 0.25 s
2nd retry 1 s
3rd retry 4 s

4xx responses are not retried (client error — bad token, oversized batch). After all retries fail the batch is passed to onDrop (if configured) and discarded. The client never throws.

onDrop callback

$client = new KamoriClient(
    url: 'https://your-kamori-server.com',
    token: 'your-log-token',
    onDrop: function (array $events): void {
        error_log('Kamori dropped ' . count($events) . ' events');
    },
);

Requirements

  • PHP 8.1+
  • openssl extension (enabled by default) for HTTPS
  • Monolog 3.x (optional, only needed for KamoriHandler)
  • Laravel 10+ (optional, only needed for KamoriServiceProvider)

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固