Example #1
0
 /**
  * {@inheritdoc}
  */
 public function report()
 {
     $this->debugger->logger()->error($this->getMessage());
     if (!$this->config['reporting']['enabled']) {
         //No need to record anything
         return;
     }
     //Snapshot filename
     $filename = \Spiral\interpolate($this->config['reporting']['filename'], ['date' => date($this->config['reporting']['dateFormat'], time()), 'exception' => $this->getName()]);
     //Writing to hard drive
     $this->files->write($this->config['reporting']['directory'] . '/' . $filename, $this->render(), FilesInterface::RUNTIME, true);
     $snapshots = $this->files->getFiles($this->config['reporting']['directory']);
     if (count($snapshots) > $this->config['reporting']['maxSnapshots']) {
         $oldestSnapshot = '';
         $oldestTimestamp = PHP_INT_MAX;
         foreach ($snapshots as $snapshot) {
             $snapshotTimestamp = $this->files->time($snapshot);
             if ($snapshotTimestamp < $oldestTimestamp) {
                 $oldestTimestamp = $snapshotTimestamp;
                 $oldestSnapshot = $snapshot;
             }
         }
         $this->files->delete($oldestSnapshot);
     }
 }
Example #2
0
 /**
  * Dump value content into specified output.
  **
  *
  * @param mixed $value
  * @param int   $output
  * @return null|string
  */
 public function dump($value, $output = self::OUTPUT_ECHO)
 {
     if (php_sapi_name() === 'cli' && $output != self::OUTPUT_LOG) {
         print_r($value);
         if (is_scalar($value)) {
             echo "\n";
         }
         return null;
     }
     $result = \Spiral\interpolate($this->options['container'], ['dump' => $this->dumpValue($value, '', 0)]);
     switch ($output) {
         case self::OUTPUT_ECHO:
             echo $result;
             break;
         case self::OUTPUT_RETURN:
             return $result;
         case self::OUTPUT_LOG:
             if (!empty($this->debugger)) {
                 $this->debugger->logger()->debug(print_r($value, true));
             }
             break;
         case self::OUTPUT_LOG_NICE:
             if (!empty($this->debugger)) {
                 $this->debugger->logger()->debug($this->dump($value, self::OUTPUT_RETURN));
             }
             break;
     }
     return null;
 }
Example #3
0
 /**
  * @param Debugger           $debugger
  * @param ORM                $orm
  * @param ContainerInterface $container
  * @param ClassLocator       $locator
  */
 public function perform(Debugger $debugger, ORM $orm, ContainerInterface $container, ClassLocator $locator)
 {
     //We don't really need location errors here
     $locator->setLogger(new NullLogger());
     $benchmark = $debugger->benchmark($this, 'update');
     $builder = $orm->schemaBuilder($locator);
     //To make builder available for other commands (in sequence)
     $container->bind(get_class($builder), $builder);
     if (!$this->option('sync')) {
         $this->writeln("<comment>Silent mode on, no databases to be altered.</comment>");
     }
     $orm->updateSchema($builder, $this->option('sync'));
     $elapsed = number_format($debugger->benchmark($this, $benchmark), 3);
     $countModels = count($builder->getRecords());
     $this->write("<info>ORM Schema has been updated: <comment>{$elapsed} s</comment>");
     $this->writeln(", found records: <comment>{$countModels}</comment></info>");
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function start(ConsoleHandler $handler = null)
 {
     //Some console commands utilizes benchmarking, let's help them
     $this->container->bind(BenchmarkerInterface::class, Debugger::class);
     $output = new ConsoleOutput();
     $this->debugger->shareHandler($this->consoleHandler($output));
     $scope = $this->container->replace(LogsInterface::class, $this->debugger);
     try {
         $this->application()->run(null, $output);
     } finally {
         $this->container->restore($scope);
     }
 }
Example #5
0
 /**
  * Benchmarks will be returned in normalized form.
  *
  * @param float $lastEnding Last recorded time.
  * @return array|null
  */
 public function getBenchmarks(&$lastEnding = null)
 {
     $result = [];
     foreach ($this->debugger->getBenchmarks() as $record => $benchmark) {
         if (!isset($benchmark[self::BENCHMARK_ENDED])) {
             //Closing continues record
             $benchmark[self::BENCHMARK_ENDED] = microtime(true);
         }
         $elapsed = $benchmark[self::BENCHMARK_ENDED] - $benchmark[self::BENCHMARK_STARTED];
         $result[$record] = ['caller' => $benchmark[self::BENCHMARK_CALLER], 'record' => $benchmark[self::BENCHMARK_RECORD], 'context' => $benchmark[self::BENCHMARK_CONTEXT], 'started' => $benchmark[self::BENCHMARK_STARTED], 'ended' => $lastEnding = $benchmark[self::BENCHMARK_ENDED], 'elapsed' => $elapsed];
     }
     return $result;
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function log($level, $message, array $context = [])
 {
     if (!empty($this->debugger)) {
         //Global logging
         $this->debugger->logGlobal($this->name, $level, $message, $context);
     }
     if (empty($this->handlers)) {
         return $this;
     }
     $payload = [self::MESSAGE_CHANNEL => $this->name, self::MESSAGE_TIMESTAMP => microtime(true), self::MESSAGE_LEVEL => $level, self::MESSAGE_BODY => \Spiral\interpolate($message, $context), self::MESSAGE_CONTEXT => $context];
     //We don't need this information for log handlers
     unset($payload[self::MESSAGE_CHANNEL], $payload[self::MESSAGE_TIMESTAMP]);
     if (isset($this->handlers[$level])) {
         call_user_func_array($this->handlers[$level], $payload);
     } elseif (isset($this->handlers[self::ALL])) {
         call_user_func_array($this->handlers[self::ALL], $payload);
     }
     return $this;
 }
 /**
  * @param Debugger $debugger
  */
 public function __construct(Debugger $debugger)
 {
     parent::__construct(static::class, $debugger->logHandlers(static::class));
 }