/** * Logs with an arbitrary level. * * Also adds few items to the context array. * * @param mixed $level * @param string $message * @param array $context * @return null */ public function log($level, $message, array $context = array()) { $context = array_merge(array('_name' => $this->name, '_channel' => $this->name, '_uuid' => $this->uuid, '_tags' => isset($context['_tags']) ? is_array($context['_tags']) ? $context['_tags'] : explode(',', trim((string) $context['_tags'])) : array(), '_level' => $level, '_timestamp' => Timer::getMicroTime(), '_date' => new \DateTime()), $context); $message = StringUtils::interpolate((string) $message, $context); foreach ($this->writers as $writer) { $writer->log($level, $message, $context); } }
/** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * @return null */ public function log($level, $message, array $context = array()) { if (!$this->isEnabled()) { return false; } $tags = array(); if (isset($context['_tags'])) { $tags = is_array($context['_tags']) ? $context['_tags'] : explode(',', trim((string) $context['_tags'])); unset($context['_tags']); } $timer = null; if (isset($context['_timer']) && $context['_timer'] instanceof Timer) { $timer = $context['_timer']; unset($context['_timer']); } $this->_log[] = array('message' => StringUtils::parseVariables((string) $message, $context), 'context' => $context, '_tags' => $tags, '_timestamp' => Timer::getMicroTime(), '_time' => new \DateTime(), '_level' => $level, '_timer' => $timer); }
/** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * @return null */ public function log($level, $message, array $context = array()) { // add color to all injected variables $message = preg_replace_callback('/{([\\w\\d_\\.]+)}/is', function ($matches) { $var = $matches[1]; return '<info>{' . $var . '}</info>'; }, $message); $message = StringUtils::interpolate($message, $context); $alert = ''; switch ($level) { case LogLevel::ALERT: case LogLevel::CRITICAL: case LogLevel::EMERGENCY: case LogLevel::ERROR: $alert = '<error> ' . $level . ' </error> '; break; case LogLevel::NOTICE: case LogLevel::WARNING: $alert = '<fg=yellow;options=bold>' . $level . '</fg=yellow;options=bold> '; break; } $this->output->writeln($alert . $message . ' [@mem: ' . StringUtils::bytesToString(Timer::getCurrentMemory()) . ']'); }
/** * Removes items from the collection based on the given criteria. * * @param string $collection Name of the collection from which to remove items. * @param CriteriaExpression $criteria [optional] Lookup criteria. */ public function remove($collection, CriteriaExpression $criteria = null) { $this->connect(); $timer = new Timer(); $criteria = $this->criteriaParser->parse($criteria); try { $info = $this->database->{$collection}->deleteMany($criteria); } catch (MongoException $e) { $this->log('remove', $collection, $criteria, [], 'error'); throw new StoreQueryErrorException('MongoDB: ' . $e->getMessage(), $e->getCode(), $e); } // log this query $this->log('remove', $collection, $criteria, ['time' => $timer->stop(), 'affected' => is_array($info) ? $info['n'] : 'unknown']); }
public function testGetCurrentMemoryPeak() { $this->assertEquals(memory_get_peak_usage(true), Timer::getCurrentMemoryPeak()); $this->assertInternalType('int', Timer::getCurrentMemoryPeak()); $this->assertTrue(Timer::getCurrentMemoryPeak() > 0); }
/** * Removes items from the collection based on the given criteria. * * @param string $collection Name of the collection from which to remove items. * @param CriteriaExpression $criteria [optional] Lookup criteria. */ public function remove($collection, CriteriaExpression $criteria = null) { $timer = new Timer(); $this->ensureCollection($collection); // remove the matching items $affected = 0; foreach ($this->collections[$collection] as $i => $item) { if ($this->matcher->matches($item, $criteria)) { unset($this->collections[$collection][$i]); $affected++; } } // log this query $this->log('remove', $collection, $criteria ? $criteria->getRaw() : [], ['time' => $timer->stop(), 'affected' => $affected]); }
/** * Removes items from the collection based on the given criteria. * * @param string $collection Name of the collection from which to remove items. * @param CriteriaExpression $criteria [optional] Lookup criteria. */ public function remove($collection, CriteriaExpression $criteria = null) { $this->connect(); $timer = new Timer(); $queryBuilder = $this->connection->createQueryBuilder()->delete($collection); $this->criteriaParser->parse($queryBuilder, $criteria); $sql = $queryBuilder->getSQL(); $sqlParams = $queryBuilder->getParameters(); // execute the query try { $queryBuilder->execute(); } catch (\Exception $e) { $this->log($sql, $sqlParams, [], 'error'); throw new StoreQueryErrorException('DoctrineDBAL: ' . $e->getMessage(), $e->getCode(), $e); } // log this query $this->log($sql, $sqlParams, ['time' => $timer->stop()]); }
/** * Runs the application in context of the given mode and using the given runner function. * * The last argument is a callback that will be invoked by this method and it should contain * execution logic specific to the given mode. * * Returns whatever the runner returns (which should be a result of running the application). * * @param AbstractApplication $application Application to be ran. * @param int $mode Mode in which the application should be ran. * One of the `Framework::MODE_*` constants. * @param callable $runner Closure that is responsible for actually running * the application in appropriate mode. * @return mixed */ protected function doRunApplication(AbstractApplication $application, $mode, $runner) { $timer = new Timer(); $container = $application->getContainer(); // container can now know in what mode its running $container->setParameter('mode', $mode); // run modules and the app foreach ($application->getModules() as $module) { $module->run(); } $application->run(); // run using the passed runner $result = call_user_func($runner); // log benchmark data $time = $timer->stop(); $memory = $timer->getMemoryUsage(); $container->get('splot.logger')->debug('Application run phase in mode "{mode}" finished in {time} ms and used {memory} memory.', array('mode' => self::modeName($mode), 'env' => $container->getParameter('env'), 'debug' => $container->getParameter('debug'), 'time' => $time, 'memory' => StringUtils::bytesToString($memory), '@stat' => 'splot.run.' . self::modeName($mode), '@time' => $time, '@memory' => $memory)); return $result; }