summaryrefslogtreecommitdiffstats
path: root/application/forms/Config/General/LoggingConfigForm.php
blob: bbc772314890f6f0ca499c33c623021cf5020f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Forms\Config\General;

use Icinga\Application\Logger;
use Icinga\Application\Logger\Writer\SyslogWriter;
use Icinga\Application\Platform;
use Icinga\Web\Form;

/**
 * Configuration form for logging options
 *
 * This form is not used directly but as subform for the {@link GeneralConfigForm}.
 */
class LoggingConfigForm extends Form
{
    /**
     * {@inheritdoc}
     */
    public function init()
    {
        $this->setName('form_config_general_logging');
    }

    /**
     * {@inheritdoc}
     *
     * @return  $this
     */
    public function createElements(array $formData)
    {
        $defaultType = getenv('ICINGAWEB_OFFICIAL_DOCKER_IMAGE') ? 'php' : 'syslog';

        $this->addElement(
            'select',
            'logging_log',
            array(
                'required'      => true,
                'autosubmit'    => true,
                'label'         => $this->translate('Logging Type'),
                'description'   => $this->translate('The type of logging to utilize.'),
                'value'         => $defaultType,
                'multiOptions'  => array(
                    'syslog'    => 'Syslog',
                    'php'       => $this->translate('Webserver Log', 'app.config.logging.type'),
                    'file'      => $this->translate('File', 'app.config.logging.type'),
                    'none'      => $this->translate('None', 'app.config.logging.type')
                )
            )
        );

        if (! isset($formData['logging_log']) || $formData['logging_log'] !== 'none') {
            $this->addElement(
                'select',
                'logging_level',
                array(
                    'required'      => true,
                    'label'         => $this->translate('Logging Level'),
                    'description'   => $this->translate('The maximum logging level to emit.'),
                    'multiOptions'  => array(
                        Logger::$levels[Logger::ERROR]   => $this->translate('Error', 'app.config.logging.level'),
                        Logger::$levels[Logger::WARNING] => $this->translate('Warning', 'app.config.logging.level'),
                        Logger::$levels[Logger::INFO]    => $this->translate('Information', 'app.config.logging.level'),
                        Logger::$levels[Logger::DEBUG]   => $this->translate('Debug', 'app.config.logging.level')
                    )
                )
            );
        }

        if (! isset($formData['logging_log']) || in_array($formData['logging_log'], array('syslog', 'php'))) {
            $this->addElement(
                'text',
                'logging_application',
                array(
                    'required'      => true,
                    'label'         => $this->translate('Application Prefix'),
                    'description'   => $this->translate(
                        'The name of the application by which to prefix log messages.'
                    ),
                    'requirement'   => $this->translate('The application prefix must not contain whitespace.'),
                    'value'         => 'icingaweb2',
                    'validators'    => array(
                        array(
                            'Regex',
                            false,
                            array(
                                'pattern'  => '/^\S+$/',
                                'messages' => array(
                                    'regexNotMatch' => $this->translate(
                                        'The application prefix must not contain whitespace.'
                                    )
                                )
                            )
                        )
                    )
                )
            );

            if ((isset($formData['logging_log']) ? $formData['logging_log'] : $defaultType) === 'syslog') {
                if (Platform::isWindows()) {
                    /* @see https://secure.php.net/manual/en/function.openlog.php */
                    $this->addElement(
                        'hidden',
                        'logging_facility',
                        array(
                            'value' => 'user',
                            'disabled' => true
                        )
                    );
                } else {
                    $facilities = array_keys(SyslogWriter::$facilities);
                    $this->addElement(
                        'select',
                        'logging_facility',
                        array(
                            'required' => true,
                            'label' => $this->translate('Facility'),
                            'description' => $this->translate('The syslog facility to utilize.'),
                            'value' => 'user',
                            'multiOptions' => array_combine($facilities, $facilities)
                        )
                    );
                }
            }
        } elseif (isset($formData['logging_log']) && $formData['logging_log'] === 'file') {
            $this->addElement(
                'text',
                'logging_file',
                array(
                    'required'      => true,
                    'label'         => $this->translate('File path'),
                    'description'   => $this->translate('The full path to the log file to write messages to.'),
                    'value'         => '/var/log/icingaweb2/icingaweb2.log',
                    'validators'    => array('WritablePathValidator')
                )
            );
        }

        return $this;
    }
}