Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC/RFC] Simplify Client logger #1069

Merged
merged 2 commits into from
Apr 27, 2016
Merged

[POC/RFC] Simplify Client logger #1069

merged 2 commits into from
Apr 27, 2016

Conversation

merk
Copy link
Collaborator

@merk merk commented Mar 26, 2016

Not for merging (yet).

While working elsewhere, I've run into a few things that could be cleaned up and simplified. I've chosen 2 things I've noticed to send initial POC PRs to start a discussion about potential directions.

In this case, there is no point in having complicated configurations for logging in Elastica - lets move the heavy lifting of logging to the PSR logger and just pass one in at Client construction.

This could be implemented differently again: if we end up with a ClientInterface, we could wrap the Client with a LoggingClient decorator instead. I realise this may lead to more boilerplate for simple projects but there are solutions to talk about for that if the direction is a good one for this project.

@merk merk changed the title [POC/RFQ] Simplify Client logger [POC/RFC] Simplify Client logger Mar 26, 2016
@ruflin
Copy link
Owner

ruflin commented Mar 27, 2016

Generally +1 on removing logging functionality form Elastica as this is not the responsibility of Elastica and with Psr/Log now a reasonable standard exist. As I don't really have experience with Psr logger two questions:

  • What happens when no logger is passed? No logs are created?
  • The current logger provided some Elastica specific magic. Should we keep this around but somewhere else? Or is this something that is not needed anyways?

It is important for me to keep starting a project with Elastica stays as simple as creating a new Elastica\Client with empty params for localhost (if possible).

@merk
Copy link
Collaborator Author

merk commented Mar 27, 2016

  • No logs are created
  • If there is a need to format logs in a specific way, we can provide an implementation of a LoggerInterface that wraps a real implementation like Monolog

@merk
Copy link
Collaborator Author

merk commented Mar 27, 2016

I've modified the ->debug call to provide additional context information.

Most LoggerInterface implementations will provide a way to format additional context information in whatever way the user desires. For example, the primary logger implementation used by Symfony provides formatters and processors to allow customisation of the log messages as they're written: https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md

Alternatively, per my second point we could provide something like what FOSElasticaBundle provides: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/Logger/ElasticaLogger.php (note that ours does more to provide debugging information in the Symfony profiler.)

@merk
Copy link
Collaborator Author

merk commented Mar 28, 2016

My initial attempt at adding more context wouldn't work. I've had another go. We now log a successful request/response, or a request/exception pair.

I've also removed the null condition on the $_logger - it is always a LoggerInterface. If one is not provided, it is an instance of NullLogger.

@@ -640,6 +647,14 @@ public function request($path, $method = Request::GET, $data = array(), array $q

return $this->request($path, $method, $data, $query);
}

$this->_logger->debug('Elastica Request', [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought: I'm not sure how the implementation of Elastica\Log will handle the context array if we have any unserializable object as part of the Request (from what it looks like, the Response would die before we get here if it had anything unserializable).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which cases would that happen? Not sure I can follow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a document had a reference to something that couldn't be serialized it could become a problem - except I suspect Elastica itself wouldn't work in that circumstance anyway.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would probably break quite a few things.

@merk
Copy link
Collaborator Author

merk commented Mar 28, 2016

The build failure looks like its unrelated to the PR.

@merk
Copy link
Collaborator Author

merk commented Mar 28, 2016

I'm ready for this to be merged if the change is agreeable. I believe I've covered most use cases and avoided any BC breaks.

@ruflin
Copy link
Owner

ruflin commented Apr 25, 2016

LGTM. Can you rebase this on top of master again?

*
* @throws Exception\RuntimeException
*/
protected function _log($context)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried here - this method could be overwritten by people extending \Elastica\Client. Its a BC break.

We should probably still do the logging in this method but deprecate it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added _log back with similar behaviour above. It's still not perfect: without adding a second parameter (which will cause php errors if someone has overwritten _log) we cant get the Response for logging details about the response.

I think its an okay trade-off, its simple to fix by adding the second parameter to the overwritten _log method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, we store it in Client::_lastResponse.

@codecov-io
Copy link

codecov-io commented Apr 26, 2016

Current coverage is 84.42%

Merging #1069 into master will increase coverage by +0.16%

@@           master   #1069   diff @@
=====================================
  Files         179     179          
  Lines        4756    4750     -6   
  Methods      1188    1186     -2   
  Branches        0       0          
=====================================
+ Hits         4006    4010     +4   
+ Misses        750     740    -10   
  Partials        0       0          
  1. 2 files (not in diff) in lib/Elastica were modified. more
    • Misses -3
    • Hits +1
  2. File .../Elastica/Client.php was modified. more
    • Misses +2
    • Partials 0
    • Hits -5

Sunburst

Powered by Codecov. Last updated by 149fef3

@merk merk force-pushed the remove-logger branch 2 times, most recently from 16c8bf4 to 9f6ce9a Compare April 26, 2016 09:08
@merk
Copy link
Collaborator Author

merk commented Apr 26, 2016

Rebased

@ruflin ruflin merged commit 3d041a4 into ruflin:master Apr 27, 2016
@ruflin
Copy link
Owner

ruflin commented Apr 27, 2016

Merged. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants