docs: add section on logging under features

This commit is contained in:
Evans Hauser 2018-06-04 14:35:39 -07:00
parent 311c927c83
commit 3deceffe93
No known key found for this signature in database
GPG key ID: 88AF586817F52EEC
2 changed files with 52 additions and 0 deletions

View file

@ -17,6 +17,7 @@ sidebar_categories:
- features/resolvers
- features/mocking
- features/errors
- features/logging
- features/scalars-enums
- features/unions-interfaces
- features/directives

View file

@ -0,0 +1,51 @@
---
title: Logging
description: Ensuring Actions can be Recreated
---
Apollo Server provides two ways to log a server: by input,response, and errors or periodically throughout a request's lifecycle. Treating the GraphQL execution as a black box by logging the inputs and outputs of the system allows developers to diagnose issues quickly without being mired by lower level logs. Once a problem has been found at a high level, the lower level logs enable accurate tracing of how a request was handled.
## High Level Logging
To log the inputs, response, and request, Apollo Server provides three methods: `formatParams`, `formatError`, and `formatResponse`. This example uses `console.log` to record the information, servers can use other more sophisticated tools.
```js
const server = new ApolloServer({
typeDefs,
resolvers,
formatParams: params => {
console.log(params);
return params;
},
formatError: error => {
console.log(error);
return error;
},
formatResponse: response => {
console.log(response);
return response;
},
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
```
## Granular Logs
Apollo Server provides a `logFunction` option that receives the start and completion information for each major phase of GraphQL execution: parse, validate, and execute. Additionally, `logFunction` receives the information that initiates the request and response data. This example uses `console.log`:
```js
const server = new ApolloServer({
typeDefs,
resolvers,
logFunction: information => {
console.log(information)
},
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
```