Browse Source

Add log file to store logs into custom log files

Ishan Vyas 5 years ago
parent
commit
335e06f371
1 changed files with 84 additions and 0 deletions
  1. 84 0
      src/Utility/Log.php

+ 84 - 0
src/Utility/Log.php

@@ -0,0 +1,84 @@
+<?php
+
+namespace Tools\Utility;
+
+use Cake\Log\Log as CoreLog;
+
+/**
+ * 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);
+    }
+}