PhpWriter: prefix messages with the app name as in Syslog

refs #11652
This commit is contained in:
Alexander A. Klimov 2016-11-03 12:35:40 +01:00
parent a24999ff7f
commit 8952434393
2 changed files with 22 additions and 3 deletions

View File

@ -63,7 +63,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(
'text',
'logging_application',
@ -71,7 +71,7 @@ class LoggingConfigForm extends Form
'required' => true,
'label' => $this->translate('Application Prefix'),
'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.'),
'value' => 'icingaweb2',

View File

@ -5,6 +5,7 @@ namespace Icinga\Application\Logger\Writer;
use Icinga\Application\Logger;
use Icinga\Application\Logger\LogWriter;
use Icinga\Data\ConfigObject;
use Icinga\Exception\NotWritableError;
/**
@ -14,12 +15,30 @@ use Icinga\Exception\NotWritableError;
*/
class PhpWriter extends LogWriter
{
/**
* Prefix to prepend to each message
*
* @var string
*/
protected $ident;
/**
* {@inheritDoc}
*/
public function __construct(ConfigObject $config)
{
parent::__construct($config);
$this->ident = $config->get('application', 'icingaweb2');
}
/**
* {@inheritdoc}
*/
public function log($severity, $message)
{
if (! error_log(Logger::$levels[$severity] . ' - ' . str_replace("\n", ' ', $message))) {
if (! error_log(
$this->ident . ': ' . Logger::$levels[$severity] . ' - ' . str_replace("\n", ' ', $message)
)) {
throw new NotWritableError('Could not log to ' . (ini_get('error_log') ?: 'SAPI'));
}
}