Skip to content

Commit

Permalink
Add monolog/monolog as a logger for oxid
Browse files Browse the repository at this point in the history
Related #601
  • Loading branch information
Florian Engelhardt authored and godefroy-le-hardi committed May 17, 2018
1 parent 001d619 commit 9fa8daf
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 36 deletions.
5 changes: 3 additions & 2 deletions composer.json
Expand Up @@ -20,13 +20,14 @@
"oxid-esales/oxideshop-db-views-generator": "^v1.1.1",
"oxid-esales/oxideshop-demodata-installer": "^v1.1.0",
"oxid-esales/oxideshop-composer-plugin": "^v2.0.0",
"oxid-esales/oxideshop-unified-namespace-generator": "^1.0.0",
"oxid-esales/oxideshop-unified-namespace-generator": "dev-master || ^1.0.0",
"oxid-esales/testing-library": "dev-master",
"oxid-esales/coding-standards": "^v3.0.2",
"oxid-esales/coding-standards-wrapper": "dev-master",
"incenteev/composer-parameter-handler": "~v2.0",
"oxid-esales/oxideshop-ide-helper": "^3.0",
"oxid-esales/azure-theme": "dev-b-1.4"
"oxid-esales/azure-theme": "dev-b-1.4",
"monolog/monolog": "^v1.23.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion source/Application/Controller/Admin/LoginController.php
Expand Up @@ -131,7 +131,7 @@ public function checklogin()
}

// success
\OxidEsales\Eshop\Core\Registry::getUtils()->logger("login successful");
\OxidEsales\Eshop\Core\Registry::getLogger()->info("login successful");

//execute onAdminLogin() event
$oEvenHandler = oxNew(\OxidEsales\Eshop\Core\SystemEventHandler::class);
Expand Down
3 changes: 1 addition & 2 deletions source/Application/Model/Order.php
Expand Up @@ -474,8 +474,7 @@ public function finalizeOrder(\OxidEsales\Eshop\Application\Model\Basket $oBaske
// check if this order is already stored
$sGetChallenge = \OxidEsales\Eshop\Core\Registry::getSession()->getVariable('sess_challenge');
if ($this->_checkOrderExist($sGetChallenge)) {
\OxidEsales\Eshop\Core\Registry::getUtils()->logger('BLOCKER');

\OxidEsales\Eshop\Core\Registry::getLogger()->debug('BLOCKER');
// we might use this later, this means that somebody clicked like mad on order button
return self::ORDER_STATE_ORDEREXISTS;
}
Expand Down
15 changes: 6 additions & 9 deletions source/Core/OnlineVatIdCheck.php
Expand Up @@ -87,19 +87,16 @@ public function validate(\OxidEsales\Eshop\Application\Model\CompanyVatIn $oVatI
* @param string $sErrFile error file
* @param int $iErrLine error line
*
* @return bool
* @return void
*/
public function catchWarning($iErrNo, $sErrStr, $sErrFile, $iErrLine)
{
// message to write to exception log
$sLogMessage = "Warning: $sErrStr in $sErrFile on line $iErrLine";

// fetching exception log file name
$oEx = oxNew(\OxidEsales\Eshop\Core\Exception\StandardException::class);
$sLogFileName = $oEx->getLogFileName();

// logs error message
return \OxidEsales\Eshop\Core\Registry::getUtils()->writeToLog($sLogMessage, $sLogFileName);
\OxidEsales\Eshop\Core\Registry::getLogger()->warning($sErrStr, [
'file' => $sErrFile,
'line' => $iErrLine,
'code' => $iErrNo
]);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions source/Core/Registry.php
Expand Up @@ -311,6 +311,20 @@ public static function getControllerClassNameResolver()
return self::getObject(\OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver::class);
}

/**
* Return instance of \Monolog\Logger or whatever getLogger() returns
*
* @static
* @return \Psr\Log\LoggerInterface
*/
public static function getLogger()
{
if (!self::instanceExists('logger')) {
self::set('logger', getLogger());
}
return self::get('logger');
}

/**
* Return all class instances, which are currently set in the registry
*
Expand Down
2 changes: 1 addition & 1 deletion source/Core/SystemEventHandler.php
Expand Up @@ -179,7 +179,7 @@ protected function validateOnline()
$this->sendShopInformation();
}
} catch (Exception $eException) {
\OxidEsales\Eshop\Core\Registry::getUtils()->logger("OLC-Error: " . $eException->getMessage());
\OxidEsales\Eshop\Core\Regsitry::getLogger()->error($eException->getMessage());
}
}

Expand Down
22 changes: 5 additions & 17 deletions source/bootstrap.php
Expand Up @@ -55,8 +55,11 @@ function () {
'Also double-check, if the class file for this very class was created.';
}
/** report the error */
$logMessage = "[uncaught error] [type $errorType] [file {$error['file']}] [line {$error['line']}] [code ] [message {$errorMessage}]";
writeToLog($logMessage);
\OxidEsales\Eshop\Core\Registry::getLogger()->alert($errorMessage, [
'file' => $error['file'],
'line' => $error['line'],
'type' => $errorType
]);

$bootstrapConfigFileReader = new \BootstrapConfigFileReader();
if (!$bootstrapConfigFileReader->isDebugMode()) {
Expand Down Expand Up @@ -215,18 +218,3 @@ function oxTriggerOfflinePageDisplay()
};
}
}

