2013-06-03 16:14:46 +02:00
|
|
|
<?php
|
2013-06-06 16:52:54 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-28 16:47:30 +02:00
|
|
|
/**
|
2013-07-15 12:26:10 +02:00
|
|
|
* This file is part of Icinga 2 Web.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-07-15 12:26:10 +02:00
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
2013-06-28 16:47:30 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
2013-07-15 12:26:10 +02:00
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
2013-06-28 16:47:30 +02:00
|
|
|
*/
|
2013-06-06 16:52:54 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2013-06-03 16:14:46 +02:00
|
|
|
namespace Icinga\Application;
|
|
|
|
|
2013-08-27 18:26:30 +02:00
|
|
|
use Icinga\Protocol\Ldap\Exception;
|
2013-08-23 09:57:32 +02:00
|
|
|
use \Zend_Config;
|
|
|
|
use \Zend_Log;
|
|
|
|
use \Zend_Log_Filter_Priority;
|
|
|
|
use \Zend_Log_Writer_Abstract;
|
|
|
|
use \Zend_Log_Exception;
|
|
|
|
use \Icinga\Exception\ConfigurationError;
|
2013-06-07 13:35:03 +02:00
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Singleton logger
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-08-23 09:57:32 +02:00
|
|
|
final class Logger
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Default log type
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
const DEFAULT_LOG_TYPE = "stream";
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Default log target
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
const DEFAULT_LOG_TARGET = "./var/log/icinga.log";
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Default debug target
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
const DEFAULT_DEBUG_TARGET = "./var/log/icinga.debug.log";
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Array of writers
|
|
|
|
*
|
2013-06-06 16:52:54 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private $writers = array();
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Instance of Zend_Log
|
|
|
|
*
|
|
|
|
* @var Zend_Log
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-08-23 09:57:32 +02:00
|
|
|
private $logger;
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Singleton instance
|
|
|
|
*
|
|
|
|
* @var Logger
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private static $instance;
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Queue of unwritten messages
|
|
|
|
*
|
2013-06-06 16:52:54 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private static $queue = array();
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Flag indicate that errors occurred in the past
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $errorsOccurred = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new logger object
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-08-23 09:57:32 +02:00
|
|
|
public function __construct(Zend_Config $config)
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
|
|
|
$this->overwrite($config);
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getWriters()
|
|
|
|
{
|
2013-06-03 16:14:46 +02:00
|
|
|
return $this->writers;
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Overwrite config to initiated logger
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config
|
|
|
|
*
|
|
|
|
* @return self
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-08-23 09:57:32 +02:00
|
|
|
public function overwrite(Zend_Config $config)
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
|
|
|
$this->clearLog();
|
|
|
|
try {
|
2013-08-29 17:31:24 +02:00
|
|
|
if ($config->debug && $config->debug->enable == '1') {
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->setupDebugLog($config);
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-07 13:35:03 +02:00
|
|
|
} catch (ConfigurationError $e) {
|
2013-08-23 09:57:32 +02:00
|
|
|
$this->warn('Could not create debug log: ' . $e->getMessage());
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
2013-08-29 17:31:24 +02:00
|
|
|
if ($config->get('enable', '1') != '0') {
|
|
|
|
$this->setupLog($config);
|
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->flushQueue();
|
2013-08-23 09:57:32 +02:00
|
|
|
|
|
|
|
return $this;
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Configure debug log
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-08-23 09:57:32 +02:00
|
|
|
private function setupDebugLog(Zend_Config $config)
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
|
|
|
$type = $config->debug->get("type", self::DEFAULT_LOG_TYPE);
|
|
|
|
$target = $config->debug->get("target", self::DEFAULT_LOG_TARGET);
|
2013-06-06 16:52:54 +02:00
|
|
|
if ($target == self::DEFAULT_LOG_TARGET) {
|
2013-08-23 09:57:32 +02:00
|
|
|
$type = self::DEFAULT_LOG_TYPE;
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-08-23 09:57:32 +02:00
|
|
|
$this->addWriter($type, $target, Zend_Log::DEBUG);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Configure log
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-08-23 09:57:32 +02:00
|
|
|
private function setupLog(Zend_Config $config)
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
|
|
|
$type = $config->get("type", self::DEFAULT_LOG_TYPE);
|
|
|
|
$target = $config->get("target", self::DEFAULT_DEBUG_TARGET);
|
2013-06-06 16:52:54 +02:00
|
|
|
if ($target == self::DEFAULT_DEBUG_TARGET) {
|
2013-08-23 09:57:32 +02:00
|
|
|
$type = self::DEFAULT_LOG_TYPE;
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-08-23 09:57:32 +02:00
|
|
|
$level = Zend_Log::WARN;
|
2013-06-06 16:52:54 +02:00
|
|
|
if ($config->get("verbose", 0) == 1) {
|
2013-08-23 09:57:32 +02:00
|
|
|
$level = Zend_Log::INFO;
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->addWriter($type, $target, $level);
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Add writer to log instance
|
|
|
|
*
|
|
|
|
* @param string $type Type, e.g. stream
|
|
|
|
* @param string $target Target, e.g. filename
|
|
|
|
* @param int $priority Value of Zend::* constant
|
|
|
|
* @throws ConfigurationError
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
|
|
|
private function addWriter($type, $target, $priority)
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
|
|
|
$type[0] = strtoupper($type[0]);
|
2013-08-23 09:57:32 +02:00
|
|
|
$writerClass = "Zend_Log_Writer_" . $type;
|
2013-06-03 16:14:46 +02:00
|
|
|
|
2013-08-23 09:57:32 +02:00
|
|
|
if (!@class_exists($writerClass)) {
|
|
|
|
self::fatal(
|
|
|
|
'Could not add log writer of type "%s". Type does not exist.',
|
|
|
|
$type
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2013-08-27 18:26:30 +02:00
|
|
|
|
|
|
|
$target = Config::resolvePath($target);
|
|
|
|
$writer = new $writerClass($target);
|
2013-08-23 09:57:32 +02:00
|
|
|
$writer->addFilter(new Zend_Log_Filter_Priority($priority));
|
2013-08-27 18:26:30 +02:00
|
|
|
// Make sure the permissions for log target file are correct
|
|
|
|
if ($type === 'Stream' && !file_exists($target)) {
|
|
|
|
touch($target);
|
|
|
|
chmod($target, 0664);
|
|
|
|
}
|
|
|
|
|
2013-08-23 09:57:32 +02:00
|
|
|
$this->logger->addWriter($writer);
|
|
|
|
$this->writers[] = $writer;
|
|
|
|
} catch (Zend_Log_Exception $e) {
|
|
|
|
self::fatal(
|
|
|
|
'Could not add log writer of type %s. An exception was thrown: %s',
|
|
|
|
$type,
|
|
|
|
$e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Flush pending messages to writer
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public function flushQueue()
|
|
|
|
{
|
2013-08-23 09:57:32 +02:00
|
|
|
try {
|
|
|
|
foreach (self::$queue as $msgTypePair) {
|
|
|
|
$this->logger->log($msgTypePair[0], $msgTypePair[1]);
|
|
|
|
}
|
|
|
|
} catch (Zend_Log_Exception $e) {
|
|
|
|
self::fatal(
|
|
|
|
'Could not flush logs to output. An exception was thrown: %s',
|
|
|
|
$e->getMessage()
|
|
|
|
);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Format output message
|
|
|
|
*
|
|
|
|
* @param array $argv
|
|
|
|
*
|
|
|
|
* @return string
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public static function formatMessage(array $argv)
|
|
|
|
{
|
|
|
|
if (count($argv) == 1) {
|
|
|
|
$format = $argv[0];
|
|
|
|
} else {
|
|
|
|
$format = array_shift($argv);
|
|
|
|
}
|
|
|
|
if (!is_string($format)) {
|
|
|
|
$format = json_encode($format);
|
|
|
|
}
|
|
|
|
foreach ($argv as &$arg) {
|
2013-06-06 16:52:54 +02:00
|
|
|
if (!is_string($arg)) {
|
2013-06-03 16:14:46 +02:00
|
|
|
$arg = json_encode($arg);
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return @vsprintf($format, $argv);
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Reset object configuration
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public function clearLog()
|
|
|
|
{
|
|
|
|
$this->logger = null;
|
|
|
|
$this->writers = array();
|
2013-08-23 09:57:32 +02:00
|
|
|
$this->logger = new Zend_Log();
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Create an instance
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config
|
|
|
|
*
|
|
|
|
* @return Logger
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-08-23 09:57:32 +02:00
|
|
|
public static function create(Zend_Config $config)
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
2013-06-06 16:52:54 +02:00
|
|
|
if (self::$instance) {
|
2013-06-03 16:14:46 +02:00
|
|
|
return self::$instance->overwrite($config);
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
return self::$instance = new Logger($config);
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Log message with severity debug
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public static function debug()
|
|
|
|
{
|
2013-08-23 09:57:32 +02:00
|
|
|
self::log(self::formatMessage(func_get_args()), Zend_Log::DEBUG);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Log message with severity warn
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
|
|
|
public static function warn()
|
|
|
|
{
|
2013-08-23 09:57:32 +02:00
|
|
|
self::log(self::formatMessage(func_get_args()), Zend_Log::WARN);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Log message with severity info
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
|
|
|
public static function info()
|
|
|
|
{
|
2013-08-23 09:57:32 +02:00
|
|
|
self::log(self::formatMessage(func_get_args()), Zend_Log::INFO);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Log message with severity error
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
|
|
|
public static function error()
|
|
|
|
{
|
2013-08-23 09:57:32 +02:00
|
|
|
self::log(self::formatMessage(func_get_args()), Zend_Log::ERR);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Log message with severity fatal
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
|
|
|
public static function fatal()
|
|
|
|
{
|
2013-08-23 09:57:32 +02:00
|
|
|
self::log(self::formatMessage(func_get_args()), Zend_Log::EMERG);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Log message
|
|
|
|
*
|
|
|
|
* @param string $msg Message
|
|
|
|
* @param int $level Log level
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
2013-08-23 09:57:32 +02:00
|
|
|
private static function log($msg, $level = Zend_Log::INFO)
|
2013-06-06 16:52:54 +02:00
|
|
|
{
|
2013-06-03 16:14:46 +02:00
|
|
|
$logger = self::$instance;
|
|
|
|
|
2013-08-23 09:57:32 +02:00
|
|
|
if ($level < Zend_Log::WARN && self::$errorsOccurred === false) {
|
|
|
|
self::$errorsOccurred =true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$logger || !count($logger->getWriters())) {
|
2013-06-06 16:52:54 +02:00
|
|
|
array_push(self::$queue, array($msg, $level));
|
2013-06-03 16:14:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
$logger->logger->log($msg, $level);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
2013-08-23 09:57:32 +02:00
|
|
|
* Flag if messages > warning occurred
|
2013-06-06 16:52:54 +02:00
|
|
|
*
|
2013-08-23 09:57:32 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function hasErrorsOccurred()
|
|
|
|
{
|
|
|
|
return self::$errorsOccurred;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access the log queue
|
|
|
|
*
|
|
|
|
* The log queue holds messages that could not be written to output
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getQueue()
|
|
|
|
{
|
|
|
|
return self::$queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset object state
|
2013-06-06 16:52:54 +02:00
|
|
|
*/
|
|
|
|
public static function reset()
|
|
|
|
{
|
2013-06-03 16:14:46 +02:00
|
|
|
self::$queue = array();
|
|
|
|
self::$instance = null;
|
|
|
|
}
|
|
|
|
}
|