host = self::DEFAULT_MONGO_URL_PREFIX . self::DEFAULT_MONGO_HOST; $this->port = self::DEFAULT_MONGO_PORT; $this->databaseName = self::DEFAULT_DB_NAME; $this->collectionName = self::DEFAULT_COLLECTION_NAME; } /** * Setup db connection. * Based on defined options, this method connects to the database and * creates a {@link $collection}. * * @throws Exception if the attempt to connect to the requested database fails. */ public function activateOptions() { try { $this->connection = new Mongo(sprintf('%s:%d', $this->host, $this->port)); $db = $this->connection->selectDB($this->databaseName); if ($this->userName !== null && $this->password !== null) { $authResult = $db->authenticate($this->userName, $this->password); if ($authResult['ok'] == floatval(0)) { throw new Exception($authResult['errmsg'], $authResult['ok']); } } $this->collection = $db->selectCollection($this->collectionName); } catch (Exception $ex) { $this->canAppend = false; throw new LoggerException($ex); } $this->canAppend = true; } /** * Appends a new event to the mongo database. * * @throws LoggerException If the pattern conversion or the INSERT statement fails. */ public function append(LoggerLoggingEvent $event) { if ($this->canAppend == true && $this->collection != null) { $document = $this->format($event); $this->collection->insert($document); } } /** * Converts the logging event into an array which can be logged to mongodb. * * @param LoggerLoggingEvent $event * @return array The array representation of the logging event. */ protected function format(LoggerLoggingEvent $event) { $timestampSec = (int) $event->getTimestamp(); $timestampUsec = (int) (($event->getTimestamp() - $timestampSec) * 1000000); $document = array( 'timestamp' => new MongoDate($timestampSec, $timestampUsec), 'level' => $event->getLevel()->toString(), 'thread' => (int) $event->getThreadName(), 'message' => $event->getMessage(), 'loggerName' => $event->getLoggerName() ); $locationInfo = $event->getLocationInformation(); if ($locationInfo != null) { $document['fileName'] = $locationInfo->getFileName(); $document['method'] = $locationInfo->getMethodName(); $document['lineNumber'] = ($locationInfo->getLineNumber() == 'NA') ? 'NA' : (int) $locationInfo->getLineNumber(); $document['className'] = $locationInfo->getClassName(); } $throwableInfo = $event->getThrowableInformation(); if ($throwableInfo != null) { $document['exception'] = $this->formatThrowable($throwableInfo->getThrowable()); } return $document; } /** * Converts an Exception into an array which can be logged to mongodb. * * Supports innner exceptions (PHP >= 5.3) * * @param Exception $ex * @return array */ protected function formatThrowable(Exception $ex) { $array = array( 'message' => $ex->getMessage(), 'code' => $ex->getCode(), 'stackTrace' => $ex->getTraceAsString(), ); if (method_exists($ex, 'getPrevious') && $ex->getPrevious() !== null) { $array['innerException'] = $this->formatThrowable($ex->getPrevious()); } return $array; } /** * Closes the connection to the logging database */ public function close() { if($this->closed != true) { $this->collection = null; if ($this->connection !== null) { $this->connection->close(); $this->connection = null; } $this->closed = true; } } public function __destruct() { $this->close(); } /** Sets the value of {@link $host} parameter. */ public function setHost($host) { if (!preg_match('/^mongodb\:\/\//', $host)) { $host = self::DEFAULT_MONGO_URL_PREFIX . $host; } $this->host = $host; } /** Returns the value of {@link $host} parameter. */ public function getHost() { return $this->host; } /** Sets the value of {@link $port} parameter. */ public function setPort($port) { $this->port = $port; } /** Returns the value of {@link $port} parameter. */ public function getPort() { return $this->port; } /** Sets the value of {@link $databaseName} parameter. */ public function setDatabaseName($databaseName) { $this->databaseName = $databaseName; } /** Returns the value of {@link $databaseName} parameter. */ public function getDatabaseName() { return $this->databaseName; } /** Sets the value of {@link $collectionName} parameter. */ public function setCollectionName($collectionName) { $this->collectionName = $collectionName; } /** Returns the value of {@link $collectionName} parameter. */ public function getCollectionName() { return $this->collectionName; } /** Sets the value of {@link $userName} parameter. */ public function setUserName($userName) { $this->userName = $userName; } /** Returns the value of {@link $userName} parameter. */ public function getUserName() { return $this->userName; } /** Sets the value of {@link $password} parameter. */ public function setPassword($password) { $this->password = $password; } /** Returns the value of {@link $password} parameter. */ public function getPassword() { return $this->password; } /** * Returns the mongodb connection. * @return Mongo */ public function getConnection() { return $this->connection; } /** * Returns the active mongodb collection. * @return MongoCollection */ public function getCollection() { return $this->collection; } } ?>