| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace Tools\Utility;
- use Cake\Log\Log as CoreLog;
- use Exception;
- use Throwable;
- /**
- * Wrapper class to log data into custom file(s).
- */
- class FileLog {
- /**
- * Debug configuration.
- *
- * @var mixed|null
- */
- protected static $_debugConfig;
- /**
- * Initialize configurations.
- *
- * @param string|null $filename Filename to log.
- * @return void
- */
- protected static function _init($filename): void {
- if ($filename === null) {
- $filename = 'custom_log';
- }
- CoreLog::drop('custom');
- 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 \ArrayObject|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): bool {
- static::_init($filename);
- // Pretty print array or object
- if (is_array($data) || is_object($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(): void {
- CoreLog::drop('custom');
- CoreLog::setConfig('debug', static::$_debugConfig);
- }
- }
|