Ishan Vyas 5 years ago
parent
commit
3eb947d8f5
4 changed files with 112 additions and 108 deletions
  1. 1 1
      docs/Contributing.md
  2. 76 74
      src/Utility/Log.php
  3. 25 23
      tests/TestCase/Utility/LogTest.php
  4. 10 10
      tests/bootstrap.php

+ 1 - 1
docs/Contributing.md

@@ -15,7 +15,7 @@ There are a few guidelines that I need contributors to follow:
 * Coding standards (`composer cs-check` to check and `composer cs-fix` to fix)
 * Coding standards (`composer cs-check` to check and `composer cs-fix` to fix)
 * Passing tests (you can enable travis to assert your changes pass) for Windows and Unix (`php phpunit.phar`)
 * Passing tests (you can enable travis to assert your changes pass) for Windows and Unix (`php phpunit.phar`)
 
 
-## i18n 
+## i18n
 Check if translations via pot file need to be changed.
 Check if translations via pot file need to be changed.
 Run
 Run
 ```
 ```

+ 76 - 74
src/Utility/Log.php

@@ -3,82 +3,84 @@
 namespace Tools\Utility;
 namespace Tools\Utility;
 
 
 use Cake\Log\Log as CoreLog;
 use Cake\Log\Log as CoreLog;
+use Exception;
 
 
 /**
 /**
  * Wrapper class to log data into custom file(s).
  * Wrapper class to log data into custom file(s).
  */
  */
-class Log
-{
-    /**
-     * Debug configuration.
-     *
-     * @var mixed|null
-     */
-    protected static $_debugConfig;
-
-    /**
-     * Initialize configurations.
-     *
-     * @param string $filename Filename to log.
-     * @return void
-     */
-    protected static function _init($filename) {
-        if ($filename === null) {
-            $filename = 'custom_log';
-        }
-
-        CoreLog::setConfig('custom', [
-            'className' => 'File',
-            'path' => LOGS,
-            'levels' => [],
-            'scopes' => ['custom'],
-            'file' => $filename,
-        ]);
-
-        static::$_debugConfig = CoreLog::getConfig('debug');
-
-        CoreLog::drop('debug');
-    }
-
-    /**
-     * Log data into custom file
-     *
-     * @param array|string $data Data to store
-     * @param string $filename Filename of log file
-     * @param bool $traceKey Add trace string key into log data
-     * @return bool Success
-     */
-    public static function write($data, $filename = null, $traceKey = false) {
-        static::_init($filename);
-
-        // Pretty print array
-        if (is_array($data)) {
-            if ($traceKey) {
-                try {
-                    throw new \Exception('Trace string', 1);
-                } catch (\Throwable $t) {
-                    $data['trace_string'] = $t->getTraceAsString();
-                }
-            }
-
-            $data = print_r($data, true);
-        }
-
-        $logged = CoreLog::write('debug', $data, ['scope' => 'custom']);
-
-        static::_cleanUp();
-
-        return $logged;
-    }
-
-    /**
-     * Drop custom log config, set default `debug` config in log registry.
-     *
-     * @return void
-     */
-    protected static function _cleanUp() {
-        CoreLog::drop('custom');
-
-        CoreLog::setConfig('debug', static::$_debugConfig);
-    }
+class Log {
+
+	/**
+	 * Debug configuration.
+	 *
+	 * @var mixed|null
+	 */
+	protected static $_debugConfig;
+
+	/**
+	 * Initialize configurations.
+	 *
+	 * @param string $filename Filename to log.
+	 * @return void
+	 */
+	protected static function _init($filename) {
+		if ($filename === null) {
+			$filename = 'custom_log';
+		}
+
+		CoreLog::setConfig('custom', [
+			'className' => 'File',
+			'path' => LOGS,
+			'levels' => [],
+			'scopes' => ['custom'],
+			'file' => $filename,
+		]);
+
+		static::$_debugConfig = CoreLog::getConfig('debug');
+
+		CoreLog::drop('debug');
+	}
+
+	/**
+	 * Log data into custom file
+	 *
+	 * @param array|string $data Data to store
+	 * @param string|null $filename Filename of log file
+	 * @param bool $traceKey Add trace string key into log data
+	 * @return bool Success
+	 */
+	public static function write($data, $filename = null, $traceKey = false) {
+		static::_init($filename);
+
+		// Pretty print array
+		if (is_array($data)) {
+			if ($traceKey) {
+				try {
+					throw new Exception('Trace string', 1);
+				} catch (\Throwable $t) {
+					$data['trace_string'] = $t->getTraceAsString();
+				}
+			}
+
+			$data = print_r($data, true);
+		}
+
+		$logged = CoreLog::write('debug', $data, ['scope' => 'custom']);
+
+		static::_cleanUp();
+
+		return $logged;
+	}
+
+	/**
+	 * Drop custom log config, set default `debug` config in log registry.
+	 *
+	 * @return void
+	 */
+	protected static function _cleanUp() {
+		CoreLog::drop('custom');
+
+		CoreLog::setConfig('debug', static::$_debugConfig);
+	}
+
 }
 }

