-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from wonder-game/develop
日志和消费进程参数调整
- Loading branch information
Showing
7 changed files
with
148 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters