https://github.com/loremipsum/action-logger-bundle
Symfony bundle to log custom actions and events with doctrine.
https://github.com/loremipsum/action-logger-bundle
Last synced: over 1 year ago
JSON representation
Symfony bundle to log custom actions and events with doctrine.
- Host: GitHub
- URL: https://github.com/loremipsum/action-logger-bundle
- Owner: loremipsum
- License: mit
- Created: 2018-12-27T08:30:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-10T13:42:51.000Z (over 6 years ago)
- Last Synced: 2025-01-16T15:29:37.886Z (over 1 year ago)
- Language: PHP
- Size: 45.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ActionLogger bundle
Symfony bundle to log custom actions and events with doctrine.
## Configuration
```yaml
# config/packages/lorem_ipsum_action_logger.yaml
lorem_ipsum_action_logger:
# mapping is used to store actions in the database without using the class name
mapping:
user.add: { class: App\Action\User\UserAddAction }
user.edit: { class: App\Action\User\UserEditAction, alias: 'user_edit' }
settings.edit: { class: App\Action\SettingsEditAction, alias: ['settings_edit', 'settings_update'] }
# entity_mapping is used to store action relations to entities in the database using the class name
entity_mapping:
user: App\Entity\User
settings: App\Entity\Settings
```
## Action example
Usage example:
A new user has been created. All you have to do is call `log` or `flashLog`
on our ActionLogger with the `UserAddAction` and the new `$user` entity.
```php
/** @var LoremIpsum\ActionLoggerBundle\ActionLogger $actionLogger **/
$actionLogger->log(new UserAddAction($user));
// Display action message as flash message
$actionLogger->flashLog(new UserAddAction($user), 'success');
```
And here is our `UserAddAction`:
```php
entity = $em->getRepository(User::class)->find($this->meta['id']);
}
private function getLink(RouteGeneratorInterface $router = null): array
{
if (! $router || ! $this->entity instanceof User) {
return ['%entity%', ['%entity%' => $this->meta['name']]];
}
return ["generate($this->entity)}\">%entity%", ['%entity%' => $this->entity]];
}
public function getMessage(RouteGeneratorInterface $router = null)
{
list($entity, $entities) = $this->getLink($router);
return ["User $entity added.", $entities];
}
public function getUserMessage(RouteGeneratorInterface $router = null)
{
list($user, $users) = $this->getUserLink($router);
list($entity, $entities) = $this->getLink($router);
return ["$user added user $entity.", $users, $entities];
}
}
```
## Event subscriber example
Log all action events with Psr\Log\LoggerInterface.
```php
logger = $logger;
}
public function onLog(ActionEvent $event)
{
$action = $event->getAction();
$this->logger->debug('[ActionLogListener] ' . $event->getActionFactory()->getAction(get_class($action)), $action->getUserMessage());
}
public static function getSubscribedEvents()
{
return [
ActionEvent::class => 'onLog',
];
}
}
```