+ 25 - 23
tests/TestCase/Utility/LogTest.php

@@ -9,6 +9,7 @@ use Tools\Utility\Log;
  * LogTest class
  * LogTest class
  */
  */
 class LogTest extends TestCase {
 class LogTest extends TestCase {
+
 	/**
 	/**
 	 * Filename with path to use in string test case.
 	 * Filename with path to use in string test case.
 	 *
 	 *
@@ -42,20 +43,20 @@ class LogTest extends TestCase {
 	 * @return void
 	 * @return void
 	 */
 	 */
 	public function testLogsStringData() {
 	public function testLogsStringData() {
-		if (file_exists(self::TEST_FILEPATH_STRING)) {
-            unlink(self::TEST_FILEPATH_STRING);
-        }
+		if (file_exists(static::TEST_FILEPATH_STRING)) {
+			unlink(static::TEST_FILEPATH_STRING);
+		}
 
 
-		$result = Log::write('It works!', self::TEST_FILENAME_STRING);
+		$result = Log::write('It works!', static::TEST_FILENAME_STRING);
 
 
 		$this->assertTrue($result);
 		$this->assertTrue($result);
-		$this->assertFileExists(self::TEST_FILEPATH_STRING);
+		$this->assertFileExists(static::TEST_FILEPATH_STRING);
 		$this->assertRegExp(
 		$this->assertRegExp(
 			'/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: It works!/',
 			'/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: It works!/',
-			file_get_contents(self::TEST_FILEPATH_STRING)
+			file_get_contents(static::TEST_FILEPATH_STRING)
 		);
 		);
 
 
-		unlink(self::TEST_FILENAME_STRING);
+		unlink(static::TEST_FILENAME_STRING);
 	}
 	}
 
 
 	/**
 	/**
@@ -64,12 +65,12 @@ class LogTest extends TestCase {
 	 * @return void
 	 * @return void
 	 */
 	 */
 	public function testLogsArray() {
 	public function testLogsArray() {
-		if (file_exists(self::TEST_FILEPATH_ARRAY1)) {
-            unlink(self::TEST_FILEPATH_ARRAY1);
-        }
-        if (file_exists(self::TEST_FILEPATH_ARRAY2)) {
-            unlink(self::TEST_FILEPATH_ARRAY2);
-        }
+		if (file_exists(static::TEST_FILEPATH_ARRAY1)) {
+			unlink(static::TEST_FILEPATH_ARRAY1);
+		}
+		if (file_exists(static::TEST_FILEPATH_ARRAY2)) {
+			unlink(static::TEST_FILEPATH_ARRAY2);
+		}
 
 
 		$result1 = Log::write(
 		$result1 = Log::write(
 			[
 			[
@@ -77,9 +78,9 @@ class LogTest extends TestCase {
 					'id' => 1,
 					'id' => 1,
 					'firstname' => 'John Doe',
 					'firstname' => 'John Doe',
 					'email' => 'john.doe@example.com',
 					'email' => 'john.doe@example.com',
-				]
+				],
 			],
 			],
-			self::TEST_FILENAME_ARRAY1
+			static::TEST_FILENAME_ARRAY1
 		);
 		);
 
 
 		$result2 = Log::write(
 		$result2 = Log::write(
@@ -88,15 +89,15 @@ class LogTest extends TestCase {
 					'id' => 2,
 					'id' => 2,
 					'firstname' => 'Jane Doe',
 					'firstname' => 'Jane Doe',
 					'email' => 'jane.doe@example.com',
 					'email' => 'jane.doe@example.com',
-				]
+				],
 			],
 			],
-			self::TEST_FILENAME_ARRAY2
+			static::TEST_FILENAME_ARRAY2
 		);
 		);
 
 
 		// Assert for `TEST_FILENAME_ARRAY1`
 		// Assert for `TEST_FILENAME_ARRAY1`
 		$this->assertTrue($result1);
 		$this->assertTrue($result1);
