Permalink
Browse files

adding scoped logging

  • Loading branch information...
1 parent a269a12 commit 6040d967108149e734cf4a1ef5cc8bcad2945f2c @rchavik rchavik committed May 10, 2012
View
@@ -249,11 +249,14 @@ protected static function _autoConfig() {
*
* `CakeLog::write('warning', 'Stuff is broken here');`
*
- * @param string $type Type of message being written
+ * @param mixed $type Type of message being written. When value is an integer
+ * or a string matching the recognized levels, then it will
+ * be treated log levels. Otherwise it's treated as scope.
* @param string $message Message content to log
+ * @param mixed $scope string or array
@ADmad

ADmad May 15, 2012

Member

Can you please add some description for the $scope param. Currently one doesn't get any idea of what it does or what its values can be. Also an examples for usage with scope would be nice.

@markstory

markstory May 16, 2012

Owner

I can add some api docs for that.

@rchavik

rchavik May 16, 2012

Member

Yup. Will do.

* @return boolean Success
*/
- public static function write($type, $message) {
+ public static function write($type, $message, $scope = array()) {
if (empty(self::$_Collection)) {
self::_init();
}
@@ -269,14 +272,24 @@ public static function write($type, $message) {
if (is_int($type) && isset($levels[$type])) {
$type = $levels[$type];
}
+ if (is_string($type) && empty($scope) && !in_array($type, $levels)) {
+ $scope = $type;
+ }
if (empty(self::$_streams)) {
self::_autoConfig();
}
foreach (self::$_Collection->enabled() as $streamName) {
$logger = self::$_Collection->{$streamName};
$config = $logger->config();
$types = $config['types'];
- if (empty($types) || in_array($type, $types)) {
+ $scopes = $config['scopes'];
+ if (is_string($scope)) {
+ $inScope = in_array($scope, $scopes);
+ } else {
+ $intersect = array_intersect($scope, $scopes);
+ $inScope = !empty($intersect);
+ }
+ if (empty($types) || in_array($type, $types) || in_array($type, $scopes) && $inScope) {
$logger->write($type, $message);
}
}
@@ -50,6 +50,7 @@ public function __construct($config = array()) {
$config = Set::merge(array(
'stream' => 'php://stderr',
'types' => null,
+ 'scopes' => array(),
'outputAs' => ConsoleOutput::COLOR,
), $this->_config);
$config = $this->config($config);
@@ -49,6 +49,7 @@ public function __construct($config = array()) {
'path' => LOGS,
'file' => null,
'types' => null,
+ 'scopes' => array(),
), $this->_config);
$config = $this->config($config);
$this->_path = $config['path'];
@@ -74,6 +75,8 @@ public function write($type, $message) {
$filename = $this->_path . 'error.log';
} elseif (in_array($type, $debugTypes)) {
$filename = $this->_path . 'debug.log';
+ } elseif (in_array($type, $this->_config['scopes'])) {
+ $filename = $this->_path . $this->_file;
} else {
$filename = $this->_path . $type . '.log';
}
@@ -281,4 +281,152 @@ public function testStreamDisableInvalid() {
CakeLog::disable('bogus_stream');
}
+ protected function _deleteLogs() {
+ @unlink(LOGS . 'shops.log');
+ @unlink(LOGS . 'error.log');
+ @unlink(LOGS . 'debug.log');
+ }
+
+/**
+ * test backward compatible scoped logging
+ */
+ public function testScopedLoggingBC() {
+ if (file_exists(LOGS . 'shops.log')) {
+ unlink(LOGS . 'shops.log');
+ }
+ if (file_exists(LOGS . 'error.log')) {
+ unlink(LOGS . 'error.log');
+ }
+ if (file_exists(LOGS . 'debug.log')) {
+ unlink(LOGS . 'debug.log');
+ }
+
+ CakeLog::config('debug', array(
+ 'engine' => 'FileLog',
+ 'types' => array('notice', 'info', 'debug'),
+ 'file' => 'debug',
+ ));
+ CakeLog::config('error', array(
+ 'engine' => 'FileLog',
+ 'types' => array('error', 'warning'),
+ 'file' => 'error',
+ ));
+ CakeLog::config('shops', array(
+ 'engine' => 'FileLog',
+ 'types' => array('info', 'notice', 'warning'),
+ 'scopes' => array('transactions', 'orders'),
+ 'file' => 'shops',
+ ));
+
+ CakeLog::write('info', 'info message');
+ $this->assertFalse(file_exists(LOGS . 'error.log'));
+ $this->assertTrue(file_exists(LOGS . 'shops.log'));
+ $this->assertTrue(file_exists(LOGS . 'debug.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::write('transactions', 'transaction message');
+ $this->assertTrue(file_exists(LOGS . 'shops.log'));
+ $this->assertFalse(file_exists(LOGS . 'transactions.log'));
+ $this->assertFalse(file_exists(LOGS . 'error.log'));
+ $this->assertFalse(file_exists(LOGS . 'debug.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::write('error', 'error message');
+ $this->assertTrue(file_exists(LOGS . 'error.log'));
+ $this->assertFalse(file_exists(LOGS . 'debug.log'));
+ $this->assertFalse(file_exists(LOGS . 'shops.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::write('orders', 'order message');
+ $this->assertFalse(file_exists(LOGS . 'error.log'));
+ $this->assertFalse(file_exists(LOGS . 'debug.log'));
+ $this->assertFalse(file_exists(LOGS . 'orders.log'));
+ $this->assertTrue(file_exists(LOGS . 'shops.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::write('warning', 'warning message');
+ $this->assertTrue(file_exists(LOGS . 'error.log'));
+ $this->assertTrue(file_exists(LOGS . 'shops.log'));
+ $this->assertFalse(file_exists(LOGS . 'debug.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::drop('shops');
+ }
+
+/**
+ * test scoped logging
+ */
+ public function testScopedLogging() {
+ if (file_exists(LOGS . 'shops.log')) {
+ unlink(LOGS . 'shops.log');
+ }
+ if (file_exists(LOGS . 'error.log')) {
+ unlink(LOGS . 'error.log');
+ }
+ if (file_exists(LOGS . 'debug.log')) {
+ unlink(LOGS . 'debug.log');
+ }
+
+ CakeLog::config('debug', array(
+ 'engine' => 'FileLog',
+ 'types' => array('notice', 'info', 'debug'),
+ 'file' => 'debug',
+ ));
+ CakeLog::config('error', array(
+ 'engine' => 'FileLog',
+ 'types' => array('error', 'warning'),
+ 'file' => 'error',
+ ));
+ CakeLog::config('shops', array(
+ 'engine' => 'FileLog',
+ 'types' => array('info', 'notice', 'warning'),
+ 'scopes' => array('transactions', 'orders'),
+ 'file' => 'shops',
+ ));
+
+ CakeLog::write('info', 'info message', 'transactions');
+ $this->assertFalse(file_exists(LOGS . 'error.log'));
+ $this->assertTrue(file_exists(LOGS . 'shops.log'));
+ $this->assertTrue(file_exists(LOGS . 'debug.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::write('transactions', 'transaction message', 'orders');
+ $this->assertTrue(file_exists(LOGS . 'shops.log'));
+ $this->assertFalse(file_exists(LOGS . 'transactions.log'));
+ $this->assertFalse(file_exists(LOGS . 'error.log'));
+ $this->assertFalse(file_exists(LOGS . 'debug.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::write('error', 'error message', 'orders');
+ $this->assertTrue(file_exists(LOGS . 'error.log'));
+ $this->assertFalse(file_exists(LOGS . 'debug.log'));
+ $this->assertFalse(file_exists(LOGS . 'shops.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::write('orders', 'order message', 'transactions');
+ $this->assertFalse(file_exists(LOGS . 'error.log'));
+ $this->assertFalse(file_exists(LOGS . 'debug.log'));
+ $this->assertFalse(file_exists(LOGS . 'orders.log'));
+ $this->assertTrue(file_exists(LOGS . 'shops.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::write('warning', 'warning message', 'orders');
+ $this->assertTrue(file_exists(LOGS . 'error.log'));
+ $this->assertTrue(file_exists(LOGS . 'shops.log'));
+ $this->assertFalse(file_exists(LOGS . 'debug.log'));
+
+ $this->_deleteLogs();
+
+ CakeLog::drop('shops');
+ }
+
}

0 comments on commit 6040d96

Please sign in to comment.