Skip to content

Commit

Permalink
直接写日志
Browse files Browse the repository at this point in the history
  • Loading branch information
Joyboo committed Sep 26, 2023
1 parent 054a0bc commit eaee6fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 77 deletions.
91 changes: 15 additions & 76 deletions src/Common/Logs/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,18 @@

namespace WonderGame\EsUtility\Common\Logs;

use EasySwoole\Component\Timer;
use EasySwoole\Log\LoggerInterface;
use Swoole\Coroutine;

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

protected $times = 10;

protected $writeing = false;

/**
* @param string|null $logDir 日志主目录
* @params int $times
*/
public function __construct(string $logDir = null, $times = 10)
{
$this->logDir = $logDir ?: '';
$times && $this->times = $times;
}

public function __destruct()
{
$this->run();
}
protected $logDir = '';

public function log($msg, int $logLevel = self::LOG_LEVEL_INFO, string $category = 'debug'): string
{
Expand All @@ -40,18 +23,8 @@ public function log($msg, int $logLevel = self::LOG_LEVEL_INFO, string $category
'level' => $logLevel,
'category' => $category
]);
$this->list[] = $Item;

if (Coroutine::getCid() < 0) {
$this->run();
} else {
Coroutine::defer(function () {
$this->run();
});
if (count($this->list) >= $this->times) {
$this->run();
}
}
$this->save($Item);
return $Item->getWriteStr();
}

Expand All @@ -67,59 +40,25 @@ public function console($msg, int $logLevel = self::LOG_LEVEL_INFO, string $cate
fwrite(STDOUT, "$str\n");
}

public function run()
public function save(Item $Item)
{
if ($this->writeing) {
return;
}
$this->writeing = true;

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

if (empty($this->list)) {
return;
}

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

// $apart-独立记录, $arr-通用记录
$apart = $arr = [];
/** @var Item $Item */
foreach ($this->list as $key => $Item) {

$str = $Item->getWriteStr();

if (in_array($Item->level, $apartLevel)) {
$apart[$Item->level][] = $str;
} elseif (in_array($Item->category, $apartCategory)) {
$apart[$Item->category][] = $str;
} else {
$arr[] = $str;
}

unset($this->list[$key]);
}
$this->list = [];
$this->writeing = false;

foreach ($apart as $name => $values) {
$this->save($dir, $values, $name);
}
$arr && $this->save($dir, $arr);
}

protected function save($dir, $values, $name = '')
{
if (empty($values)) {
return;
$name = '';
if (in_array($Item->level, $apartLevel)) {
$name = '-' . $Item->level;
} elseif (in_array($Item->category, $apartCategory)) {
$name = '-' . $Item->category;
}
$name = $name ? "-$name" : '';

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

private function levelMap(int $level)
Expand Down
2 changes: 1 addition & 1 deletion src/Consumer/BaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function run()
}
try {
if ( ! empty($this->args['json'])) {
$data = json_decode($data);
$data = json_decode($data, true);
}
$this->consume($data, $Redis);
} catch (\Throwable $throwable) {
Expand Down

0 comments on commit eaee6fa

Please sign in to comment.