Permalink
Browse files

Add mask setting to FileLog

  • Loading branch information...
1 parent b105318 commit 278700677ae2ee3cc57644397c8ee0da715b552f @remidewitte remidewitte committed Mar 7, 2013
Showing with 49 additions and 2 deletions.
  1. +19 −2 lib/Cake/Log/Engine/FileLog.php
  2. +30 −0 lib/Cake/Test/Case/Log/Engine/FileLogTest.php
View
21 lib/Cake/Log/Engine/FileLog.php
@@ -42,7 +42,8 @@ class FileLog extends BaseLog {
'types' => null,
'scopes' => array(),
'rotate' => 10,
- 'size' => 10485760 // 10MB
+ 'size' => 10485760, // 10MB
+ 'mask' => null,
);
/**
@@ -81,6 +82,8 @@ class FileLog extends BaseLog {
* human reabable string values like '10MB', '100KB' etc.
* - `rotate` Log files are rotated specified times before being removed.
* If value is 0, old versions are removed rather then rotated.
+ * - `mask` A mask is applied when log files are created. Left empty no chmod
+ * is made.
*
* @param array $options Options for the FileLog, see above.
*/
@@ -132,7 +135,21 @@ public function write($type, $message) {
$this->_rotateFile($filename);
}
- return file_put_contents($this->_path . $filename, $output, FILE_APPEND);
+ $pathname = $this->_path . $filename;
+ if (empty($this->_config['mask'])) {
+ return file_put_contents($pathname, $output, FILE_APPEND);
+ }
+
+ $exists = file_exists($pathname);
+ $r = file_put_contents($pathname, $output, FILE_APPEND);
+ static $selfError = false;
+ if (!$selfError && !$exists && !chmod($pathname, (int)$this->_config['mask'])) {
+ $selfError = true;
+ trigger_error(__d(
+ 'cake_dev', 'Could not apply permission mask "%s" on log file "%s"',
+ array($pathname, $this->_config['mask'])), E_USER_WARNING);
+ $selfError = false;
+ }
}
/**
View
30 lib/Cake/Test/Case/Log/Engine/FileLogTest.php
@@ -148,6 +148,36 @@ public function testRotation() {
$this->assertEquals(0, count(glob($path . 'debug.log.*')));
}
+ public function testMaskSetting() {
+ if (DS === '\\') {
+ $this->markTestSkipped('File permission testing does not work on Windows.');
+ }
+
+ $path = TMP . 'tests' . DS;
+ $this->_deleteLogs($path);
+
+ $log = new FileLog(array('path' => $path, 'mask' => 0666));
+ $log->write('warning', 'Test warning one');
+ $result = substr(sprintf('%o',fileperms($path . 'error.log')), -4);
+ $expected = '0666';
+ $this->assertEquals($expected, $result);
+ unlink($path . 'error.log');
+
+ $log = new FileLog(array('path' => $path, 'mask' => 0644));
+ $log->write('warning', 'Test warning two');
+ $result = substr(sprintf('%o',fileperms($path . 'error.log')), -4);
+ $expected = '0644';
+ $this->assertEquals($expected, $result);
+ unlink($path . 'error.log');
+
+ $log = new FileLog(array('path' => $path, 'mask' => 0640));
+ $log->write('warning', 'Test warning three');
+ $result = substr(sprintf('%o',fileperms($path . 'error.log')), -4);
+ $expected = '0640';
+ $this->assertEquals($expected, $result);
+ unlink($path . 'error.log');
+ }
+
/**
* helper function to clears all log files in specified directory
*

0 comments on commit 2787006

Please sign in to comment.