Download the PHP package locastic/loggastic without Composer

On this page you can find all versions of the php package locastic/loggastic. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package loggastic

Loggastic

Loggastic is made for tracking changes to your objects and their relations. Built on top of the Symfony framework, this library makes it easy to implement activity logs and store them on Elasticsearch for fast logs browsing.

Each tracked entity will have two indexes in the ElasticSearch:

  1. entity_name_activity_log -> saving all CRUD actions made on an object. And additionally saving before and after values for Edit actions.
  2. entity_name_current_data_tracker -> saving the latest object values used for comparing the changes made on Edit actions. This enables us to only store before and after values for modified fields in the activity_log index

System requirements

Elasticsearch version 7.17

Installation

composer require locastic/loggastic

Making your entity loggable

To make your entity loggable you need to do the following steps:

1. Add Loggable attribute to your entity

Add Locastic\Loggastic\Annotation\Loggable annotation to your entity and define serialization group name:

If you are using YAML:

Or XML:

2. Add serialization groups to the fields you want to log

Use the serialization group defined in the Loggable attribute config on the fields you want to track. You can add them to the relations and their fields too.

Example for logging fields from relations:

Note: You can also use annotations, xml and yaml! Examples coming soon.

3. Run commands for creating indexes in ElasticSearch

bin/console locastic:activity-logs:create-loggable-indexes

If you already have some data in the database, make sure to populate current data trackers with the following command:

bin/console locastic:activity-logs:populate-current-data-trackers

4. Displaying activity logs

Here are the examples for displaying activity logs in twig or as Api endpoints:

a) Displaying logs in Twig Locastic\Loggastic\DataProvider\ActivityLogProviderInterface service comes with a few useful methods for getting the activity logs data:

Use them to fetch data from the Elasticsearch and display it in your views. Example for displaying results in Twig:

The output would look something like this:

b) Displaying logs in the api endpoint using ApiPlatform In order to display Loggastic activity logs in an ApiPlatform endpoint, you can use ApiPlatforms ElasticSearch integration: https://api-platform.com/docs/core/elasticsearch/

Example for displaying activity logs in the ApiPlatform endpoint:

You can easily filter the results using the existing ApiPlatform filters: https://api-platform.com/docs/core/filters/. If you want to have different fields in the response, use serialization groups or even create a custom DTO.

Using *_activity_log index will return all activity logs. If you want to return only logs for one entity, use the exact index name. For example if you only want to show BlogPost entity logs, use blog_post_activity_log index in stateOptions config.

That's it!

Now you have the basic activity logs setup. Each time some change happens in the database for loggable entities, the activity log will be saved to the Elasticsearch.

Customization guide

Now that you have the basic setup, you can add some additional options and customize the library to your needs.

Configuration reference

Default configuration:

Saving logs async

Activity logs are using Symfony messenger component and are made to work in the async way too. If you want to make them async add the following messages to the messenger config:

Important note!

Only one consumer should be used per loggable object in order to not corrupt the data.

Optimising messenger for large amount of data

If you have a large amount of data, you might need more than one consumer to process the messages. In that case, you can configure different transports for the messages and use different consumer for each one. First step is to configure the transports. Here are the examples for AMQP and Doctrine transports for the activity_logs_default and activity_logs_product queues:

AMQP transport config example:

Doctrine transport config example:

Next step is to decorate ActivityLogDispatcher and add your own logic for dispatching messages to the transports. In this example we are sending all messages to the activity_logs_default transport except the ones for the Product entity which are sent to the activity_logs_product transport:

Depending on your project needs, you can have more transports and dispatch messages to them based on your own logic.

Handling relations

Sometimes you want to log changes made on some entity to some related entity. For example if you are using the Doctrine listener, you will only get the entity that actually had changes. Let's say you want to log Product changes which has a relation to the ProductVariant. On the edit form only fields from the ProductVariant were changed. Even if you run persist() method on Product, in this case only ProductVariant will be shown in the Doctrine listener. For this case you can use the Locastic\Loggastic\Loggable\LoggableChildInterface on ProductVariant:

Now each change made on ProductVariant will be logged to the Product.

Custom event listeners for saving activity logs

You can use Locastic\Loggastic\Logger\ActivityLoggerInterface service to save item changes to the Elasticsearch:

Depending on you application logic, you need to find the most fitting place to trigger logs saving.

In most cases that can be the Doctrine event listener which is triggered on each database change. Loggastic comes with a built-in Doctrine listener which is used by default. If you want to turn it off, you can do it by setting the loggastic.doctrine_listener_enabled config parameter to false:

If you are using ApiPlatform, one of the good options would be to use its POST_WRITE event: https://api-platform.com/docs/core/events/#custom-event-listeners

And for the Sylius projects you can use the Resource bundle events: https://docs.sylius.com/en/1.12/book/architecture/events.html

Save activity logs when no data changes were made

Sometimes you want to save activity logs even if no data changes were made. For example if you want to log order confirmation email was sent or some PDF was downloaded.

You can do that by setting the 3rd parameter to true:

Contribution

If you have idea on how to improve this bundle, feel free to contribute. If you have problems or you found some bugs, please open an issue.

Support

Want us to help you with this bundle or any ApiPlatform/Symfony project? Write us an email on [email protected]


All versions of loggastic with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
doctrine/annotations Version ^1.14
doctrine/doctrine-bundle Version ^2.8
doctrine/orm Version ^2.14
elasticsearch/elasticsearch Version ^7.1
symfony/dotenv Version 6.4.*
symfony/expression-language Version 6.4.*
symfony/framework-bundle Version ^6.4
symfony/messenger Version 6.4.*
symfony/property-info Version 6.4.*
symfony/runtime Version 6.4.*
symfony/serializer Version 6.4.*
symfony/validator Version 6.4.*
symfony/yaml Version 6.4.*
ext-simplexml Version *
symfony/security-bundle Version ^6.4
symfony/finder Version ^6.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package locastic/loggastic contains the following files

Loading the files please wait ....