/**
* @param string $message
*/
function writeToLog($message)
{
$time = microtime(true);
$micro = sprintf("%06d", ($time - floor($time)) * 1000000);
$date = new \DateTime(date('Y-m-d H:i:s.' . $micro, $time));
$timestamp = $date->format('d M H:i:s.u Y');

$message = "[$timestamp] " . $message . PHP_EOL;

file_put_contents(OX_LOG_FILE, $message, FILE_APPEND);
}
19 changes: 19 additions & 0 deletions source/overridablefunctions.php
Expand Up @@ -194,3 +194,22 @@ function getRequestUrl($sParams = '', $blReturnUrl = false)
return Registry::get(Request::class)->getRequestUrl($sParams, $blReturnUrl);
}
}

if (!function_exists('getLogger')) {
/**
* Returns the Logger class to use
*
* @return \Monolog\Logger
*/
function getLogger()
{
$logger = new Monolog\Logger('OxidEsales');
$logger->pushHandler(
new Monolog\Handler\StreamHandler(
\OxidEsales\Eshop\Core\Registry::getConfig()->getLogsDir() . 'logs.txt'
),
Monolog\Logger::WARNING
);
return $logger;
}
}
17 changes: 13 additions & 4 deletions tests/Unit/Core/OnlineVatIdCheckTest.php
Expand Up @@ -26,12 +26,21 @@ protected function setUp()
*/
public function testCatchWarning()
{
oxTestModules::addFunction('oxUtils', 'writeToLog', '{ return $aA; }');
$oLogger = \OxidEsales\Eshop\Core\Registry::getLogger();
$oLogger->setHandlers([]); // reset handlers
$oHandler = new \Monolog\Handler\TestHandler();
$oLogger->pushHandler($oHandler);

$oOnlineVatIdCheck = oxNew('oxOnlineVatIdCheck');
$aResult = $oOnlineVatIdCheck->catchWarning(1, 1, 1, 1);

$this->assertEquals(array("Warning: 1 in 1 on line 1", "EXCEPTION_LOG.txt"), $aResult);
$oOnlineVatIdCheck->catchWarning(1, 2, 3, 4);

$aRecords = $oHandler->getRecords();
$this->assertEquals(2, $aRecords[0]['message']);
$this->assertEquals([
'code' => 1,
'file' => 3,
'line' => 4
], $aRecords[0]['context']);
}

/**
Expand Down

0 comments on commit 9fa8daf

Please sign in to comment.