mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-27 07:44:04 +02:00
parent
45d7864198
commit
54a5e996bb
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
namespace Icinga\Logger;
|
namespace Icinga\Logger;
|
||||||
|
|
||||||
use \Zend_Config;
|
use Zend_Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class for writers that write messages to a log
|
* Abstract class for writers that write messages to a log
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
namespace Icinga\Logger;
|
namespace Icinga\Logger;
|
||||||
|
|
||||||
use \Exception;
|
use Exception;
|
||||||
use \Zend_Config;
|
use Zend_Config;
|
||||||
use Icinga\Exception\ConfigurationError;
|
use Icinga\Exception\ConfigurationError;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
namespace Icinga\Logger\Writer;
|
namespace Icinga\Logger\Writer;
|
||||||
|
|
||||||
use \Zend_Config;
|
use Exception;
|
||||||
|
use Zend_Config;
|
||||||
use Icinga\Logger\Logger;
|
use Icinga\Logger\Logger;
|
||||||
use Icinga\Logger\LogWriter;
|
use Icinga\Logger\LogWriter;
|
||||||
use Icinga\Application\Config;
|
use Icinga\Application\Config;
|
||||||
@ -87,11 +88,17 @@ class StreamWriter extends LogWriter
|
|||||||
* Write a message to the stream
|
* Write a message to the stream
|
||||||
*
|
*
|
||||||
* @param string $text The message to write
|
* @param string $text The message to write
|
||||||
|
*
|
||||||
|
* @throws Exception In case write acess to the stream failed
|
||||||
*/
|
*/
|
||||||
protected function write($text)
|
protected function write($text)
|
||||||
{
|
{
|
||||||
$fd = fopen($this->stream, 'a');
|
$fd = fopen($this->stream, 'a');
|
||||||
fwrite($fd, $text . PHP_EOL);
|
|
||||||
|
if ($fd === false || fwrite($fd, $text . PHP_EOL) === false) {
|
||||||
|
throw new Exception('Failed to write to log file "' . $this->stream . '"');
|
||||||
|
}
|
||||||
|
|
||||||
fclose($fd);
|
fclose($fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
namespace Icinga\Logger\Writer;
|
namespace Icinga\Logger\Writer;
|
||||||
|
|
||||||
use \Exception;
|
use Exception;
|
||||||
use \Zend_Config;
|
use Zend_Config;
|
||||||
use Icinga\Logger\Logger;
|
use Icinga\Logger\Logger;
|
||||||
use Icinga\Logger\LogWriter;
|
use Icinga\Logger\LogWriter;
|
||||||
use Icinga\Exception\ConfigurationError;
|
use Icinga\Exception\ConfigurationError;
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
<?php
|
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
|
|
||||||
namespace Tests\Icinga\Application;
|
|
||||||
|
|
||||||
use \Zend_Config;
|
|
||||||
use Icinga\Logger\Logger;
|
|
||||||
use Icinga\Test\BaseTestCase;
|
|
||||||
|
|
||||||
class LoggerTest extends BaseTestCase
|
|
||||||
{
|
|
||||||
public function testLogfileCreation()
|
|
||||||
{
|
|
||||||
$target = tempnam(sys_get_temp_dir(), 'log');
|
|
||||||
unlink($target);
|
|
||||||
new Logger(
|
|
||||||
new Zend_Config(
|
|
||||||
array(
|
|
||||||
'enable' => true,
|
|
||||||
'level' => Logger::$ERROR,
|
|
||||||
'type' => 'stream',
|
|
||||||
'target' => $target
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertFileExists($target, 'Logger did not create the log file');
|
|
||||||
unlink($target);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @depends testLogfileCreation
|
|
||||||
*/
|
|
||||||
public function testLoggingErrorMessages()
|
|
||||||
{
|
|
||||||
$target = tempnam(sys_get_temp_dir(), 'log');
|
|
||||||
unlink($target);
|
|
||||||
$logger = new Logger(
|
|
||||||
new Zend_Config(
|
|
||||||
array(
|
|
||||||
'enable' => true,
|
|
||||||
'level' => Logger::$ERROR,
|
|
||||||
'type' => 'stream',
|
|
||||||
'target' => $target
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$logger->log('This is a test error', Logger::$ERROR);
|
|
||||||
$log = file_get_contents($target);
|
|
||||||
unlink($target);
|
|
||||||
$this->assertContains('This is a test error', $log, 'Log does not contain the error "This is a test error"');
|
|
||||||
}
|
|
||||||
}
|
|
44
test/php/library/Icinga/Logger/Writer/StreamWriterTest.php
Normal file
44
test/php/library/Icinga/Logger/Writer/StreamWriterTest.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
namespace Tests\Icinga\Logger\Writer;
|
||||||
|
|
||||||
|
use Zend_Config;
|
||||||
|
use Icinga\Logger\Logger;
|
||||||
|
use Icinga\Test\BaseTestCase;
|
||||||
|
use Icinga\Logger\Writer\StreamWriter;
|
||||||
|
|
||||||
|
class LoggerTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->target = tempnam(sys_get_temp_dir(), 'log');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
parent::tearDown();
|
||||||
|
|
||||||
|
unlink($this->target);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWhetherStreamWriterCreatesMissingFiles()
|
||||||
|
{
|
||||||
|
new StreamWriter(new Zend_Config(array('target' => $this->target)));
|
||||||
|
$this->assertFileExists($this->target, 'StreamWriter does not create missing files on initialization');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testWhetherStreamWriterCreatesMissingFiles
|
||||||
|
*/
|
||||||
|
public function testWhetherStreamWriterWritesMessages()
|
||||||
|
{
|
||||||
|
$writer = new StreamWriter(new Zend_Config(array('target' => $this->target)));
|
||||||
|
$writer->log(Logger::$ERROR, 'This is a test error');
|
||||||
|
$log = file_get_contents($this->target);
|
||||||
|
$this->assertContains('This is a test error', $log, 'StreamWriter does not write log messages');
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user