hisune/log2ck
Composer 安装命令:
composer require hisune/log2ck
包简介
Real-time, high-performance reading of log files and writing to clickhouse.
README 文档
README
English | 中文
This tool can write the monolog standard log directly to clickhouse in real time via the tcp protocol. If you can write regular rules, other standardized log can also support it.
Feature
- Minimalist code
- High performance (Verify that the cpu usage in online business is only 1/20 of
filebeat) - No dependence on third-party services (such as queues, etc.)
- Configurable
- Customization (custom regularization, line processing callback functions)
- Supports reading log divided by day
- Supports automatic breakpoint resume collection
- Supports batch insert data
- Supports graceful restart
Usage specification
- If you use the default regularity, the log file that needs to be read must be the standard default monolog log format file, and the monolog
nameandgroupname cannot contain spaces. - The log to be read must be one line at a time. For example, monolog needs to set the formatter to:
'allowInlineLineBreaks'= > false
How to use
# Install composer require "hisune/log2ck" # Modify config.php to the configuration you want cp vendor/hisune/log2ck/test.config.php config.php # Create manager vim manager.php
Example of the content of the manager.php file:
<?php use Hisune\Log2Ck\Manager; require_once 'vendor/autoload.php'; (new Manager(__DIR__ . DIRECTORY_SEPARATOR . 'config.php'))->run();
# Begin execution
php manager.php
By default, the manager and worker execution logs can be seen in the vendor/hisune/log2ck/logs/ directory. You can also modify the storage path of these two logs through the configuration file.
config.php Configuration example
return [ 'env' => [ // System environment variables // 'bin' => [ // 'php' => '/usr/bin/php', // Optional configuration, the path to which the php bin file belongs // ], 'clickhouse' => [ // Required configuration 'dsn' => 'tcp://192.168.37.205:9000', 'username' => 'default', 'password' => '', 'options' => [ 'connect_timeout' => 3, 'socket_timeout' => 30, 'tcp_nodelay' => true, 'persistent' => true, ], 'database' => 'logs', // Database name 'table' => 'repo', // Table name 'max_sent_count' => 100, // Insert when there are many pieces of data in a single batch 'max_sent_wait' => 10, // If the number of data items in a single batch is not satisfied, the insertion will be performed at least once in how many seconds ], // 'worker' => [ // 'cache_path' => '/dev/shm/', // Optional configuration, worker cache directory // ], // 'logger' => [ // 'enable' => true, // Optional configuration, whether to record logs // 'path' => __DIR__ . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR, // Specify the directory where the logs are logged, optional configuration, and need to end with / // ], ], 'tails' => [ 'access' => [ // Key is the log name, corresponding to the name field of clickhouse 'repo' => 'api2', // The name of the project to which the log belongs 'path' => '/mnt/c/access.log', // eg: Log path, fixed file name log // 'path' => '/mnt/c/access-{date}.log', // eg: Log path, a daily log with a file name, currently only one macro variable {date} is supported. For example, the date format: 2022-02-22 // 'host' => 'host1', // Customize the host name, the default is the server host name if it is not set, which corresponds to the host field of clickhouse // 'pattern' => '/\[(?P<created_at>.*)\] (?P<logger>\w+).(?P<level>\w+): (?P<message>.*[^ ]+) (?P<context>[^ ]+) (?P<extra>[^ ]+)/', // Optional configuration, if regular processing is not required, set to false // 'callback' => function($data) { // Optional configuration, this line of data is processed according to a custom callback method, and the content of the method can implement any logic for cleaning this stream by itself. // $data['message'] = 'xxoo'; // For example, customize the processing of this data // return $data; // Need to return an array, key is the field name of the table in clickhouse, and value is the stored value // } // 'clickhouse' => [...] // You can also configure the clickhouse connection information for individual projects, and the configuration content is the same as the clickhouse array of env. ], ], ];
supervisord
It is recommended to use supervisor to manage your manager process.
[program:log2ck] directory=/data/log2ck command=php manager.php user=root autostart=true autorestart=true startretries=10 stderr_logfile=/data/logs/err.log stdout_logfile=/data/logs/out.log
clickhouse Log table structure
If you use monolog and use the default regular rules, you can directly use the following table structure. If you have a custom regular, you can customize your own clickhouse table structure based on your own regular matching results.
create table repo
(
repo LowCardinality(String) comment 'Project name',
name LowCardinality(String) comment 'Log name',
host LowCardinality(String) comment 'The machine where the log is generated',
created_at DateTime,
logger LowCardinality(String),
level LowCardinality(String),
message String,
context String,
extra String
) engine = MergeTree()
PARTITION BY toDate(created_at)
ORDER BY (created_at, repo, host)
TTL created_at + INTERVAL 10 DAY;
If the content of your message or context is json, you can refer to clickhouse's json query function:https://clickhouse.com/docs/en/sql-reference/functions/json-functions/
hisune/log2ck 适用场景与选型建议
hisune/log2ck 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 213 次下载、GitHub Stars 达 11, 最近一次更新时间为 2022 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「monolog」 「realtime」 「clickhouse」 「log2ck」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hisune/log2ck 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hisune/log2ck 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hisune/log2ck 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A GeoIP decorator/processor for monolog
Talk is a Laravel based realtime messaging, chatting and conversation system. It helps to develop users messaging, chatting and conversations in easy way.
A Zend Framework module that sets up Monolog for logging in applications.
Laravel 5.6 Monolog custom telegram channel
A Monolog processor to add X-Request-Id or UNIQUE_ID env to monolog stack if present, provide a default one otherwise. Optionally, add a forwarded request-id list.
PSR-3 compatible logger with dynamic file naming and hourly rotation, powered by Monolog
统计信息
- 总下载量: 213
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2022-02-24