Browse Source

Add mask setting to FileLog

Rémi Dewitte 13 years ago
parent
commit
278700677a
2 changed files 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

+ 19 - 2
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 @@ class FileLog extends BaseLog {
 			$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;
+		}
 	}
 
 /**

+ 30 - 0
lib/Cake/Test/Case/Log/Engine/FileLogTest.php

@@ -148,6 +148,36 @@ class FileLogTest extends CakeTestCase {
 		$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
  *