/** * Exists the table at the db? * * @return void */ public function optimize() { static $optimized = false; // Only one optimize run per request if ($optimized) { return; } $optimized = true; $maxRows = \DMK\Mklog\Factory::getConfigUtility()->getMaxLogs(); // no cleanup if (empty($maxRows)) { return; } // fetch current rows $numRows = $this->search(array(), array('count' => true)); // there are log entries to delete if ($numRows > $maxRows) { // fetch the execution date from the latest log entry $collection = $this->search(array(), array('what' => 'run_id', 'offset' => $maxRows, 'limit' => 1, 'orderby' => array('DEVLOGENTRY.run_id' => 'ASC'))); if ($collection->isEmpty()) { return; } $lastExec = reset($collection->first()); // nothing found to delete!? if (empty($lastExec)) { return; } // delete all entries, older than the last exeution date! $this->getConnection()->doDelete($this->getEmptyModel()->getTableName(), 'run_id < ' . $lastExec); } }
/** * Writes the log record * * @param \TYPO3\CMS\Core\Log\LogRecord $record Log record * * @return WriterInterface $this */ public function writeLog(\TYPO3\CMS\Core\Log\LogRecord $record) { $config = \DMK\Mklog\Factory::getConfigUtility(); // check min log level if (!$config->getGelfEnable() || !$config->getGelfCredentials() || $record->getLevel() > $config->getGelfMinLogLevel()) { return $this; } $options = \tx_rnbase::makeInstance('Tx_Rnbase_Domain_Model_Data', array('credentials' => $config->getGelfCredentials())); $transport = $this->getTransport(); $transport->initialize($options); $message = $this->createDevlogEntry($record->getMessage(), $record->getComponent(), $record->getLevel(), $record->getData()); try { $transport->publish($message); } catch (\Exception $e) { // what todo on transport exception? // usualy we have a emergency and a other logger (file or mail) shold take over return $this; } $transport->shutdown(); return $this; }
/** * Stores a devlog entry * * @param string $message * @param string $extension * @param int $severity * @param mixed $extraData * * @return \DMK\Mklog\Domain\Model\DevlogEntryModel */ protected function createDevlogEntry($message, $extension, $severity, $extraData) { $config = \DMK\Mklog\Factory::getConfigUtility(); $repo = $this->getDevlogEntryRepository(); /* @var $entry \DMK\Mklog\Domain\Model\DevlogEntryModel */ $entry = $repo->createNewModel(); $entry->setCrdate(time()); $entry->setRunId($config->getCurrentRunId()); $entry->setHost($entry->getHost()); $entry->setMessage((string) $message); $entry->setExtKey((string) $extension); $entry->setSeverity((int) $severity); $entry->setPid(0); if (TYPO3_MODE === 'FE' && isset($GLOBALS['TSFE'])) { $entry->setPid((int) $GLOBALS['TSFE']->id); } $entry->setCruserId(0); if (!empty($GLOBALS['BE_USER']->user['uid'])) { $entry->setCruserId((int) $GLOBALS['BE_USER']->user['uid']); } $entry->setExtraData($this->progressExtraData($extraData)); return $entry; }
/** * Is logging enabled? * * @return bool */ protected function isLoggingEnabled() { // skip logging, if there is no db. if (empty($GLOBALS['TYPO3_DB']) || !is_object($GLOBALS['TYPO3_DB'])) { return false; } // skip if logging is disabled if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['mklog']['nolog'] || $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['devlog']['nolog']) { return false; } // now check some cachable options $storage = \DMK\Mklog\Factory::getStorage(); if ($storage->hasLoggingActive()) { return $storage->getLoggingActive(); } $repo = \DMK\Mklog\Factory::getDevlogEntryRepository(); $config = \DMK\Mklog\Factory::getConfigUtility(); $storage->setLoggingActive(true); if (!$config->getEnableDevLog()) { $storage->setLoggingActive(false); } elseif (!$repo->isTableAvailable()) { // check for exsisting db table $storage->setLoggingActive(false); } return $storage->getLoggingActive(); }
/** * Sends the devlog content per mail * * @param string $content * * @return void */ protected function sendMail($content) { /* @var $mail \tx_rnbase_util_Mail */ $mail = \tx_rnbase::makeInstance('tx_rnbase_util_Mail'); $mail->setSubject('DevLog WatchDog on site ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']); $mail->setFrom(\DMK\Mklog\Factory::getConfigUtility()->getGlobalMailFrom()); $mail->setTo($this->getOptions()->getCredentials()); $mail->setTextPart($content); $mail->send(); }
/** * Tears down the fixture, for example, close a network connection. * This method is called after a test is executed. * * @return void */ protected function tearDown() { // reset extconf cache \DMK\Mklog\Factory::getConfigUtility()->getExtConf()->setProperty(array()); }
/** * Test the storeLog method * * @return void * * @group unit * @test */ public function testStoreLog() { $msg = 'msg'; $extKey = 'mklog'; $severity = 7; $extraData = array('foo' => 1, 'bar' => array('baz')); $logger = $this->getDevlogLoggerMock(array('isLoggingEnabled')); $logger->expects(self::any())->method('isLoggingEnabled')->will(self::returnValue(true)); $that = $this; // workaround for php 5.3 $repo = $this->callInaccessibleMethod($logger, 'getDevlogEntryRepository'); $connection = $this->callInaccessibleMethod($repo, 'getConnection'); $connection->expects(self::once())->method('doInsert')->with($this->callback(function ($tablename) { return $tablename === 'tx_mklog_devlog_entry'; }), $this->callback(function ($data) use($that, $msg, $extKey, $severity, $extraData) { $that->assertSame(\DMK\Mklog\Factory::getConfigUtility()->getCurrentRunId(), $data['run_id']); $that->assertGreaterThan(time() - 60, $data['crdate']); $that->assertSame(0, $data['pid']); $that->assertSame($msg, $data['message']); $that->assertSame($extKey, $data['ext_key']); $that->assertSame($severity, $data['severity']); // how to check? on cli it is 0, on be runs the current user id! $that->assertArrayHasKey('cruser_id', $data); $that->assertArrayHasKey('extra_data', $data); $that->assertTrue(is_string($data['extra_data'])); $logData = json_decode($data['extra_data'], true); $that->assertSame(1, $logData['foo']); $that->assertSame(array('baz'), $logData['bar']); $that->assertArrayHasKey('__feuser', $logData); $that->assertArrayHasKey('__beuser', $logData); $that->assertArrayHasKey('__trace', $logData); return true; })); $this->callInaccessibleMethod(array($logger, 'storeLog'), array($msg, $extKey, $severity, $extraData)); }