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
|
|
|
/**
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring frontends
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
2013-06-06 16:52:54 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2013-06-03 16:14:46 +02:00
|
|
|
namespace Icinga\Application;
|
|
|
|
|
2013-06-07 13:35:03 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* Class Logger
|
|
|
|
* @package Icinga\Application
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
class Logger
|
|
|
|
{
|
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-06-03 16:14:46 +02:00
|
|
|
const DEFAULT_LOG_TARGET = "./var/log/icinga.log";
|
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
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private $writers = array();
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private $logger = null;
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private static $instance;
|
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
|
|
|
/**
|
|
|
|
* @param \Zend_Config $config
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public function __construct(\Zend_Config $config)
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @param \Zend_Config $config
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public function overwrite(\Zend_Config $config)
|
|
|
|
{
|
|
|
|
$this->clearLog();
|
|
|
|
try {
|
2013-06-06 16:52:54 +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-06-06 16:52:54 +02:00
|
|
|
$this->warn("Could not create debug log: {$e->getMessage()}");
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->setupLog($config);
|
|
|
|
$this->flushQueue();
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @param \Zend_Config $config
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private function setupDebugLog(\Zend_Config $config)
|
|
|
|
{
|
|
|
|
$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) {
|
|
|
|
$type == self::DEFAULT_LOG_TYPE;
|
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
|
|
|
|
$this->addWriter($type, $target, \Zend_Log::DEBUG);
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @param \Zend_Config $config
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private function setupLog(\Zend_Config $config)
|
|
|
|
{
|
|
|
|
$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-06-03 16:14:46 +02:00
|
|
|
$type == self::DEFAULT_LOG_TYPE;
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
$level = \Zend_Log::WARN;
|
2013-06-06 16:52:54 +02:00
|
|
|
if ($config->get("verbose", 0) == 1) {
|
2013-06-03 16:14:46 +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
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @param $target
|
|
|
|
* @param $priority
|
2013-06-07 13:35:03 +02:00
|
|
|
* @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]);
|
|
|
|
$writerClass = "\Zend_Log_Writer_" . $type;
|
2013-06-06 16:52:54 +02:00
|
|
|
if (!class_exists($writerClass)) {
|
2013-06-07 13:35:03 +02:00
|
|
|
throw new ConfigurationError("Could not create log: Unknown type " . $type);
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
|
|
|
|
$writer = new $writerClass($target);
|
|
|
|
|
|
|
|
$writer->addFilter(new \Zend_Log_Filter_Priority($priority));
|
|
|
|
$this->logger->addWriter($writer);
|
|
|
|
$this->writers[] = $writer;
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* flushQueue
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public function flushQueue()
|
|
|
|
{
|
2013-06-06 16:52:54 +02:00
|
|
|
foreach (self::$queue as $msgTypePair) {
|
|
|
|
$this->logger->log($msgTypePair[0], $msgTypePair[1]);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @param array $argv
|
|
|
|
* @return string
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* clearLog
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public function clearLog()
|
|
|
|
{
|
|
|
|
$this->logger = null;
|
|
|
|
$this->writers = array();
|
|
|
|
$this->logger = new \Zend_Log();
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @param \Zend_Config $config
|
|
|
|
* @return Logger
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public static function create(\Zend_Config $config)
|
|
|
|
{
|
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
|
|
|
/**
|
|
|
|
* debug
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public static function debug()
|
|
|
|
{
|
2013-06-06 16:52:54 +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
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function warn()
|
|
|
|
{
|
|
|
|
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
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function info()
|
|
|
|
{
|
|
|
|
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
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function error()
|
|
|
|
{
|
|
|
|
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
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function fatal()
|
|
|
|
{
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param $msg
|
|
|
|
* @param int $level
|
|
|
|
*/
|
|
|
|
private static function log($msg, $level = \Zend_Log::INFO)
|
|
|
|
{
|
2013-06-03 16:14:46 +02:00
|
|
|
$logger = self::$instance;
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
if (!$logger) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function reset()
|
|
|
|
{
|
2013-06-03 16:14:46 +02:00
|
|
|
self::$queue = array();
|
|
|
|
self::$instance = null;
|
|
|
|
}
|
|
|
|
}
|