Merge LoggingForm into GeneralForm

fixes #6933
This commit is contained in:
Johannes Meyer 2014-08-22 11:05:20 +02:00
parent d1b1bc368f
commit bc05d2ee64
4 changed files with 139 additions and 184 deletions

View File

@ -58,20 +58,15 @@ class ConfigController extends BaseConfigController
$form = new GeneralForm();
$request = $this->getRequest();
$currentConfig = IcingaConfig::app();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
$formConfig = $form->getConfiguration();
$newConfig = new Zend_Config($currentConfig->toArray(), true);
$newConfig->global = $formConfig->global;
$newConfig->preferences = $formConfig->preferences;
if ($this->writeConfigFile($newConfig, 'config')) {
if ($this->writeConfigFile($form->getConfiguration(), 'config')) {
Notification::success($this->translate('New configuration has successfully been stored'));
$this->redirectNow('config');
}
}
} else {
$form->setConfiguration($currentConfig);
$form->setConfiguration(IcingaConfig::app());
}
$this->view->form = $form;

View File

@ -8,7 +8,9 @@ use DateTimeZone;
use Zend_Config;
use Icinga\Web\Form;
use Icinga\Util\Translator;
use Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory;
use Icinga\Web\Form\Validator\WritablePathValidator;
/**
* Configuration form for general, application-wide settings
@ -34,7 +36,11 @@ class GeneralForm extends Form
$this->getModulePathInput($formData)
);
return array_merge($elements, $this->getPreferencesElements($formData));
return array_merge(
$elements,
$this->getPreferencesElements($formData),
$this->getLoggingElements($formData)
);
}
/**
@ -63,13 +69,14 @@ class GeneralForm extends Form
*/
public function setConfiguration(Zend_Config $config)
{
$defaults = $config->get('global', new Zend_Config(array()))->toArray();
if ($config->get('preferences', new Zend_Config(array()))->type !== null) {
$defaults['preferences_type'] = $config->get('preferences')->type;
$defaults['preferences_resource'] = $config->get('preferences')->resource;
$defaults = array();
foreach ($config as $section => $properties) {
foreach ($properties as $name => $value) {
$defaults[$section . '_' . $name] = $value;
}
}
$this->setDefaults($defaults);
$this->populate($defaults);
return $this;
}
@ -80,19 +87,14 @@ class GeneralForm extends Form
*/
public function getConfiguration()
{
$config = array();
$values = $this->getValues();
$globalData = array(
'language' => $values['language'],
'timezone' => $values['timezone'],
'modulePath' => $values['modulePath']
);
$preferencesData = array('type' => $values['preferences_type']);
if ($values['preferences_type'] === 'db') {
$preferencesData['resource'] = $values['preferences_resource'];
foreach ($values as $sectionAndPropertyName => $value) {
list($section, $property) = explode('_', $sectionAndPropertyName);
$config[$section][$property] = $value;
}
return new Zend_Config(array('global' => $globalData, 'preferences' => $preferencesData));
return new Zend_Config($config);
}
/**
@ -113,7 +115,7 @@ class GeneralForm extends Form
return $this->createElement(
'select',
'language',
'global_language',
array(
'label' => t('Default Language'),
'required' => true,
@ -144,7 +146,7 @@ class GeneralForm extends Form
$this->addElement(
'select',
'timezone',
'global_timezone',
array(
'label' => t('Default Application Timezone'),
'required' => true,
@ -167,7 +169,7 @@ class GeneralForm extends Form
{
$this->addElement(
'text',
'modulePath',
'global_modulePath',
array(
'label' => t('Module Path'),
'required' => true,
@ -232,4 +234,108 @@ class GeneralForm extends Form
return $elements;
}
/**
* Return form elements to setup the application's logging
*
* @param array $formData The data sent by the user
*
* @return array
*/
protected function getLoggingElements(array $formData)
{
$elements = array();
$elements[] = $this->createElement(
'select',
'logging_level',
array(
'required' => true,
'label' => t('Logging Level'),
'helptext' => t('The maximum loglevel to emit.'),
'multiOptions' => array(
0 => t('None'),
1 => t('Error'),
2 => t('Warning'),
3 => t('Information'),
4 => t('Debug')
)
)
);
$elements[] = $this->createElement(
'select',
'logging_type',
array(
'required' => true,
'class' => 'autosubmit',
'label' => t('Logging Type'),
'helptext' => t('The type of logging to utilize.'),
'multiOptions' => array(
'syslog' => 'Syslog',
'file' => t('File')
)
)
);
if (false === isset($formData['logging_type']) || $formData['logging_type'] === 'syslog') {
$elements[] = $this->createElement(
'text',
'logging_application',
array(
'required' => true,
'label' => t('Application Prefix'),
'helptext' => t('The name of the application by which to prefix syslog messages.'),
'value' => 'icingaweb',
'validators' => array(
array(
'Regex',
false,
array(
'pattern' => '/^[^\W]+$/',
'messages' => array(
'regexNotMatch' => 'The application prefix cannot contain any whitespaces.'
)
)
)
)
)
);
$elements[] = $this->createElement(
'select',
'logging_facility',
array(
'required' => true,
'label' => t('Facility'),
'helptext' => t('The Syslog facility to utilize.'),
'multiOptions' => array(
'LOG_USER' => 'LOG_USER'
)
)
);
} elseif ($formData['logging_type'] === 'file') {
$elements[] = $this->createElement(
'text',
'logging_target',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'),
'value' => $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator())
)
);
}
return $elements;
}
/**
* Return the default logging directory for type "file"
*
* @return string
*/
protected function getDefaultLogDir()
{
return realpath(Icinga::app()->getApplicationDir('../var/log/icingaweb.log'));
}
}

