Skip to content

Replace normalizer with message formatter #29

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

Merged
merged 1 commit into from
Dec 25, 2015
Merged

Conversation

sagikazarmark
Copy link
Member

No description provided.

@sagikazarmark
Copy link
Member Author

ping @Nyholm

@Nyholm
Copy link
Member

Nyholm commented Dec 25, 2015

One thing I would like to have in my logs are the response body when the response fails. It often have some detailed error messages. That means I would create a formatter like this:

public function formatResponse(ResponseInterface $response)
    {
        return sprintf(
            '%s %s %s with body %s',
            $response->getStatusCode(),
            $response->getReasonPhrase(),
            $response->getProtocolVersion(),
            $response->getBody()->__toString()
        );
    }

In my error logs I would get a line like:

Error: "Bad request" with response: "400, Bad request 1.1 with body [Big and awfully long body. ]" when emitting request: "GET /something"

I think I would like the response to always be at the end of the log entry.

So make the log entry look like this:

 sprintf('Error: "%s" with when emitting request: "%s". This is the response %s', $exception->getMessage(), $this->formatter->formatRequest($request), $this->formatter->formatResponse($exception->getResponse())),

@sagikazarmark
Copy link
Member Author

You can. There is an interface in the message package which allows you to implement your own formatter.

@Nyholm
Copy link
Member

Nyholm commented Dec 25, 2015

Sorry, not done with the post... one sec

@Nyholm
Copy link
Member

Nyholm commented Dec 25, 2015

I was too fast on the keys and accidentally posted the comment before I was done writing it.

@joelwurtz
Copy link
Member

+1 With @Nyholm so having response or not does not change the beginning of the message :

With response :

sprintf('Error: "%s" when emitting request: "%s" with the response %s', $exception->getMessage(), $this->formatter->formatRequest($request), $this->formatter->formatResponse($exception->getResponse()))

Without :

sprintf('Error: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatRequest($request));

Is that ok with you @Nyholm ?

@Nyholm
Copy link
Member

Nyholm commented Dec 25, 2015

Is that ok with you @Nyholm ?

Yes, That is what I meant.

I think this PR is a great improvement. One could argue that you should be able to configure the complete log entry but I think that is overkill.

Good job @sagikazarmark!

@sagikazarmark
Copy link
Member Author

This PR is strictly about replacing Normalizer with formatter, also introduces an interface and possibility to use custom formatters. If you would like to change the log message, please do it in a separate PR/issue.

sagikazarmark added a commit that referenced this pull request Dec 25, 2015
Replace normalizer with message formatter
@sagikazarmark sagikazarmark merged commit 2f5651c into master Dec 25, 2015
@sagikazarmark sagikazarmark deleted the formatters branch December 25, 2015 15:52
@sagikazarmark
Copy link
Member Author

Thanks @Nyholm

@joelwurtz
Copy link
Member

We could always make the write of log entry / errors in decoupled protected function so someone can extend this class and change the message. But yeah another PR.

@sagikazarmark
Copy link
Member Author

I think this is the same case as with the error plugin: it is not that complicated that someone cannot write his/her own. We created the plugin system for this purpose after all. We agree on a common use case and provide that. If someone has different needs, it is not that hard to implement a custom plugin.

In HttplugBundle plugin services could even be replace with a custom one.

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