This commit is contained in:
2021-07-12 11:24:33 +08:00
parent 20fbbeb666
commit 99f7ebd712
+32 -8
View File
@@ -10,15 +10,12 @@ declare(strict_types=1);
namespace Snowflake\Error;
use Exception;
use HttpServer\Http\Context;
use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config;
use Snowflake\Core\Json;
use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Swoole\Process;
use Throwable;
/**
@@ -172,19 +169,27 @@ class Logger extends Component
*/
public function write(string $messages, string $method = 'app')
{
return;
if (empty($messages)) {
return;
}
$to_day = date('Y-m-d');
$dirName = 'log/' . ($method ?? 'app');
$fileName = storage('server-' . date('Y-m-d') . '.log', $dirName);
if (!isset($this->sources[$fileName])) {
$this->sources[$fileName] = fopen($fileName, 'rw');
if (!isset($this->sources[$to_day])) {
$this->sources[$to_day] = [];
}
$fileName = storage('server-' . $to_day . '.log', $dirName);
if (!isset($this->sources[$to_day][$fileName])) {
$this->sources[$to_day][$fileName] = fopen($fileName, 'rw');
}
fwrite($this->sources[$fileName], '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL);
$this->clearHistoryFile($dirName);
$this->clearPrevLog($to_day);
}
@@ -200,6 +205,25 @@ class Logger extends Component
}
/**
* @param $to_day
*/
public function clearPrevLog($to_day)
{
if (count($this->sources) > 1) {
foreach ($this->sources as $day => $source) {
if ($day == $to_day) {
continue;
}
foreach ($source as $value) {
fclose($value);
}
unset($this->sources[$day]);
}
}
}
/**
* @param string $dirName
* @throws \Exception