<?php
namespace Engine\Logger; abstract class GeneralAbstract { protected $logSession = null; public function __construct() { } protected function generateLogSession() { if (php_sapi_name()=='cli') { $logSession = sprintf("%s-%04s-%08s", \Engine\NumberConvertor::baseEncode(TIMESTAMP), \Engine\NumberConvertor::baseEncode(getmypid()), \Engine\NumberConvertor::baseEncode(mt_rand())); } else { $logSession = sprintf("%s-%04s-%09s-%08s", \Engine\NumberConvertor::baseEncode(TIMESTAMP), \Engine\NumberConvertor::baseEncode(getmypid()), substr(session_id(), 0, 9), \Engine\NumberConvertor::baseEncode(mt_rand())); } return $logSession; } public function setLogSession($logSession) { $this->logSession = $logSession; return $this; } public function getLogSession($autogenerate=true) { if ($autogenerate && !$this->logSession) { $this->logSession = $this->generateLogSession(); } return $this->logSession; } public function critical($message, array $context = []) { return $this->log('critical', $message, $context); } public function error($message, array $context = []) { return $this->log('error', $message, $context); } public function warning($message, array $context = []) { return $this->log('warning', $message, $context); } public function info($message, array $context = []) { return $this->log('info', $message, $context); } public function log($level, $message, array $context = []) { if (!in_array($level, ['critical', 'error', 'warning', 'info'])) { throw new \Exception("Invalid error level: {$level}"); } $stacktrace = ''; if (isset($context['exception']) && ($context['exception'] instanceof \Exception)) { $exceptionMessage = ''; if (get_class($context['exception'])=='App\Entity\Exception') { foreach ($context['exception']->getMessagesCollector()->getMessages() as $msg) { $exceptionMessage .= sprintf("%s:%s ", $msg->getType(), $msg->getText()); } } else { $exceptionMessage = $context['exception']->getMessage(); } $stacktrace = $context['exception']->getTrace(); $context['exception'] = sprintf("%s: '%s'. ", get_class($context['exception']), $exceptionMessage); } if (!$stacktrace) { $stacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); } try { return $this->writeRawMessage([ 'level' => $level, 'message' => $message, 'context' => $context, 'stacktrace' => $stacktrace, 'idUser' => CURRENT_ID_USER, 'addTime' => date('Y-m-d H:i:s'), 'logSession' => $this->getLogSession(true), ]); } catch (\RuntimeException $ex) { } } public function dbError($message, array $context = [], $dbErrorNo=null, $dbErrorMsg=null, $dbLastQuery=null) { $db = \DB::getInstance(); $context = array_merge([ 'ErrorMsg' => new \Engine\Logger\Context\StringRaw(sprintf("[%d] %s", ($dbErrorNo!==null?$dbErrorNo:$db->ErrorNo()), ($dbErrorMsg!==null?$dbErrorMsg:$db->ErrorMsg()))), 'query'=> new \Engine\Logger\Context\Sql(($dbLastQuery!==null?$dbLastQuery:\DB::getLastQuery())), ], $context); ksort($context); return $this->error($message, $context); } abstract public function writeRawMessage($fields); } 