[icinga-checkins] icinga.org: icingaweb2/master: PhpWriter: prefix messages with the app name as in Syslog

git at icinga.org git at icinga.org
Fri Nov 4 17:21:05 CET 2016


Module: icingaweb2
Branch: master
Commit: 8952434393ab59a30bc6c61f3d3f9e4ee293ff3c
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=8952434393ab59a30bc6c61f3d3f9e4ee293ff3c

Author: Alexander A. Klimov <alexander.klimov at icinga.com>
Date:   Thu Nov  3 12:35:40 2016 +0100

PhpWriter: prefix messages with the app name as in Syslog

refs #11652

---

 .../forms/Config/General/LoggingConfigForm.php     |    4 ++--
 .../Icinga/Application/Logger/Writer/PhpWriter.php |   21 +++++++++++++++++++-
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/application/forms/Config/General/LoggingConfigForm.php b/application/forms/Config/General/LoggingConfigForm.php
index 1ca6bf8..6a5cb92 100644
--- a/application/forms/Config/General/LoggingConfigForm.php
+++ b/application/forms/Config/General/LoggingConfigForm.php
@@ -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',
diff --git a/library/Icinga/Application/Logger/Writer/PhpWriter.php b/library/Icinga/Application/Logger/Writer/PhpWriter.php
index 51c2fea..913ea07 100644
--- a/library/Icinga/Application/Logger/Writer/PhpWriter.php
+++ b/library/Icinga/Application/Logger/Writer/PhpWriter.php
@@ -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;
 
 /**
@@ -15,11 +16,29 @@ 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'));
         }
     }



More information about the icinga-checkins mailing list