Skip to content

Commit

Permalink
feat: Storing logs to contexts in hyperf framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance-fyi committed Aug 4, 2023
1 parent 54b110a commit 17a7fcf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ jobs:
fail-fast: false
matrix:
php: [ 8.0, 8.1, 8.2 ]
swoole: [ '', swoole ]

name: PHP ${{ matrix.php }}
name: PHP ${{ matrix.php }} ${{ matrix.swoole }}

steps:
- name: Checkout code
Expand All @@ -28,7 +29,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: pdo, pdo_mysql
extensions: pdo, pdo_mysql, ${{ matrix.swoole }}
ini-values: error_reporting=E_ALL
tools: composer:v2
coverage: none
Expand Down
38 changes: 32 additions & 6 deletions src/OperationLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Chance\Log;

use Chance\Log\facades\OperationLog as OperationLogFacade;
use Hyperf\Context\Context;
use Hyperf\Database\Model\Model as HyperfModel;
use Illuminate\Database\Eloquent\Model as LaravelModel;
use think\Model as ThinkModel;
Expand All @@ -29,6 +30,8 @@ class OperationLog
public const BATCH_UPDATED = 'batch_updated';
public const DELETED = 'deleted';
public const BATCH_DELETED = 'batch_deleted';

private const CONTEXT_LOG = 'context_operation_log';
protected array $tableComment;

protected array $columnComment;
Expand All @@ -47,26 +50,28 @@ public function __construct()

public function getLog(): string
{
$log = $this->log;
$log = $this->getRawLog();
$this->clearLog();

return trim(implode('', $log), PHP_EOL);
}

public function clearLog(): void
{
$this->log = [''];
$this->setRawLog(['']);
}

public function beginTransaction(): void
{
$this->log[] = '';
$log = $this->getRawLog();
$log[] = '';
$this->setRawLog($log);
}

public function rollBackTransaction(int $toLevel): void
{
$this->log = array_slice($this->log, 0, $toLevel);
if (0 === count($this->log)) {
$this->setRawLog(array_slice($this->getRawLog(), 0, $toLevel));
if (0 === count($this->getRawLog())) {
$this->clearLog();
}
}
Expand Down Expand Up @@ -185,7 +190,9 @@ public function generateLog(ThinkModel|LaravelModel|HyperfModel $model, string $
}
if (!empty($log)) {
$log = mb_substr($log, 0, mb_strlen($log, 'utf8') - 1, 'utf8');
array_splice($this->log, -1, 1, end($this->log) . $logHeader . $log . PHP_EOL);
$logs = $this->getRawLog();
array_splice($logs, -1, 1, end($logs) . $logHeader . $log . PHP_EOL);
$this->setRawLog($logs);
}
}

Expand All @@ -198,4 +205,23 @@ public function getTableModelMapping(): array
{
return $this->tableModelMapping;
}

private function getRawLog()
{
if (extension_loaded('swoole') && class_exists(Context::class)) {
return Context::get(self::CONTEXT_LOG, ['']);
}

return $this->log;
}

private function setRawLog(array $log): void
{
if (extension_loaded('swoole') && class_exists(Context::class)) {
Context::set(self::CONTEXT_LOG, $log);

return;
}
$this->log = $log;
}
}

0 comments on commit 17a7fcf

Please sign in to comment.