Menu: do not show application log when none exists

Added new functions to the logger to get rid of distributed logging config
"knowledge".

fixes #7062
fixes #7098
This commit is contained in:
Thomas Gelf 2014-09-05 15:28:39 +02:00
parent 975985dc8c
commit b034028ff8
3 changed files with 40 additions and 16 deletions

View File

@ -9,6 +9,7 @@ use Icinga\Data\ResourceFactory;
use Icinga\Logger\Logger; use Icinga\Logger\Logger;
use Icinga\Logger\Writer\FileWriter; use Icinga\Logger\Writer\FileWriter;
use Icinga\Protocol\File\FileReader; use Icinga\Protocol\File\FileReader;
use \Zend_Controller_Action_Exception as ActionError;
/** /**
* Class ListController * Class ListController
@ -38,20 +39,21 @@ class ListController extends Controller
*/ */
public function applicationlogAction() public function applicationlogAction()
{ {
if (! Logger::writesToFile()) {
throw new ActionError('Site not found', 404);
}
$this->addTitleTab('application log'); $this->addTitleTab('application log');
$pattern = '/^(?<datetime>[0-9]{4}(-[0-9]{2}){2}' // date
. 'T[0-9]{2}(:[0-9]{2}){2}([\\+\\-][0-9]{2}:[0-9]{2})?)' // time
. ' - (?<loglevel>[A-Za-z]+)' // loglevel
. ' - (?<message>.*)$/' // message
$loggerWriter = Logger::getInstance()->getWriter(); $loggerWriter = Logger::getInstance()->getWriter();
if ($loggerWriter instanceof FileWriter) { $resource = new FileReader(new Zend_Config(array(
$resource = new FileReader(new Zend_Config(array( 'filename' => $loggerWriter->getPath(),
'filename' => $loggerWriter->getPath(), 'fields' => $pattern
'fields' => '/^(?<datetime>[0-9]{4}(-[0-9]{2}){2}' // date )));
. 'T[0-9]{2}(:[0-9]{2}){2}([\\+\\-][0-9]{2}:[0-9]{2})?)' // time $this->view->logData = $resource->select()->order('DESC')->paginate();
. ' - (?<loglevel>[A-Za-z]+)' // loglevel
. ' - (?<message>.*)$/' // message
)));
$this->view->logData = $resource->select()->order('DESC')->paginate();
} else {
$this->view->logData = null;
}
} }
} }

View File

@ -27,6 +27,13 @@ class Logger
*/ */
protected $writer; protected $writer;
/**
* The configured type
*
* @string Type (syslog, file)
*/
protected $type = 'none';
/** /**
* The maximum severity to emit * The maximum severity to emit
* *
@ -83,6 +90,7 @@ class Logger
$config->type $config->type
); );
} }
$this->type = $config->type;
return new $class($config); return new $class($config);
} }
@ -204,6 +212,16 @@ class Logger
return $this->writer; return $this->writer;
} }
public static function writesToSyslog()
{
return static::$instance && static::$instance->type === 'syslog';
}
public static function writesToFile()
{
return static::$instance && static::$instance->type === 'file';
}
/** /**
* Get this' instance * Get this' instance
* *

View File

@ -8,6 +8,7 @@ use RecursiveIterator;
use Zend_Config; use Zend_Config;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Logger\Logger;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Web\Url; use Icinga\Web\Url;
@ -195,10 +196,13 @@ class Menu implements RecursiveIterator
'url' => 'config/modules', 'url' => 'config/modules',
'priority' => 400 'priority' => 400
)); ));
$section->add(t('ApplicationLog'), array(
'url' => 'list/applicationlog', if (Logger::writesToFile()) {
'priority' => 500 $section->add(t('Application Log'), array(
)); 'url' => 'list/applicationlog',
'priority' => 500
));
}
$this->add(t('Logout'), array( $this->add(t('Logout'), array(
'url' => 'authentication/logout', 'url' => 'authentication/logout',