diff --git a/Entity/LogEntry.php b/Entity/LogEntry.php new file mode 100644 index 0000000..07776ad --- /dev/null +++ b/Entity/LogEntry.php @@ -0,0 +1,187 @@ +createdAt = new DateTime(); + } + + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + + /** + * @param mixed $id + * @return LogEntry + */ + public function setId($id) + { + $this->id = $id; + return $this; + } + + /** + * @return mixed + */ + public function getMessage() + { + return $this->message; + } + + /** + * @param mixed $message + * @return LogEntry + */ + public function setMessage($message) + { + $this->message = $message; + return $this; + } + + /** + * @return mixed + */ + public function getContext() + { + return $this->context; + } + + /** + * @param mixed $context + * @return LogEntry + */ + public function setContext($context) + { + $this->context = $context; + return $this; + } + + /** + * @return mixed + */ + public function getLevel() + { + return $this->level; + } + + /** + * @param mixed $level + * @return LogEntry + */ + public function setLevel($level) + { + $this->level = $level; + return $this; + } + + /** + * @return mixed + */ + public function getLevelName() + { + return $this->levelName; + } + + /** + * @param mixed $levelName + * @return LogEntry + */ + public function setLevelName($levelName) + { + $this->levelName = $levelName; + return $this; + } + + /** + * @return mixed + */ + public function getChannel() + { + return $this->channel; + } + + /** + * @param mixed $channel + * @return LogEntry + */ + public function setChannel($channel) + { + $this->channel = $channel; + return $this; + } + + /** + * @return mixed + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + /** + * @param mixed $createdAt + * @return LogEntry + */ + public function setCreatedAt($createdAt) + { + $this->createdAt = $createdAt; + return $this; + } +} \ No newline at end of file diff --git a/Service/LoggerService.php b/Service/LoggerService.php new file mode 100644 index 0000000..08e4393 --- /dev/null +++ b/Service/LoggerService.php @@ -0,0 +1,219 @@ + 'DEBUG', + self::INFO => 'INFO', + self::NOTICE => 'NOTICE', + self::WARNING => 'WARNING', + self::ERROR => 'ERROR', + self::CRITICAL => 'CRITICAL', + self::ALERT => 'ALERT', + self::EMERGENCY => 'EMERGENCY', + ); + + private $name; + + /** + * @var EntityManagerInterface + */ + private $em; + + /** + * LoggerService constructor. + * @param EntityManagerInterface $em + */ + public function __construct(EntityManagerInterface $em) + { + $this->name = ""; + $this->em = $em; + } + + public function addNotice($message, $context = []) + { + return $this->addRecord(self::NOTICE, $message, $context); + } + + public function addWarning($message, $context = []) + { + return $this->addRecord(self::WARNING, $message, $context); + } + + public function addError($message, $context = []) + { + return $this->addRecord(self::ERROR, $message, $context); + } + + /** + * Adds a log record. + * + * @param int $level The logging level + * @param string $message The log message + * @param array $context The log context + * + * @return bool Whether the record has been processed + */ + public function addRecord($level, $message, $context = []) + { + $levelName = static::getLevelName($level); + + if( $level > self::WARNING) { + $context += $this->automaticContext(); + } + + $logEntry = (new LogEntry()) + ->setMessage((string) $message) + ->setContext($context) + ->setLevel($level) + ->setLevelName($levelName) + ->setChannel($this->name) + ; + + $this->em->persist($logEntry); + $this->em->flush(); + + return true; + } + + private function automaticContext() + { + $classFilters = [ + 'Ardent\LoggerBundle\Service\LoggerService', + 'HttpKernel\Kernel', + 'HttpKernel\HttpKernel', + ]; + $backtraces = debug_backtrace(); + $context = []; + $traces = 0; + foreach ($backtraces as $trace) { + $skip = false; + foreach ($classFilters as $class) { + if(strpos($trace['class'], $class)) { + $skip = true; + } + } + if($skip) { + continue; + } + $context[] = "function[$traces]: ".$trace['function']; + $context[] = "class[$traces]: ".$trace['class']; + ++$traces; + } + return $context; + } + + public function getLatest($level = self::DEBUG, $amount = 1) + { + $qb = $this->em->createQueryBuilder(); + + $qb + ->select('l') + ->from('ArdentLoggerBundle:LogEntry', 'l') + ->where($qb->expr()->gte('l.level', '?1')) + ->orderBy('l.createdAt', 'DESC') + ->setParameter(1, $level) + ->setMaxResults($amount); + ; + + return $qb->getQuery()->getResult(); + } + + /** + * Gets the name of the logging level. + * + * @param int $level + * @return string + */ + public static function getLevelName($level) + { + if (!isset(static::$levels[$level])) { + throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels))); + } + + return static::$levels[$level]; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @param mixed $name + * @return LoggerService + */ + public function setName($name) + { + $this->name = $name; + return $this; + } +} \ No newline at end of file