From 70cf14f775daa9203ef99897422cd19bffb11019 Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 11:34:21 +0200 Subject: [PATCH 1/9] Logger: rename getWriter() -> createWriter() refs #7060 --- library/Icinga/Logger/Logger.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Icinga/Logger/Logger.php b/library/Icinga/Logger/Logger.php index 87811ae69..c3550a610 100644 --- a/library/Icinga/Logger/Logger.php +++ b/library/Icinga/Logger/Logger.php @@ -52,7 +52,7 @@ class Logger $this->verbosity = $config->level; if ($config->enable) { - $this->writer = $this->getWriter($config); + $this->writer = $this->createWriter($config); } } @@ -75,7 +75,7 @@ class Logger * * @throws ConfigurationError In case the requested writer cannot be found */ - protected function getWriter(Zend_Config $config) + protected function createWriter(Zend_Config $config) { $class = 'Icinga\\Logger\\Writer\\' . ucfirst(strtolower($config->type)) . 'Writer'; if (!class_exists($class)) { From 6aeab59eadff3f4eb89500f3daf826e6c1cb28bd Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 12:02:11 +0200 Subject: [PATCH 2/9] Logger: implement getWriter() and getInstance() refs #7060 --- library/Icinga/Logger/Logger.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/Icinga/Logger/Logger.php b/library/Icinga/Logger/Logger.php index c3550a610..b01ac2a71 100644 --- a/library/Icinga/Logger/Logger.php +++ b/library/Icinga/Logger/Logger.php @@ -194,4 +194,14 @@ class Logger static::$instance->log(static::formatMessage(func_get_args()), static::$DEBUG); } } + + public function getWriter() + { + return $this->writer; + } + + public static function getInstance() + { + return static::$instance; + } } From 1e06c20dcb0eca4837497f95689b70c8e7393044 Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 12:09:38 +0200 Subject: [PATCH 3/9] FileWriter: implement getPath() refs #7060 --- library/Icinga/Logger/Writer/FileWriter.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/Icinga/Logger/Writer/FileWriter.php b/library/Icinga/Logger/Writer/FileWriter.php index 2a50ca36b..4ab04d85b 100644 --- a/library/Icinga/Logger/Writer/FileWriter.php +++ b/library/Icinga/Logger/Writer/FileWriter.php @@ -103,4 +103,12 @@ class FileWriter extends LogWriter $file->fwrite($text); $file->fflush(); } + + /** + * @return string + */ + public function getPath() + { + return $this->path; + } } From 3b8fcb80a8633be3dc20390dc22207dd00ff61be Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 12:44:23 +0200 Subject: [PATCH 4/9] Application log: don't crash if logData === null refs #7060 --- application/views/scripts/list/applicationlog.phtml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/application/views/scripts/list/applicationlog.phtml b/application/views/scripts/list/applicationlog.phtml index 68b115152..583fde516 100644 --- a/application/views/scripts/list/applicationlog.phtml +++ b/application/views/scripts/list/applicationlog.phtml @@ -5,6 +5,7 @@
+ logData !== null): ?> logData as $value): ?> @@ -21,4 +22,5 @@
-
\ No newline at end of file + + From f4b820aa574b0a2151c9ddbe9b91867fc663b82e Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 13:12:22 +0200 Subject: [PATCH 5/9] Application log: don't use Icinga\Application\Config to get the log file name refs #7060 --- application/controllers/ListController.php | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/application/controllers/ListController.php b/application/controllers/ListController.php index dac0cc68b..599df06a1 100644 --- a/application/controllers/ListController.php +++ b/application/controllers/ListController.php @@ -4,9 +4,11 @@ use Icinga\Module\Monitoring\Controller; use Icinga\Web\Hook; -use Icinga\Application\Config as IcingaConfig; use Icinga\Web\Url; use Icinga\Data\ResourceFactory; +use Icinga\Logger\Logger; +use Icinga\Logger\Writer\FileWriter; +use Icinga\Protocol\File\Reader as FileReader; /** * Class ListController @@ -37,15 +39,16 @@ class ListController extends Controller public function applicationlogAction() { $this->addTitleTab('application log'); - $config_ini = IcingaConfig::app()->toArray(); - if (!in_array('logging', $config_ini) || ( - in_array('type', $config_ini['logging']) && - $config_ini['logging']['type'] === 'file' && - in_array('target', $config_ini['logging']) && - file_exists($config_ini['logging']['target']) - ) - ) { - $resource = ResourceFactory::create('logfile'); + + $loggerWriter = Logger::getInstance()->getWriter(); + if ($loggerWriter instanceof FileWriter) { + $resource = new FileReader(new Zend_Config(array( + 'filename' => $loggerWriter->getPath(), + 'fields' => '/^(?[0-9]{4}(-[0-9]{2}){2}' // date + . 'T[0-9]{2}(:[0-9]{2}){2}([\\+\\-][0-9]{2}:[0-9]{2})?)' // time + . ' - (?[A-Za-z]+)' // loglevel + . ' - (?.*)$/' // message + ))); $this->view->logData = $resource->select()->order('DESC')->paginate(); } else { $this->view->logData = null; From 33669f8969e7e4fdbcc953e6a9e779aad6601b10 Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 13:53:46 +0200 Subject: [PATCH 6/9] Logger: add/update documentation refs #7060 --- library/Icinga/Logger/Logger.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/library/Icinga/Logger/Logger.php b/library/Icinga/Logger/Logger.php index b01ac2a71..828b4de88 100644 --- a/library/Icinga/Logger/Logger.php +++ b/library/Icinga/Logger/Logger.php @@ -23,7 +23,7 @@ class Logger /** * The log writer to use * - * @var LogWriter + * @var \Icinga\Logger\LogWriter */ protected $writer; @@ -71,9 +71,8 @@ class Logger * * @param Zend_Config $config The configuration to initialize the writer with * - * @return LogWriter The requested log writer - * - * @throws ConfigurationError In case the requested writer cannot be found + * @return \Icinga\Logger\LogWriter The requested log writer + * @throws ConfigurationError If the requested writer cannot be found */ protected function createWriter(Zend_Config $config) { @@ -195,11 +194,21 @@ class Logger } } + /** + * Get the log writer to use + * + * @return \Icinga\Logger\LogWriter + */ public function getWriter() { return $this->writer; } + /** + * Get this' instance + * + * @return Logger + */ public static function getInstance() { return static::$instance; From 88dde47b7a3306bccc1293d6bb5ea1e909b8299a Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 15:39:25 +0200 Subject: [PATCH 7/9] Foreach: add $value so that the value won't be assigned to $key refs #7060 --- library/Icinga/Protocol/File/Reader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Icinga/Protocol/File/Reader.php b/library/Icinga/Protocol/File/Reader.php index 651846b58..a9b0455b7 100644 --- a/library/Icinga/Protocol/File/Reader.php +++ b/library/Icinga/Protocol/File/Reader.php @@ -81,7 +81,7 @@ class Reader extends FilterIterator if ($matched === false) { throw new FileReaderException('Failed parsing regular expression!'); } else if ($matched === 1) { - foreach ($data as $key) { + foreach ($data as $key => $value) { if (is_int($key)) { unset($data[$key]); } From e45f50408ac5a95f7df77eb6a1faf5bbb392df7f Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 16:14:14 +0200 Subject: [PATCH 8/9] Icinga\Protocol\File\Reader::fetchPairs(): rename variables $skipLines -> $skip $readLines -> $read refs #7060 --- library/Icinga/Protocol/File/Reader.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/library/Icinga/Protocol/File/Reader.php b/library/Icinga/Protocol/File/Reader.php index a9b0455b7..1ab088c9c 100644 --- a/library/Icinga/Protocol/File/Reader.php +++ b/library/Icinga/Protocol/File/Reader.php @@ -137,26 +137,26 @@ class Reader extends FilterIterator */ public function fetchPairs(Query $query) { - $skipLines = $query->getOffset(); - $readLines = $query->getLimit(); - if ($skipLines === null) { - $skipLines = 0; + $skip = $query->getOffset(); + $read = $query->getLimit(); + if ($skip === null) { + $skip = 0; } $lines = array(); if ($query->sortDesc()) { $count = $this->count($query); - if ($count <= $skipLines) { + if ($count <= $skip) { return $lines; - } else if ($count < ($skipLines + $readLines)) { - $readLines = $count - $skipLines; - $skipLines = 0; + } else if ($count < ($skip + $read)) { + $read = $count - $skip; + $skip = 0; } else { - $skipLines = $count - ($skipLines + $readLines); + $skip = $count - ($skip + $read); } } foreach ($this as $index => $line) { - if ($index >= $skipLines) { - if ($index >= $skipLines + $readLines) { + if ($index >= $skip) { + if ($index >= $skip + $read) { break; } $lines[] = $line; From ede5b8f0b90388da2c8e20de31ca8f46ed15292f Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Wed, 3 Sep 2014 18:36:59 +0200 Subject: [PATCH 9/9] Icinga\Protocol\File\Reader::fetchPairs(): increase $index manually as the iterator's one makes trouble refs #7060 --- library/Icinga/Protocol/File/Reader.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Protocol/File/Reader.php b/library/Icinga/Protocol/File/Reader.php index 1ab088c9c..0f5509b7b 100644 --- a/library/Icinga/Protocol/File/Reader.php +++ b/library/Icinga/Protocol/File/Reader.php @@ -154,13 +154,15 @@ class Reader extends FilterIterator $skip = $count - ($skip + $read); } } - foreach ($this as $index => $line) { + $index = 0; + foreach ($this as $line) { if ($index >= $skip) { if ($index >= $skip + $read) { break; } $lines[] = $line; } + ++$index; } if ($query->sortDesc()) { $lines = array_reverse($lines);