Set the configured logging level even if the configured logging type is invalid

refs #8957
This commit is contained in:
Johannes Meyer 2015-04-13 17:09:49 +02:00
parent 46296dce95
commit 67ad575cf5
2 changed files with 51 additions and 28 deletions

View File

@ -506,12 +506,21 @@ abstract class ApplicationBootstrap
protected function setupLogger() protected function setupLogger()
{ {
if ($this->config->hasSection('logging')) { if ($this->config->hasSection('logging')) {
$loggingConfig = $this->config->getSection('logging');
try { try {
Logger::create($this->config->getSection('logging')); Logger::create($loggingConfig);
} catch (ConfigurationError $e) {
Logger::error($e);
try {
Logger::getInstance()->setLevel($loggingConfig->get('level', Logger::ERROR));
} catch (ConfigurationError $e) { } catch (ConfigurationError $e) {
Logger::error($e); Logger::error($e);
} }
} }
}
return $this; return $this;
} }

View File

@ -81,37 +81,51 @@ class Logger
throw new ConfigurationError('Required logging configuration directive \'log\' missing'); throw new ConfigurationError('Required logging configuration directive \'log\' missing');
} }
if (($level = $config->level) !== null) { $this->setLevel($config->get('level', static::ERROR));
if (strtolower($config->get('log', 'syslog')) !== 'none') {
$this->writer = $this->createWriter($config);
}
}
/**
* Set the logging level to use
*
* @param mixed $level
*
* @return $this
*
* @throws ConfigurationError In case the given level is invalid
*/
public function setLevel($level)
{
if (is_numeric($level)) { if (is_numeric($level)) {
$level = (int) $level; $level = (int) $level;
if (! isset(static::$levels[$level])) { if (! isset(static::$levels[$level])) {
throw new ConfigurationError( throw new ConfigurationError(
'Can\'t set logging level %d. Logging level is not defined. Use one of %s or one of the' 'Can\'t set logging level %d. Logging level is invalid. Use one of %s or one of the'
. ' Logger\'s constants.', . ' Logger\'s constants.',
$level, $level,
implode(', ', array_keys(static::$levels)) implode(', ', array_keys(static::$levels))
); );
} }
$this->level = $level; $this->level = $level;
} else { } else {
$level = strtoupper($level); $level = strtoupper($level);
$levels = array_flip(static::$levels); $levels = array_flip(static::$levels);
if (! isset($levels[$level])) { if (! isset($levels[$level])) {
throw new ConfigurationError( throw new ConfigurationError(
'Can\'t set logging level "%s". Logging level is not defined. Use one of %s.', 'Can\'t set logging level "%s". Logging level is invalid. Use one of %s.',
$level, $level,
implode(', ', array_keys($levels)) implode(', ', array_keys($levels))
); );
} }
$this->level = $levels[$level]; $this->level = $levels[$level];
} }
} else {
$this->level = static::ERROR;
}
if (strtolower($config->get('log', 'syslog')) !== 'none') { return $this;
$this->writer = $this->createWriter($config);
}
} }
/** /**