minimum_log_level = $minimum_log_level; $this->blacklist = $blacklist; $this->whitelist = $whitelist; $this->em = $em; $this->security = $security; } /** * Adds the given log entry to the Log, if the entry fullfills the global configured criterias. * The change will not be flushed yet. * * @return bool Returns true, if the event was added to log. */ public function log(AbstractLogEntry $logEntry): bool { $user = $this->security->getUser(); //If the user is not specified explicitly, set it to the current user if ((null === $user || $user instanceof User) && null === $logEntry->getUser()) { if (null === $user) { $repo = $this->em->getRepository(User::class); $user = $repo->getAnonymousUser(); } //If no anonymous user is available skip the log (needed for data fixtures) if (null === $user) { return false; } $logEntry->setUser($user); } if ($this->shouldBeAdded($logEntry)) { $this->em->persist($logEntry); return true; } return false; } /** * Adds the given log entry to the Log, if the entry fullfills the global configured criterias and flush afterwards. * * @return bool Returns true, if the event was added to log. */ public function logAndFlush(AbstractLogEntry $logEntry): bool { $tmp = $this->log($logEntry); $this->em->flush(); return $tmp; } public function shouldBeAdded( AbstractLogEntry $logEntry, ?int $minimum_log_level = null, ?array $blacklist = null, ?array $whitelist = null ): bool { //Apply the global settings, if nothing was specified $minimum_log_level = $minimum_log_level ?? $this->minimum_log_level; $blacklist = $blacklist ?? $this->blacklist; $whitelist = $whitelist ?? $this->whitelist; //Dont add the entry if it does not reach the minimum level if ($logEntry->getLevel() > $minimum_log_level) { return false; } //Check if the event type is black listed if (! empty($blacklist) && $this->isObjectClassInArray($logEntry, $blacklist)) { return false; } //Check for whitelisting if (! empty($whitelist) && ! $this->isObjectClassInArray($logEntry, $whitelist)) { return false; } // By default all things should be added return true; } /** * Check if the object type is given in the classes array. This also works for inherited types. * * @param object $object The object which should be checked * @param string[] $classes The list of class names that should be used for checking. * * @return bool */ protected function isObjectClassInArray(object $object, array $classes): bool { //Check if the class is directly in the classes array if (in_array(get_class($object), $classes, true)) { return true; } //Iterate over all classes and check for inheritance foreach ($classes as $class) { if (is_a($object, $class)) { return true; } } return false; } }