View File

@ -1,154 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config;
use Icinga\Web\Form;
use Icinga\Application\Icinga;
use Icinga\Web\Form\Validator\WritablePathValidator;
/**
* Form class for setting the application wide logging configuration
*/
class LoggingForm extends Form
{
/**
* Initialize this logging configuration form
*
* Sets actually only the name.
*/
public function init()
{
$this->setName('form_config_logging');
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$elements = array();
$elements[] = $this->createElement(
'checkbox',
'enable',
array(
'required' => true,
'label' => t('Logging Enabled'),
'helptext' => t('Check this to enable logging.'),
'value' => isset($formData['enable']) ? $formData['enable'] : 0
)
);
$elements[] = $this->createElement(
'select',
'level',
array(
'required' => true,
'label' => t('Logging Level'),
'helptext' => t('The maximum loglevel to emit.'),
'value' => isset($formData['level']) ? $formData['level'] : 0,
'multiOptions' => array(
0 => t('Error'),
1 => t('Warning'),
2 => t('Information'),
3 => t('Debug')
)
)
);
$elements[] = $this->createElement(
'select',
'type',
array(
'required' => true,
'class' => 'autosubmit',
'label' => t('Logging Type'),
'helptext' => t('The type of logging to utilize.'),
'value' => isset($formData['type']) ? $formData['type'] : 'syslog',
'multiOptions' => array(
'file' => t('File'),
'syslog' => 'Syslog'
)
)
);
if (false === isset($formData['type']) || $formData['type'] === 'syslog') {
$elements[] = $this->createElement(
'text',
'application',
array(
'required' => true,
'label' => t('Application Prefix'),
'helptext' => t('The name of the application by which to prefix syslog messages.'),
'value' => isset($formData['application']) ? $formData['application'] : 'icingaweb',
'validators' => array(
array(
'Regex',
false,
array(
'pattern' => '/^[^\W]+$/',
'messages' => array(
'regexNotMatch' => 'The application prefix cannot contain any whitespaces.'
)
)
)
)
)
);
$elements[] = $this->createElement(
'select',
'facility',
array(
'required' => true,
'label' => t('Facility'),
'helptext' => t('The Syslog facility to utilize.'),
'value' => isset($formData['facility']) ? $formData['facility'] : 'LOG_USER',
'multiOptions' => array(
'LOG_USER' => 'LOG_USER'
)
)
);
} elseif ($formData['type'] === 'file') {
$elements[] = $this->createElement(
'text',
'target',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'),
'value' => isset($formData['target']) ? $formData['target'] : $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator())
)
);
}
return $elements;
}
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Save')
)
);
return $this;
}
/**
* Return the default logging directory for type "file"
*
* @return string
*/
protected function getDefaultLogDir()
{
return realpath(Icinga::app()->getApplicationDir() . '/../var/log/icingaweb.log');
}
}

View File

@ -6,6 +6,7 @@ namespace Icinga\Logger;
use Exception;
use Zend_Config;
use LogicException;
use Icinga\Exception\ConfigurationError;
/**
@ -37,10 +38,11 @@ class Logger
/**
* The supported severities
*/
public static $ERROR = 0;
public static $WARNING = 1;
public static $INFO = 2;
public static $DEBUG = 3;
public static $NONE = 0;
public static $ERROR = 1;
public static $WARNING = 2;
public static $INFO = 3;
public static $DEBUG = 4;
/**
* Create a new logger object
@ -90,9 +92,15 @@ class Logger
*
* @param string $message The message to write
* @param int $severity The severity to use
*
* @throws LogicException In case $severity equals self::$NONE
*/
public function log($message, $severity)
{
if ($severity === static::$NONE) {
throw new LogicException("`None' (0) is not a valid severity to log messages");
}
if ($this->writer !== null && $this->verbosity >= $severity) {
$this->writer->log($severity, $message);
}