Loggable
Writes messages about entity creation, changes and removal to a PSR-3 logger.
Entity
use Doctrine\ORM\Mapping as ORM;
use Ufo\DoctrineBehaviors\Contract\Entity\LoggableInterface;
use Ufo\DoctrineBehaviors\Model\Loggable\LoggableTrait;
#[ORM\Entity]
class Category implements LoggableInterface
{
use LoggableTrait;
#[ORM\Id, ORM\GeneratedValue, ORM\Column]
private ?int $id = null;
#[ORM\Column]
private string $title = '';
public function getId(): ?int
{
return $this->id;
}
}
The trait calls getId(), so the entity must have that method. The behavior adds no columns.
How it works
| Event | Message (info level) |
|---|---|
postPersist |
App\Entity\Category #1 created and the field changes |
postUpdate |
a line per changed field: App\Entity\Category #1 : property "title" changed from "Old" to "New" |
preRemove |
App\Entity\Category #1 removed |
Arrays in the message are replaced with an array, DateTime values are formatted as Y-m-d H:i:s.u.
The messages are built by getCreateLogMessage(), getUpdateLogMessage(array $changeSets) and getRemoveLogMessage() — override them in the entity to change the text.
Logger
LoggableEventSubscriber gets Psr\Log\LoggerInterface by autowiring — in an application with MonologBundle it is the app channel. Another channel or service:
# config/services.yaml
services:
Ufo\DoctrineBehaviors\EventSubscriber\LoggableEventSubscriber:
arguments:
$logger: '@monolog.logger.doctrine'