FixTrackLog.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\command;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use support\Redis;
  9. use App\services\TrackService;
  10. class FixTrackLog extends Command
  11. {
  12. protected static $defaultName = 'fix_track_log';
  13. protected static $defaultDescription = '根据 /app/runtime/logs/tarck/ 下的文件修复日志';
  14. /**
  15. * @return void
  16. */
  17. protected function configure()
  18. {
  19. //$this->addArgument('file', InputArgument::OPTIONAL, '文件路径及其名称');
  20. $this->addOption('file', 'file', InputOption::VALUE_REQUIRED, '要解析的文件名称');
  21. }
  22. /**
  23. * @param InputInterface $input
  24. * @param OutputInterface $output
  25. * @return int
  26. */
  27. protected function execute(InputInterface $input, OutputInterface $output)
  28. {
  29. $file = $input->getOption('file');
  30. $fileData = function($file) {
  31. $content = fopen($file, 'r');
  32. //if (!$file) die('file does not exist or cannot be opened');
  33. while (($line = fgets($content)) !== false) {
  34. yield $line;
  35. }
  36. fclose($content);
  37. };
  38. foreach ($fileData($file) as $line) {
  39. $line = explode(": ", $line);
  40. $paramsc = $line[count($line)-1];
  41. $params = json_decode($paramsc,true);
  42. if (!is_array($params)) {
  43. $output->writeln('不是json:'.$params);
  44. }
  45. TrackService::push($params,1);
  46. }
  47. return 1;
  48. }
  49. }