Move log level check to logger parent class

* remove duplicate code from child classes
This commit is contained in:
Morris Jobke 2015-04-30 12:06:52 +02:00
parent c9921ec127
commit 892b5ceeeb
4 changed files with 45 additions and 45 deletions

View File

@ -172,7 +172,13 @@ class Log implements ILogger {
// interpolate replacement values into the message and return
$message = strtr($message, $replace);
$logger = $this->logger;
call_user_func(array($logger, 'write'), $app, $message, $level);
$config = \OC::$server->getSystemConfig();
$minLevel = min($config->getValue('loglevel', \OC_Log::WARN), \OC_Log::ERROR);
if ($level >= $minLevel) {
$logger = $this->logger;
call_user_func(array($logger, 'write'), $app, $message, $level);
}
}
}

View File

@ -39,10 +39,7 @@ class OC_Log_Errorlog {
* @param int $level
*/
public static function write($app, $message, $level) {
$minLevel = min(OC_Config::getValue("loglevel", OC_Log::WARN), OC_Log::ERROR);
if ($level >= $minLevel) {
error_log('[owncloud]['.$app.'] '.$message);
}
error_log('[owncloud]['.$app.']['.$level.'] '.$message);
}
}

View File

@ -69,40 +69,40 @@ class OC_Log_Owncloud {
* @param int $level
*/
public static function write($app, $message, $level) {
$minLevel=min(OC_Config::getValue( "loglevel", OC_Log::WARN ), OC_Log::ERROR);
if($level>=$minLevel) {
// default to ISO8601
$format = OC_Config::getValue('logdateformat', 'c');
$logtimezone=OC_Config::getValue( "logtimezone", 'UTC' );
try {
$timezone = new DateTimeZone($logtimezone);
} catch (Exception $e) {
$timezone = new DateTimeZone('UTC');
}
$time = new DateTime(null, $timezone);
$request = \OC::$server->getRequest();
$reqId = $request->getId();
$remoteAddr = $request->getRemoteAddress();
// remove username/passwords from URLs before writing the to the log file
$time = $time->format($format);
if($minLevel == OC_Log::DEBUG) {
$url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '--';
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '--';
$entry = compact('reqId', 'remoteAddr', 'app', 'message', 'level', 'time', 'method', 'url');
}
else {
$entry = compact('reqId', 'remoteAddr', 'app', 'message', 'level', 'time');
}
$entry = json_encode($entry);
$handle = @fopen(self::$logFile, 'a');
@chmod(self::$logFile, 0640);
if ($handle) {
fwrite($handle, $entry."\n");
fclose($handle);
} else {
// Fall back to error_log
error_log($entry);
}
$config = \OC::$server->getSystemConfig();
// default to ISO8601
$format = $config->getValue('logdateformat', 'c');
$logtimezone = $config->getValue( "logtimezone", 'UTC' );
try {
$timezone = new DateTimeZone($logtimezone);
} catch (Exception $e) {
$timezone = new DateTimeZone('UTC');
}
$time = new DateTime(null, $timezone);
$request = \OC::$server->getRequest();
$reqId = $request->getId();
$remoteAddr = $request->getRemoteAddress();
// remove username/passwords from URLs before writing the to the log file
$time = $time->format($format);
$minLevel=min($config->getValue( "loglevel", OC_Log::WARN ), OC_Log::ERROR);
if($minLevel == OC_Log::DEBUG) {
$url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '--';
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '--';
$entry = compact('reqId', 'remoteAddr', 'app', 'message', 'level', 'time', 'method', 'url');
}
else {
$entry = compact('reqId', 'remoteAddr', 'app', 'message', 'level', 'time');
}
$entry = json_encode($entry);
$handle = @fopen(self::$logFile, 'a');
@chmod(self::$logFile, 0640);
if ($handle) {
fwrite($handle, $entry."\n");
fclose($handle);
} else {
// Fall back to error_log
error_log($entry);
}
}

View File

@ -47,10 +47,7 @@ class OC_Log_Syslog {
* @param int $level
*/
public static function write($app, $message, $level) {
$minLevel = min(OC_Config::getValue("loglevel", OC_Log::WARN), OC_Log::ERROR);
if ($level >= $minLevel) {
$syslog_level = self::$levels[$level];
syslog($syslog_level, '{'.$app.'} '.$message);
}
$syslog_level = self::$levels[$level];
syslog($syslog_level, '{'.$app.'} '.$message);
}
}