mirror of
https://github.com/vale981/apollo-server
synced 2025-03-05 09:41:40 -05:00
docs: add section on logging under features
This commit is contained in:
parent
311c927c83
commit
3deceffe93
2 changed files with 52 additions and 0 deletions
|
@ -17,6 +17,7 @@ sidebar_categories:
|
|||
- features/resolvers
|
||||
- features/mocking
|
||||
- features/errors
|
||||
- features/logging
|
||||
- features/scalars-enums
|
||||
- features/unions-interfaces
|
||||
- features/directives
|
||||
|
|
51
docs/source/features/logging.md
Normal file
51
docs/source/features/logging.md
Normal 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}`);
|
||||
});
|
||||
```
|
Loading…
Add table
Reference in a new issue