-		$this->assertFileExists(self::TEST_FILEPATH_ARRAY1);
-		$fileContents = file_get_contents(self::TEST_FILEPATH_ARRAY1);
+		$this->assertFileExists(static::TEST_FILEPATH_ARRAY1);
+		$fileContents = file_get_contents(static::TEST_FILEPATH_ARRAY1);
 		$this->assertRegExp(
 		$this->assertRegExp(
 			'/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Array([\s\S]*)\(([\s\S]*)[user]([\s\S]*)\[id\] => 1/',
 			'/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Array([\s\S]*)\(([\s\S]*)[user]([\s\S]*)\[id\] => 1/',
 			$fileContents
 			$fileContents
@@ -104,14 +105,15 @@ class LogTest extends TestCase {
 
 
 		// Assert for `TEST_FILENAME_ARRAY2`
 		// Assert for `TEST_FILENAME_ARRAY2`
 		$this->assertTrue($result2);
 		$this->assertTrue($result2);
-		$this->assertFileExists(self::TEST_FILEPATH_ARRAY2);
-		$fileContents = file_get_contents(self::TEST_FILEPATH_ARRAY2);
+		$this->assertFileExists(static::TEST_FILEPATH_ARRAY2);
+		$fileContents = file_get_contents(static::TEST_FILEPATH_ARRAY2);
 		$this->assertRegExp(
 		$this->assertRegExp(
 			'/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Array([\s\S]*)\(([\s\S]*)[user]([\s\S]*)\[id\] => 2/',
 			'/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Array([\s\S]*)\(([\s\S]*)[user]([\s\S]*)\[id\] => 2/',
 			$fileContents
 			$fileContents
 		);
 		);
 
 
-		unlink(self::TEST_FILEPATH_ARRAY1);
-		unlink(self::TEST_FILEPATH_ARRAY2);
+		unlink(static::TEST_FILEPATH_ARRAY1);
+		unlink(static::TEST_FILEPATH_ARRAY2);
 	}
 	}
+
 }
 }

+ 10 - 10
tests/bootstrap.php

@@ -74,18 +74,18 @@ $cache = [
 Cake\Cache\Cache::setConfig($cache);
 Cake\Cache\Cache::setConfig($cache);
 
 
 Cake\Log\Log::setConfig('debug', [
 Cake\Log\Log::setConfig('debug', [
-    'className' => 'Cake\Log\Engine\FileLog',
-    'path' => LOGS,
-    'file' => 'debug',
-    'levels' => ['notice', 'info', 'debug'],
-    'url' => env('LOG_DEBUG_URL', null)
+	'className' => 'Cake\Log\Engine\FileLog',
+	'path' => LOGS,
+	'file' => 'debug',
+	'levels' => ['notice', 'info', 'debug'],
+	'url' => env('LOG_DEBUG_URL', null),
 ]);
 ]);
 Cake\Log\Log::setConfig('error', [
 Cake\Log\Log::setConfig('error', [
-    'className' => 'Cake\Log\Engine\FileLog',
-    'path' => LOGS,
-    'file' => 'error',
-    'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
-    'url' => env('LOG_ERROR_URL', null)
+	'className' => 'Cake\Log\Engine\FileLog',
+	'path' => LOGS,
+	'file' => 'error',
+	'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
+	'url' => env('LOG_ERROR_URL', null),
 ]);
 ]);
 
 
 Cake\Core\Plugin::getCollection()->add(new Tools\Plugin());
 Cake\Core\Plugin::getCollection()->add(new Tools\Plugin());