-
Notifications
You must be signed in to change notification settings - Fork 2
manage logs for processed and failed messages #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Queue\Swoole\Command; | ||
|
|
||
| use Dot\DependencyInjection\Attribute\Inject; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| use function date; | ||
| use function file; | ||
| use function is_numeric; | ||
| use function json_decode; | ||
| use function preg_match; | ||
| use function strtolower; | ||
| use function strtotime; | ||
|
|
||
| use const FILE_IGNORE_NEW_LINES; | ||
| use const FILE_SKIP_EMPTY_LINES; | ||
|
|
||
| #[AsCommand( | ||
| name: 'failed', | ||
| description: 'Get processing failure messages.', | ||
| )] | ||
| class GetFailedMessagesCommand extends Command | ||
| { | ||
| protected static string $defaultName = 'failed'; | ||
|
|
||
| #[Inject()] | ||
| public function __construct() | ||
| { | ||
| parent::__construct(self::$defaultName); | ||
| } | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| $this->setDescription('Get processing failure messages.') | ||
| ->addOption('start', null, InputOption::VALUE_OPTIONAL, 'Start timestamp (Y-m-d H:i:s)') | ||
| ->addOption('end', null, InputOption::VALUE_OPTIONAL, 'End timestamp (Y-m-d H:i:s)') | ||
| ->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Limit in days'); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $start = $input->getOption('start'); | ||
| $end = $input->getOption('end'); | ||
| $limit = $input->getOption('limit'); | ||
|
|
||
| if (! $end) { | ||
| $end = date('Y-m-d H:i:s'); | ||
| } elseif (! preg_match('/\d{2}:\d{2}:\d{2}/', $end)) { | ||
| $end .= ' 23:59:59'; | ||
| } | ||
|
|
||
| if ($limit && is_numeric($limit) && ! $start) { | ||
| $start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end))); | ||
| } elseif ($start && ! preg_match('/\d{2}:\d{2}:\d{2}/', $start)) { | ||
| $start .= ' 00:00:00'; | ||
| } | ||
|
|
||
| $startTimestamp = $start ? strtotime($start) : null; | ||
| $endTimestamp = $end ? strtotime($end) : null; | ||
|
|
||
| $logPath = 'log/queue-log.log'; | ||
| $lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
|
|
||
| foreach ($lines as $line) { | ||
| $entry = json_decode($line, true); | ||
|
|
||
| if (! $entry || ! isset($entry['levelName'], $entry['timestamp'])) { | ||
| continue; | ||
| } | ||
|
|
||
| if (strtolower($entry['levelName']) !== 'error') { | ||
| continue; | ||
| } | ||
|
|
||
| $logTimestamp = strtotime($entry['timestamp']); | ||
| if ( | ||
| ($startTimestamp && $logTimestamp < $startTimestamp) || | ||
| ($endTimestamp && $logTimestamp > $endTimestamp) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| $output->writeln($line); | ||
| } | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
| } |
This file contains hidden or 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,95 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Queue\Swoole\Command; | ||
|
|
||
| use Dot\DependencyInjection\Attribute\Inject; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| use function date; | ||
| use function file; | ||
| use function is_numeric; | ||
| use function json_decode; | ||
| use function preg_match; | ||
| use function strtolower; | ||
| use function strtotime; | ||
|
|
||
| use const FILE_IGNORE_NEW_LINES; | ||
| use const FILE_SKIP_EMPTY_LINES; | ||
|
|
||
| #[AsCommand( | ||
| name: 'processed', | ||
| description: 'Get successfully processed messages', | ||
| )] | ||
| class GetProcessedMessagesCommand extends Command | ||
| { | ||
| protected static string $defaultName = 'processed'; | ||
|
|
||
| #[Inject()] | ||
| public function __construct() | ||
| { | ||
| parent::__construct(self::$defaultName); | ||
| } | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| $this->setDescription('Get successfully processed messages') | ||
| ->addOption('start', null, InputOption::VALUE_OPTIONAL, 'Start timestamp (Y-m-d H:i:s)') | ||
| ->addOption('end', null, InputOption::VALUE_OPTIONAL, 'End timestamp (Y-m-d H:i:s)') | ||
| ->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Limit in days'); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $start = $input->getOption('start'); | ||
| $end = $input->getOption('end'); | ||
| $limit = $input->getOption('limit'); | ||
|
|
||
| if (! $end) { | ||
| $end = date('Y-m-d H:i:s'); | ||
| } elseif (! preg_match('/\d{2}:\d{2}:\d{2}/', $end)) { | ||
| $end .= ' 23:59:59'; | ||
| } | ||
|
|
||
| if ($limit && is_numeric($limit) && ! $start) { | ||
| $start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end))); | ||
| } elseif ($start && ! preg_match('/\d{2}:\d{2}:\d{2}/', $start)) { | ||
| $start .= ' 00:00:00'; | ||
| } | ||
|
|
||
| $startTimestamp = $start ? strtotime($start) : null; | ||
| $endTimestamp = $end ? strtotime($end) : null; | ||
|
|
||
| $logPath = 'log/queue-log.log'; | ||
| $lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
|
|
||
| foreach ($lines as $line) { | ||
| $entry = json_decode($line, true); | ||
|
|
||
| if (! $entry || ! isset($entry['levelName'], $entry['timestamp'])) { | ||
| continue; | ||
| } | ||
|
|
||
| if (strtolower($entry['levelName']) !== 'info') { | ||
| continue; | ||
| } | ||
|
|
||
| $logTimestamp = strtotime($entry['timestamp']); | ||
| if ( | ||
| ($startTimestamp && $logTimestamp < $startTimestamp) || | ||
| ($endTimestamp && $logTimestamp > $endTimestamp) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| $output->writeln($line); | ||
| } | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add some comment what is this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are those ? seconds or miliseconds ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Milliseconds