public function testGetRecordContext() { $user = CMTest_TH::createUser(); $httpRequest = CM_Http_Request_Abstract::factory('post', '/foo?bar=1&baz=quux&viewInfoList=fooBar', ['bar' => 'baz', 'host' => 'foo.bar:8080'], ['http_referer' => 'http://bar/baz', 'http_user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_10)', 'foo' => 'quux'], '{"foo" : "bar", "quux" : "baz"}'); $clientId = $httpRequest->getClientId(); $computerInfo = new CM_Log_Context_ComputerInfo('www.example.com', 'v7.0.1'); $exception = new CM_Exception_Invalid('Bad', null, ['foo' => 'bar']); $context = new CM_Log_Context(); $context->setExtra(['bar' => 'baz', 'baz' => 'quux']); $context->setUser($user); $context->setException($exception); $context->setComputerInfo($computerInfo); $context->setHttpRequest($httpRequest); $contextFormatter = new CM_Log_ContextFormatter_Cargomedia('appName'); $formattedContext = $contextFormatter->formatContext($context); $this->assertSame('www.example.com', $formattedContext['computerInfo']['fqdn']); $this->assertSame('v7.0.1', $formattedContext['computerInfo']['phpVersion']); $this->assertSame('/foo?bar=1&baz=quux&viewInfoList=fooBar', $formattedContext['httpRequest']['uri']); $this->assertSame(join("\n", ['{', ' "bar": "1",', ' "baz": "quux",', ' "foo": "bar",', ' "quux": "baz"', '}']), $formattedContext['httpRequest']['query']); $this->assertSame('POST', $formattedContext['httpRequest']['method']); $this->assertSame('http://bar/baz', $formattedContext['httpRequest']['referer']); $this->assertSame('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_10)', $formattedContext['httpRequest']['useragent']); $this->assertSame('foo.bar', $formattedContext['httpRequest']['hostname']); $this->assertSame(['id' => $user->getId(), 'displayName' => 'user' . $user->getId()], $formattedContext['appName']['user']); $this->assertSame($clientId, $formattedContext['appName']['client']['id']); $this->assertSame('baz', $formattedContext['appName']['bar']); $this->assertSame('quux', $formattedContext['appName']['baz']); $this->assertSame('CM_Exception_Invalid', $formattedContext['exception']['type']); $this->assertSame('Bad', $formattedContext['exception']['message']); $this->assertArrayHasKey('stack', $formattedContext['exception']); $this->assertInternalType('string', $formattedContext['exception']['stack']); $this->assertSame(['foo' => "'bar'"], $formattedContext['exception']['metaInfo']); $this->assertRegExp('/library\\/CM\\/Log\\/ContextFormatter\\/CargomediaTest\\.php\\(\\d+\\)/', $formattedContext['exception']['stack']); }
public function testAddGet() { $client = $this->getServiceManager()->getMongoDb(); $encoder = new CM_Log_Encoder_MongoDb(); $handler = new CM_Log_Handler_MongoDb($client, $encoder, CM_Paging_Log::COLLECTION_NAME); $user = CMTest_TH::createUser(); $context = new CM_Log_Context(); $context->setExtra(['bar' => 'quux']); $context->setUser($user); $record1 = new CM_Log_Record(CM_Log_Logger::DEBUG, 'foo', $context); $record2 = new CM_Log_Record(CM_Log_Logger::INFO, 'baz', new CM_Log_Context()); $handler->handleRecord($record1); $handler->handleRecord($record2); $paging = new CM_Paging_Log([CM_Log_Logger::DEBUG, CM_Log_Logger::INFO]); $items = $paging->getItems(); $this->assertSame(2, count($items)); $this->assertSame('baz', $items[0]['message']); $this->assertSame(CM_Log_Logger::INFO, $items[0]['level']); $this->assertSame('foo', $items[1]['message']); $this->assertSame(CM_Log_Logger::DEBUG, $items[1]['level']); $this->assertSame($user->getDisplayName(), $items[1]['context']['user']['name']); $this->assertSame(['bar' => 'quux', 'type' => CM_Log_Handler_MongoDb::DEFAULT_TYPE], $items[1]['context']['extra']); $age = 86400; CMTest_TH::timeForward($age); CMTest_TH::timeForward($age); $record3 = new CM_Log_Record(CM_Log_Logger::CRITICAL, 'bar', new CM_Log_Context()); $handler->handleRecord($record3); $paging2 = new CM_Paging_Log([CM_Log_Logger::CRITICAL], null, false, $age + 1); $items = $paging2->getItems(); $this->assertSame(1, count($items)); $this->assertSame('bar', $items[0]['message']); $this->assertSame(CM_Log_Logger::CRITICAL, $items[0]['level']); }
public function testWriting() { $collection = 'cm_event_log'; $level = CM_Log_Logger::DEBUG; $message = 'foo'; $ttl = 30; $user = CMTest_TH::createUser(); $httpRequest = CM_Http_Request_Abstract::factory('post', '/foo?bar=1&baz=quux', ['bar' => 'baz'], ['foo' => 'quux'], '{"bar":"2", "quux":"baz"}'); $clientId = $httpRequest->getClientId(); $computerInfo = new CM_Log_Context_ComputerInfo('www.example.com', 'v7.0.1'); $mongoClient = $this->getServiceManager()->getMongoDb(); $this->assertSame(0, $mongoClient->count($collection)); $mongoClient->createIndex($collection, ['expireAt' => 1], ['expireAfterSeconds' => 0]); $recordContext = new CM_Log_Context(); $recordContext->setExtra(['bar' => ['baz' => 'quux']]); $recordContext->setUser($user); $recordContext->setHttpRequest($httpRequest); $recordContext->setComputerInfo($computerInfo); $record = new CM_Log_Record($level, $message, $recordContext); $encoder = $this->mockObject(CM_Log_Encoder_MongoDb::class); /** @var CM_Log_Encoder_MongoDb $encoder */ $handler = new CM_Log_Handler_MongoDb($mongoClient, $encoder, $collection, $ttl, ['w' => 0], $level); $this->callProtectedMethod($handler, '_writeRecord', [$record]); $this->assertSame(1, $mongoClient->count($collection)); $savedRecord = $mongoClient->findOne($collection); $this->assertSame($level, $savedRecord['level']); $this->assertSame($message, $savedRecord['message']); /** @var MongoDate $createdAt */ $createdAt = $savedRecord['createdAt']; /** @var MongoDate $expireAt */ $expireAt = $savedRecord['expireAt']; $this->assertInstanceOf('MongoDate', $createdAt); $this->assertInstanceOf('MongoDate', $expireAt); $this->assertSame($ttl, $expireAt->sec - $createdAt->sec); $context = $savedRecord['context']; $this->assertSame(['id' => $user->getId(), 'name' => $user->getDisplayName()], $context['user']); $this->assertSame('POST', $context['httpRequest']['method']); $this->assertSame('/foo?bar=1&baz=quux', $context['httpRequest']['uri']); $this->assertSame(['bar' => '2', 'baz' => 'quux', 'quux' => 'baz'], $context['httpRequest']['query']); $this->assertSame(['bar' => 'baz'], $context['httpRequest']['headers']); $this->assertSame(['foo' => 'quux'], $context['httpRequest']['server']); $this->assertSame($clientId, $context['httpRequest']['clientId']); $this->assertSame('www.example.com', $context['computerInfo']['fqdn']); $this->assertSame('v7.0.1', $context['computerInfo']['phpVersion']); $this->assertSame(['bar' => ['baz' => 'quux'], 'type' => CM_Log_Handler_MongoDb::DEFAULT_TYPE], $context['extra']); $this->assertSame('{"bar":"2", "quux":"baz"}', $context['httpRequest']['body']); }
/** * @param Closure $regularCode * @param Closure $errorCode * @return mixed * @throws CM_Exception */ protected function _runWithCatching(Closure $regularCode, Closure $errorCode) { try { return $regularCode(); } catch (CM_Exception $ex) { $config = self::_getConfig(); $exceptionsToCatch = $config->exceptionsToCatch; $catchPublicExceptions = !empty($config->catchPublicExceptions); $errorOptions = \Functional\first($exceptionsToCatch, function ($options, $exceptionClass) use($ex) { return is_a($ex, $exceptionClass); }); $catchException = null !== $errorOptions; if ($catchException && isset($errorOptions['log']) && true === $errorOptions['log']) { $logLevel = isset($errorOptions['level']) ? $errorOptions['level'] : null; if (null === $logLevel) { $logLevel = CM_Log_Logger::exceptionToLevel($ex); } $context = new CM_Log_Context(); $context->setUser($this->getViewer()); $context->setException($ex); $this->getServiceManager()->getLogger()->addMessage('Response processing error', $logLevel, $context); } if (!$catchException && ($catchPublicExceptions && $ex->isPublic())) { $errorOptions = []; $catchException = true; } if ($catchException) { return $errorCode($ex, $errorOptions); } throw $ex; } }