Skip to content

Commit

Permalink
Merge pull request #55 from wonder-game/develop
Browse files Browse the repository at this point in the history
日志和消费进程参数调整
  • Loading branch information
Joyboo authored Sep 26, 2023
2 parents c45030e + eaee6fa commit eae5d3b
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 150 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
│ │ ├── ExceptionTrigger 自定义异常处理器,将异常上报至redis或http
│ │ ├── FdManager WebSocket连接符管理,共享内存(Swoole\Table)实现
│ │ ├── LamJwt jwt
│ │ ├── LamLog 自定义日志处理器
│ │ ├── LamOpenssl RSA数据加密和解密
│ │ ├── LamUnit 辅助工具类
│ │ ├── Mysqli 对MysqlClient的二次封装
Expand All @@ -37,6 +36,7 @@
│ │ ├── Dictionary 国际化字典,项目请`继承`它
│ │ └── Languages I18n助手类,主要用来注册、设置
│ │
│ ├── Logs 自定义日志处理器
│ └── OrmCache 模型缓存组件,已实现 String、Hash、Set、SplArray
├── HttpController
Expand Down
136 changes: 0 additions & 136 deletions src/Common/Classes/LamLog.php

This file was deleted.

79 changes: 79 additions & 0 deletions src/Common/Logs/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace WonderGame\EsUtility\Common\Logs;

use EasySwoole\Log\LoggerInterface;

/**
* 日志处理器
*/
class Handler implements LoggerInterface
{
/**
* 本地降级使用的保存目录
* @var string
*/
protected $logDir = '';

public function log($msg, int $logLevel = self::LOG_LEVEL_INFO, string $category = 'debug'): string
{
$logLevel = $this->levelMap($logLevel);
$Item = new Item([
'message' => $msg,
'level' => $logLevel,
'category' => $category
]);

$this->save($Item);
return $Item->getWriteStr();
}

public function console($msg, int $logLevel = self::LOG_LEVEL_INFO, string $category = 'debug')
{
$logLevel = $this->levelMap($logLevel);
$Item = new Item([
'message' => $msg,
'level' => $logLevel,
'category' => $category
]);
$str = $Item->getWriteStr();
fwrite(STDOUT, "$str\n");
}

public function save(Item $Item)
{
empty($this->logDir) && $this->logDir = config('LOG.dir');
$dir = $this->logDir . '/' . date('ym');
is_dir($dir) or @ mkdir($dir, 0777, true);

$apartLevel = config('LOG.apart_level');
$apartCategory = config('LOG.apart_category');

$name = '';
if (in_array($Item->level, $apartLevel)) {
$name = '-' . $Item->level;
} elseif (in_array($Item->category, $apartCategory)) {
$name = '-' . $Item->category;
}

$d = date('d');
$str = $Item->getWriteStr();
file_put_contents("$dir/{$d}{$name}.log", "$str\n", FILE_APPEND | LOCK_EX);
}

private function levelMap(int $level)
{
switch ($level) {
case self::LOG_LEVEL_INFO:
return 'info';
case self::LOG_LEVEL_NOTICE:
return 'notice';
case self::LOG_LEVEL_WARNING:
return 'warning';
case self::LOG_LEVEL_ERROR:
return 'error';
default:
return 'unknown';
}
}
}
55 changes: 55 additions & 0 deletions src/Common/Logs/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace WonderGame\EsUtility\Common\Logs;

use EasySwoole\Spl\SplBean;
use Swoole\Coroutine;
use WonderGame\EsUtility\Common\Classes\DateUtils;

class Item extends SplBean
{
public $level = '';

public $category = '';

public $message = '';

/************* 以下为自动完成的私有属性,勿传 ************/

private $cid = -1;
private $time = 0;
private $date = '';

protected function initialize(): void
{
parent::initialize();
$this->cid = Coroutine::getCid();

if ( ! is_scalar($this->message)) {
$this->message = json_encode($this->message, JSON_UNESCAPED_UNICODE);
}
$this->message = str_replace(["\n", "\r"], '', $this->message);

// 产生日志的时间
$this->time = time();
$this->date = date(DateUtils::FULL, $this->time);
// 不在东8区,则记录东8区时间
$tznInt = intval(substr((int)date('O'), 0, -2));
if ($tznInt !== 8) {
$this->date .= ', +8区: ' . date(DateUtils::FULL, DateUtils::getTimeZoneStamp($this->time, 'PRC'));
}
}

public function getWriteStr()
{
return "[cid={$this->cid}][{$this->date}][{$this->category}][{$this->level}]{$this->message}";
}

public function __get($name)
{
if (property_exists($this, $name)) {
return $this->{$name};
}
throw new \Exception(__CLASS__ . ' Not fount property : ' . $name);
}
}
19 changes: 8 additions & 11 deletions src/Consumer/BaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ trait BaseTrait
* 'queue' => 'queue_login', // 监听的redis队列名
* 'tick' => 1000, // 多久运行一次,单位毫秒, 默认1000毫秒
* 'limit' => 200, // 单次出队列的阈值, 默认200
* 'coroutine' => false // 是否为每条数据开启协程
* 'pool' => 'default' // redis连接池名称
* 'pool' => 'default' // redis连接池名称
* 'json' => false // 是否需要json_decode
* ],
*
*/
Expand All @@ -36,10 +36,11 @@ protected function onException(\Throwable $throwable, ...$args)

/**
* 消费单条数据,由子类继承实现
* @param string $data 每一条队列数据
* @param string|array $data 每一条队列数据
* @param Redis|null $redis redis连接
* @return mixed
*/
abstract protected function consume($data = '');
abstract protected function consume($data = [], Redis $redis = null);

/**
* EasySwoole自定义进程入口
Expand All @@ -63,14 +64,10 @@ public function run()
break;
}
try {
$openCoroutine = $this->args['coroutine'] ?? false;
if ($openCoroutine) {
go(function () use ($data) {
$this->consume($data);
});
} else {
$this->consume($data);
if ( ! empty($this->args['json'])) {
$data = json_decode($data, true);
}
$this->consume($data, $Redis);
} catch (\Throwable $throwable) {
\EasySwoole\EasySwoole\Trigger::getInstance()->throwable($throwable);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Consumer/HttpTrackerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace WonderGame\EsUtility\Consumer;

use EasySwoole\Redis\Redis;

trait HttpTrackerTrait
{
protected function consume($data = '')
protected function consume($data = [], Redis $redis = null)
{
try {

Expand Down
3 changes: 2 additions & 1 deletion src/Consumer/ProcessInfoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace WonderGame\EsUtility\Consumer;

use EasySwoole\ORM\AbstractModel;
use EasySwoole\Redis\Redis;

trait ProcessInfoTrait
{
protected function consume($data = '')
protected function consume($data = [], Redis $redis = null)
{
$data = json_decode($data, true);
if ( ! $data) {
Expand Down

0 comments on commit eae5d3b

Please sign in to comment.