Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature swoole #12

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
checks:
php:
code_rating: true
duplication: true
php: true
filter:
excluded_paths:
- tests/*
- 'tests/*'
build:
tests:
override:
-
command: ./vendor/bin/phpunit --coverage-clover=clover.xml
coverage:
file: clover.xml
format: php-clover
environment:
php:
version: 7.2
ini:
'date.timezone': 'Asia/Shanghai'
dependencies:
before:
- pecl install swoole-4.4.5
- composer install
nodes:
analysis:
project_setup:
override: true
tests:
override:
- php-scrutinizer-run --enable-security-analysis
50 changes: 37 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
language: php

php:
- 7.1
- 7.2
sudo: required

before_script:
- pecl install SeasLog
- composer self-update
- composer install --prefer-source --no-interaction --dev
matrix:
include:
- php: 7.1
env: SW_VERSION="4.4.5";SL_VERSION="SeasLog-2.0.2"
- php: 7.2
env: SW_VERSION="4.4.5";SL_VERSION="SeasLog-2.0.2"
- php: 7.3
env: SW_VERSION="4.4.5";SL_VERSION="SeasLog-2.0.2"
- php: master
env: SW_VERSION="4.4.5";SL_VERSION="SeasLog-2.0.2"

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
allow_failures:
- php: master

script: ./vendor/bin/phpunit --coverage-clover=coverage.clover
services:
- mysql

matrix:
fast_finish: true
before_install:
- export PHP_MAJOR="$(`phpenv which php` -r 'echo phpversion();' | cut -d '.' -f 1)"
- export PHP_MINOR="$(`phpenv which php` -r 'echo phpversion();' | cut -d '.' -f 2)"
- echo $PHP_MAJOR
- echo $PHP_MINOR

install:
- cd $TRAVIS_BUILD_DIR
- bash .travis/swoole.install.sh
- bash .travis/seaslog.install.sh
- phpenv config-rm xdebug.ini || echo "xdebug not available"
- phpenv config-add .travis/ci.ini

before_script:
- cd $TRAVIS_BUILD_DIR
- composer self-update
- composer install --prefer-source --no-interaction --dev

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

script: ./vendor/bin/phpunit --coverage-clover=coverage.clover
8 changes: 8 additions & 0 deletions .travis/ci.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[opcache]
opcache.enable_cli=1

[seaslog]
extension = "seaslog.so"

[swoole]
extension = "swoole.so"
10 changes: 10 additions & 0 deletions .travis/seaslog.install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
wget https://github.com/SeasX/SeasLog/archive/${SL_VERSION}.tar.gz -O SeasLog.tar.gz
mkdir -p SeasLog
tar -xf SeasLog.tar.gz -C SeasLog --strip-components=1
rm SeasLog.tar.gz
cd SeasLog
phpize
./configure
make -j$(nproc)
make install
10 changes: 10 additions & 0 deletions .travis/swoole.install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
wget https://github.com/swoole/swoole-src/archive/v${SW_VERSION}.tar.gz -O swoole.tar.gz
mkdir -p swoole
tar -xf swoole.tar.gz -C swoole --strip-components=1
rm swoole.tar.gz
cd swoole
phpize
./configure --enable-openssl --enable-mysqlnd
make -j$(nproc)
make install
140 changes: 122 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,31 @@ Install the latest version with
$ composer require seasx/seas-logger
```

### Basic Usage
简介
---------
* 基于`Swoole`的日志组件,采用`Seaslog`日志模板,符合PSR-3,内置多种Target输出,包括`Kafka`,`SeasStash`,`Websocket`,`Console`。
* 没有做文件`Target`,在高并发下同时写同一个文件,加锁会影响性能,不加锁的方案还没通过,暂时不支持。(有好的方案请多多指教)

功能
------------------------
* 支持多种输出`Target`,可以自定义输出,继承`AbstractTarget`抽象类
* 可选记录日志等级,`Target`可以单独过滤已选的日志等级日志
* `Console`,`Websocket`输出支持彩色字体
* 支持日志Buffer,定时或定量输出。注意:定时或定量输出由于在输出前日志存在内存,宕机会有丢的风险。
* 支持`Seaslog`,`LoggerConfig`替换为`SeaslogConfig`,需要最新版`Seaslog`(还没发布),暂时可以用`LoggerConfig`


食用方式
----
* 日志收集建议使用`docker`目录下的`docker-compose.yaml`启动套件,用`sql`目录下的`seaslog.sql`在`Clickhouse`中创建数据库和表,`LoggerConfig`中添加`KafkaTarget`,即可在`Clickhouse`中看到日志
* 默认带的BI套件为`Superset`,可以做一些分析
* 生成环境建议`StyleTarget`或者`WebsocketTarget`仅输出`Warning`和`Error`日志或仅保留`KafkaTarget`,使用`SeaslogConfig`(等待最新版本发布)配置,`KafkaTarget`可以输出所有日志
* `WebsocketTarget`由于各个框架的`Server`获取方式不一致,需要调用`setGetServer`注册获取`Server`的回调函数返回`\Swoole\Server`,默认会往所有`Websocket`连接发送日志,如果需要过滤`fd`可以自定义`Target`。
* 支持自定义模板,符号为`%A`,默认在`%M`之前,可以在`registerTemplate`里面设置统一的值,同时会被日志记录方法的`Context`参数中设置`template`键值覆盖。
* 撸码建议采用`DI`依赖注入的方式注册`Logger`


### 非Swoole用法

```php
<?php
Expand All @@ -35,25 +59,105 @@ $logger = new Logger();
$logger->warning('Hello');
$logger->error('SeasLogger');
```
### configuration for laravel/lumen >=5.6
add seaslog configuration in config/logging.php
### Swoole用法
```php
'channels' => [
...
'seaslog' => [
'driver' => 'custom',
'via' => \Seasx\SeasLogger\Logger::class,
'path' => '/path/to/logfile',
],
...
]
```
<?php

use Seasx\SeasLogger\ArrayHelper;
use Seasx\SeasLogger\ConsoleColor;
use Seasx\SeasLogger\Context;
use Seasx\SeasLogger\HtmlColor;
use Seasx\SeasLogger\Kafka\Broker;
use Seasx\SeasLogger\Kafka\Producter;
use Seasx\SeasLogger\Kafka\ProducterConfig;
use Seasx\SeasLogger\Kafka\Socket\Pool;
use Seasx\SeasLogger\Logger;
use Seasx\SeasLogger\LoggerConfig;
use Seasx\SeasLogger\Targets\KafkaTarget;
use Seasx\SeasLogger\Targets\StyleTarget;

\Co\Run(function () {
$logger = new Logger(
new LoggerConfig([
'echo' => new StyleTarget([
'info',//过滤等级,默认为[]全部输出
]),
'kafka' => new KafkaTarget(
new Producter(
new ProducterConfig([
'requiredAck' => 0,
]),
new Broker([
'brokerVersion' => '1.0.0',
]), new Pool([
'uri' => '192.168.5.134:9092'
])),
[],
'seaslog_test',
[['task_id', 'string'], ['worker_id', 'string']]//自定义模板添加的处理字段,顺序需要按照日志记录中的template数组一致
)
], [
'appName' => 'Seaslog',//应用名:远程发送日志的时候用于区分是哪个应用发送来的
'bufferSize' => 1000,//定量:buffer>=时会输出,默认为1
'tick' => 3,//定时:每tick秒输出,默认为0,不开启定时
'recall_depth' => 2,//与Seaslog配置参数一样,默认为0
]));
/**
* 这里可以注册一个回调函数,用来处理RequestID,Request URI,Request Method,Client IP的值
* 下面是示例代码,具体的设置根据自己需要
*/
$logger->getConfig()->registerTemplate(function () {
$possibleStyles = (new ConsoleColor())->getPossibleStyles();
$htmlColors = HtmlColor::getPossibleColors();
if (($requestVar = Context::get(Logger::CONTEXT_KEY)) === null) {
/** @var Request $serverRequest */
if (($serverRequest = Context::get('request')) !== null) {
$uri = $serverRequest->getUri();
$requestId = $serverRequest->getAttribute(AttributeEnum::REQUESTID_ATTRIBUTE);
!$requestId && $requestId = uniqid();
$requestVar = array_filter([
'%Q' => $requestId,
'%R' => $uri->getPath(),
'%m' => $serverRequest->getMethod(),
'%I' => ArrayHelper::getValue($serverRequest->getServerParams(), 'remote_addr'),
'%c' => [
$possibleStyles[rand(0, count($possibleStyles) - 1)],
$htmlColors[rand(0, count($htmlColors) - 1)]
]
]);
} else {
$requestVar = array_filter([
'%Q' => uniqid(),
'%c' => [
$possibleStyles[rand(0, count($possibleStyles) - 1)],
$htmlColors[rand(0, count($htmlColors) - 1)]
]
]);
}
$requestVar['%A'] = ['123', '456'];//%A为自定义字段,会被log里面的template覆盖
Context::set(Logger::CONTEXT_KEY, $requestVar);
}
return $requestVar;
});
/*
* 这里区别于标准PSR-3,Context占用两个固定key(module),作用与Seaslog的Logger参数一样,默认值为System
* template为用户自定义模板对应的填充值,默认为[],不填充
*/
$logger->info("test logger $i", ['module' => 'logger', 'template' => ['abc', 'def']]);
});

