Rename Icinga\Logger\Writer\StreamWriter to FileWriter

refs #6038
This commit is contained in:
Johannes Meyer 2014-04-30 11:59:22 +02:00
parent 0b3d3e9bb2
commit 4cf3044a53
4 changed files with 37 additions and 37 deletions

View File

@ -66,7 +66,7 @@ class ListController extends Controller
$config_ini = IcingaConfig::app()->toArray(); $config_ini = IcingaConfig::app()->toArray();
if (!in_array('logging', $config_ini) || ( if (!in_array('logging', $config_ini) || (
in_array('type', $config_ini['logging']) && in_array('type', $config_ini['logging']) &&
$config_ini['logging']['type'] === 'stream' && $config_ini['logging']['type'] === 'file' &&
in_array('target', $config_ini['logging']) && in_array('target', $config_ini['logging']) &&
file_exists($config_ini['logging']['target']) file_exists($config_ini['logging']['target'])
) )

View File

@ -41,7 +41,7 @@ use Icinga\Web\Form\Validator\WritablePathValidator;
class LoggingForm extends Form class LoggingForm extends Form
{ {
/** /**
* Return the default logging directory for type "stream" * Return the default logging directory for type "file"
* *
* @return string * @return string
*/ */
@ -97,30 +97,17 @@ class LoggingForm extends Form
'required' => true, 'required' => true,
'label' => t('Logging Type'), 'label' => t('Logging Type'),
'helptext' => t('The type of logging to utilize.'), 'helptext' => t('The type of logging to utilize.'),
'value' => $loggingConfig->get('type', 'stream'), 'value' => $loggingConfig->get('type', 'file'),
'multiOptions' => array( 'multiOptions' => array(
'stream' => t('File'), 'file' => t('File'),
'syslog' => 'Syslog' 'syslog' => 'Syslog'
) )
) )
); );
$this->enableAutoSubmit(array('logging_type')); $this->enableAutoSubmit(array('logging_type'));
switch ($this->getRequest()->getParam('logging_type', $loggingConfig->get('type', 'stream'))) switch ($this->getRequest()->getParam('logging_type', $loggingConfig->get('type', 'file')))
{ {
case 'stream':
$this->addElement(
'text',
'logging_target',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'),
'value' => $loggingConfig->target ? $loggingConfig->target : $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator())
)
);
break;
case 'syslog': case 'syslog':
$this->addElement( $this->addElement(
'text', 'text',
@ -146,6 +133,19 @@ class LoggingForm extends Form
) )
); );
break; break;
case 'file':
default:
$this->addElement(
'text',
'logging_target',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The logfile to write messages to.'),
'value' => $loggingConfig->target ? $loggingConfig->target : $this->getDefaultLogDir(),
'validators' => array(new WritablePathValidator())
)
);
} }
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes'); $this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
@ -166,7 +166,7 @@ class LoggingForm extends Form
switch ($values['logging_type']) switch ($values['logging_type'])
{ {
case 'stream': case 'file':
$cfg['logging']['target'] = $values['logging_target']; $cfg['logging']['target'] = $values['logging_target'];
break; break;
case 'syslog': case 'syslog':

View File

@ -12,23 +12,23 @@ use Icinga\Application\Config;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
/** /**
* Class to write log messages to a stream * Class to write log messages to a file
*/ */
class StreamWriter extends LogWriter class FileWriter extends LogWriter
{ {
/** /**
* The path to the stream * The path to the file
* *
* @var string * @var string
*/ */
protected $stream; protected $path;
/** /**
* Create a new log writer initialized with the given configuration * Create a new log writer initialized with the given configuration
*/ */
public function __construct(Zend_Config $config) public function __construct(Zend_Config $config)
{ {
$this->stream = Config::resolvePath($config->target); $this->path = Config::resolvePath($config->target);
$this->setup(); $this->setup();
} }
@ -44,17 +44,17 @@ class StreamWriter extends LogWriter
} }
/** /**
* Create the stream if it does not already exist * Create the file if it does not already exist
*/ */
protected function setup() protected function setup()
{ {
if (substr($this->stream, 0, 6) !== 'php://') { if (substr($this->path, 0, 6) !== 'php://') {
if (!file_exists($this->stream) && (!@touch($this->stream) || !@chmod($this->stream, 0664))) { if (!file_exists($this->path) && (!@touch($this->path) || !@chmod($this->path, 0664))) {
throw new ConfigurationError('Cannot create log file "' . $this->stream . '"'); throw new ConfigurationError('Cannot create log file "' . $this->path . '"');
} }
if (!@is_writable($this->stream)) { if (!@is_writable($this->path)) {
throw new ConfigurationError('Cannot write to log file "' . $this->stream . '"'); throw new ConfigurationError('Cannot write to log file "' . $this->path . '"');
} }
} }
} }
@ -85,18 +85,18 @@ class StreamWriter extends LogWriter
} }
/** /**
* Write a message to the stream * Write a message to the path
* *
* @param string $text The message to write * @param string $text The message to write
* *
* @throws Exception In case write acess to the stream failed * @throws Exception In case write acess to the path failed
*/ */
protected function write($text) protected function write($text)
{ {
$fd = fopen($this->stream, 'a'); $fd = fopen($this->path, 'a');
if ($fd === false || fwrite($fd, $text . PHP_EOL) === false) { if ($fd === false || fwrite($fd, $text . PHP_EOL) === false) {
throw new Exception('Failed to write to log file "' . $this->stream . '"'); throw new Exception('Failed to write to log file "' . $this->path . '"');
} }
fclose($fd); fclose($fd);

View File

@ -7,7 +7,7 @@ namespace Tests\Icinga\Logger\Writer;
use Zend_Config; use Zend_Config;
use Icinga\Logger\Logger; use Icinga\Logger\Logger;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
use Icinga\Logger\Writer\StreamWriter; use Icinga\Logger\Writer\FileWriter;
class LoggerTest extends BaseTestCase class LoggerTest extends BaseTestCase
{ {
@ -27,7 +27,7 @@ class LoggerTest extends BaseTestCase
public function testWhetherStreamWriterCreatesMissingFiles() public function testWhetherStreamWriterCreatesMissingFiles()
{ {
new StreamWriter(new Zend_Config(array('target' => $this->target))); new FileWriter(new Zend_Config(array('target' => $this->target)));
$this->assertFileExists($this->target, 'StreamWriter does not create missing files on initialization'); $this->assertFileExists($this->target, 'StreamWriter does not create missing files on initialization');
} }
@ -36,7 +36,7 @@ class LoggerTest extends BaseTestCase
*/ */
public function testWhetherStreamWriterWritesMessages() public function testWhetherStreamWriterWritesMessages()
{ {
$writer = new StreamWriter(new Zend_Config(array('target' => $this->target))); $writer = new FileWriter(new Zend_Config(array('target' => $this->target)));
$writer->log(Logger::$ERROR, 'This is a test error'); $writer->log(Logger::$ERROR, 'This is a test error');
$log = file_get_contents($this->target); $log = file_get_contents($this->target);
$this->assertContains('This is a test error', $log, 'StreamWriter does not write log messages'); $this->assertContains('This is a test error', $log, 'StreamWriter does not write log messages');