Merge pull request #2740 from Icinga/feature/emit-log-messages-in-the-webserver-s-log-11652

Emit log messages to the web server log
This commit is contained in:
lippserd 2017-09-08 15:43:31 +02:00 committed by GitHub
commit 0e5313d4d1
2 changed files with 42 additions and 2 deletions

View File

@ -39,6 +39,7 @@ class LoggingConfigForm extends Form
'label' => $this->translate('Logging Type'), 'label' => $this->translate('Logging Type'),
'description' => $this->translate('The type of logging to utilize.'), 'description' => $this->translate('The type of logging to utilize.'),
'multiOptions' => array( 'multiOptions' => array(
'php' => $this->translate('Webserver Log', 'app.config.logging.type'),
'syslog' => 'Syslog', 'syslog' => 'Syslog',
'file' => $this->translate('File', 'app.config.logging.type'), 'file' => $this->translate('File', 'app.config.logging.type'),
'none' => $this->translate('None', 'app.config.logging.type') 'none' => $this->translate('None', 'app.config.logging.type')
@ -64,7 +65,7 @@ class LoggingConfigForm extends Form
); );
} }
if (false === isset($formData['logging_log']) || $formData['logging_log'] === 'syslog') { if (false === isset($formData['logging_log']) || in_array($formData['logging_log'], array('syslog', 'php'))) {
$this->addElement( $this->addElement(
'text', 'text',
'logging_application', 'logging_application',
@ -72,7 +73,7 @@ class LoggingConfigForm extends Form
'required' => true, 'required' => true,
'label' => $this->translate('Application Prefix'), 'label' => $this->translate('Application Prefix'),
'description' => $this->translate( 'description' => $this->translate(
'The name of the application by which to prefix syslog messages.' 'The name of the application by which to prefix log messages.'
), ),
'requirement' => $this->translate('The application prefix must not contain whitespace.'), 'requirement' => $this->translate('The application prefix must not contain whitespace.'),
'value' => 'icingaweb2', 'value' => 'icingaweb2',

View File

@ -0,0 +1,39 @@
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
namespace Icinga\Application\Logger\Writer;
use Icinga\Application\Logger;
use Icinga\Application\Logger\LogWriter;
use Icinga\Data\ConfigObject;
use Icinga\Exception\NotWritableError;
/**
* Log to the webserver log, a file or syslog
*
* @see https://secure.php.net/manual/en/errorfunc.configuration.php#ini.error-log
*/
class PhpWriter extends LogWriter
{
/**
* Prefix to prepend to each message
*
* @var string
*/
protected $ident;
public function __construct(ConfigObject $config)
{
parent::__construct($config);
$this->ident = $config->get('application', 'icingaweb2');
}
public function log($severity, $message)
{
if (ini_get('error_log') === 'syslog') {
$message = str_replace("\n", ' ', $message);
}
error_log($this->ident . ': ' . Logger::$levels[$severity] . ' - ' . $message);
}
}