edit .env file to use seaslog
```php
LOG_CHANNEL=seaslog
```

### See more
[https://github.com/SeasX/SeasLog](https://github.com/SeasX/SeasLog)
配套组件`docker-compose`
------------------------
* `Clickhouse`日志存储。
* `ClickhouseWeb`Clickhouse操作Web界面
* `Zookeeper`Kafka依赖
* `Kafka`日志队列
* `Manager`Kafka管理Web界面
* `Superset`BI分析
* `mysql`Superset依赖
* `redis`Superset依赖
* `Grafana`监控(还没添加)


83 changes: 45 additions & 38 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
{
"name": "seasx/seas-logger",
"description": "An effective,fast,stable log package for PHP",
"keywords": [
"log",
"logging",
"psr-3"
],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "SeasX",
"homepage": "https://github.com/SeasX"
"name": "seasx/seas-logger",
"description": "An effective,fast,stable log package for PHP",
"keywords": [
"log",
"logging",
"psr-3"
],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "SeasX",
"homepage": "https://github.com/SeasX"
}
],
"require": {
"php": "^7.1",
"psr/log": "^1.0.2",
"wujunze/php-cli-color": "^2.4",
"ext-seaslog": "^2.0",
"lcobucci/clock": "^1.0",
"friendsofphp/php-cs-fixer": "^2.11"
},
"require-dev": {
"swoole/ide-helper": "@dev",
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-4": {
"Seasx\\SeasLogger\\": "src/"
},
"files": [
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
"Seasx\\SeasLogger\\Tests\\": "tests/"
}
},
"scripts": {
"test": [
"./vendor/bin/phpunit"
]
}
],
"require": {
"php": "^7.0",
"psr/log": "^1.0.2",
"ext-SeasLog": "^2.0",
"friendsofphp/php-cs-fixer": "^2.11"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-4": {
"Seasx\\SeasLogger\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Seasx\\SeasLogger\\Tests\\": "tests/"
}
},
"scripts": {
"test": [
"./vendor/bin/phpunit"
]
}
}

Loading