Skip to content

Commit

Permalink
up: optimize log for output json
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 7, 2021
1 parent a159af8 commit 2a5774d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/log/src/Handler/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ protected function write(array $records): void
} else {
$records = array_column($records, 'formatted');
}
$messageText = implode("\n", $records) . "\n";

$messageText = implode("\n", $records) . "\n";
if (Co::id() <= 0) {
throw new InvalidArgumentException('Write log file must be under Coroutine!');
}
Expand All @@ -125,6 +125,7 @@ protected function write(array $records): void
private function recordFilter(array $records): array
{
$messages = [];
$outJson = Log::getLogger()->isJson();
foreach ($records as $record) {
if (!isset($record['level'])) {
continue;
Expand All @@ -133,8 +134,15 @@ private function recordFilter(array $records): array
continue;
}

$record = $this->processRecord($record);
$record['formatted'] = $this->getFormatter()->format($record);
$record = $this->processRecord($record);

// format record
if ($outJson) {
// optimize, dont call formatter
$record['formatted'] = '';
} else {
$record['formatted'] = $this->getFormatter()->format($record);
}

$messages[] = $record;
}
Expand Down
3 changes: 2 additions & 1 deletion src/log/src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ public function getProfilesInfos(): string
if (!isset($profile['cost'], $profile['total'])) {
continue;
}
$cost = sprintf('%.2f', $profile['cost'] * 1000);

$cost = sprintf('%.2f', $profile['cost'] * 1000);

$profileAry[] = "$key=" . $cost . '(ms)/' . $profile['total'];
}
Expand Down

0 comments on commit 2a5774d

Please sign in to comment.