jorge07 / symfony-6-es-cqrs-boilerplate

Symfony 6 DDD ES CQRS backend boilerplate.
MIT License
1.07k stars 187 forks source link

feat: log ES requests to specific channel #158

Closed jon-ht closed 4 years ago

jon-ht commented 4 years ago

This is really helpfull for debug as some ES requests are processed asynchronously (like update from Messenger consumer).

Logs are dumped in var/logs/es_dev.log. This config only applies on dev environment and fallbacks to main logger if $esLogger is not configured.

This is based on https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels

Two kind of logger:

Logger configuration

Activated with $defaultConfig['logger'] = $esLogger;

[2020-05-26T19:31:15.735262+00:00] es.DEBUG: Request Body ["{\"type\":\"App.Domain.User.Event.UserWasCreated\",\"payload\":{\"uuid\":\"0b527786-b07a-4472-bbd0-985e1c695d08\",\"credentials\":{\"email\":\"foo@foo.com\",\"password\":\"$2y$12$8hdOOAy7KgcmUb0fBPu0WOfsHg8w8DNQ\\/73khUx5izhUgR4GEGzPe\"},\"created_at\":\"2020-05-26T19:30:55.903978+00:00\"},\"occurred_on\":\"2020-05-26T19:30:55.915850+00:00\"}"] []
[2020-05-26T19:31:15.739068+00:00] es.INFO: Request Success: {"method":"POST","uri":"http://elasticsearch/events/_doc","port":9200,"headers":{"Host":["elasticsearch"],"Content-Type":["application/json"],"Accept":["application/json"],"User-Agent":["elasticsearch-php/7.6.1 (Linux 4.19.76-linuxkit; PHP 7.4.3)"]},"HTTP code":201,"duration":0.254767} []
[2020-05-26T19:31:15.741196+00:00] es.DEBUG: Response [{"_index":"events","_type":"_doc","_id":"sKR3UnIBt3BSclW_suY3","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}] []

Tracer configuration

Activated with $defaultConfig['tracer'] = $esLogger;

[2020-05-26T19:31:15.742195+00:00] es.INFO: curl -XPOST 'http://elasticsearch/events/_doc?pretty=true' -d '{"type":"App.Domain.User.Event.UserWasCreated","payload":{"uuid":"0b527786-b07a-4472-bbd0-985e1c695d08","credentials":{"email":"foo@foo.com","password":"$2y$12$8hdOOAy7KgcmUb0fBPu0WOfsHg8w8DNQ\/73khUx5izhUgR4GEGzPe"},"created_at":"2020-05-26T19:30:55.903978+00:00"},"occurred_on":"2020-05-26T19:30:55.915850+00:00"}' [] []
[2020-05-26T19:31:15.743282+00:00] es.DEBUG: Response: {"response":{"_index":"events","_type":"_doc","_id":"sKR3UnIBt3BSclW_suY3","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1},"method":"POST","uri":"http://elasticsearch/events/_doc","HTTP code":201,"duration":0.254767} []
jorge07 commented 4 years ago

I see it very helpful too! Thx

jon-ht commented 4 years ago

Should I also remove private string $index; in ElasticRepository ? Index is available as method now, not passed in consutructor anymore

jorge07 commented 4 years ago

LGTM

jorge07 commented 4 years ago

I've a suggestion:

This repo is used also by lot of people that isn't familiar with some terms and concepts. In this case es channel for monolog even if in this context looks clear that stand for ElasticSearch in others, like when looking at monolog config, can end with a misunderstanding with Event Sourcing. I suggest a rename the channel to elasticsearch or whatever you think is specific enough.

What're your thoughts about it?

jon-ht commented 4 years ago

Totally make sense, I'll update channel name . And what about my question ?

Should I also remove private string $index; in ElasticRepository ? Index is available as method now, not passed in consutructor anymore

jorge07 commented 4 years ago

Remove it is fine, no longer needed 👍

jorge07 commented 4 years ago

Merged! Thanks a lot!