Apollo-server: docs: logging with a custom logger

Created on 6 May 2020  路  7Comments  路  Source: apollographql/apollo-server

https://github.com/apollographql/apollo-server/blob/80a12d89ea1ae9a0892f4a81d9213eddf95ca965/packages/apollo-server-types/src/index.ts#L114-L121

Edit by @abernix: Documentation for the work introduced in #3894 would be beneficial. PR welcomed.

documentation 馃檹 help-wanted

All 7 comments

3894 has more details but you can pass a logger preference to ApolloServer, ApolloGateway (and other, more granular interfaces, like as an option to ApolloEngineReporting).

By default, it uses console, but a custom Logger you pass in (on the logger property) merely needs to implement the levels provided on that Logger interface you linked to (i.e. warn, debug, info, and error).

3894 has more details but you can pass a logger preference to ApolloServer, ApolloGateway (and other, more granular interfaces, like as an option to ApolloEngineReporting).

By default, it uses console, but a custom Logger you pass in (on the logger property) merely needs to implement the levels provided on that Logger interface you linked to (i.e. warn, debug, info, and error).

Can you share an example

const server = new ApolloServer({
  logger: console,
});
const server = new ApolloServer({
  logger: console,
});

I tried adding options like warn, debug like the examples and documentation but its not working

Hello, any news on that ?

I would like some more examples to understand how to use a custom logger.

Hello, @abernix any update on how to use options like warn, debug, etc.

I wrote a small example by adjusting the sample Apollo Server implementation within the README (https://github.com/apollographql/apollo-server#installation-standalone).

The sample below will utilize a custom logger (only the debug function) when Apollo Server starts to fulfill a GraphQL request.

const { ApolloServer, gql } = require('apollo-server');

// The GraphQL schema
const typeDefs = gql`
  type Query {
    "A simple type for getting started!"
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const customLogger = {
  ...console,
  debug: (msg) => {
    const timestamp = new Date().toISOString();
    console.debug(`[${timestamp}] [DEBUG]: ${msg}`);
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  logger: customLogger,
  debug: true, // required to output all log levels
  plugins: [
    {
      requestDidStart({ logger }) {
        logger.debug('received request');
      },
    }
  ],
});

server.listen().then(({ url }) => {
  console.log(`馃殌 Server ready at ${url}`);
});

To run the snippet above:

  1. Paste the snippet into a file called index.js
  2. Run npm init -y && npm install apollo-server graphql && node index.js
  3. Execute the hello query (available in the playground on http://localhost:4000)

I utilized a plugin to demonstrate the availability of the logger on the GraphQLRequestContext available to plugins. The documentation for the logger attribute can be found at: https://www.apollographql.com/docs/apollo-server/api/apollo-server/#apolloserver

I hope this helps.

Was this page helpful?
0 / 5 - 0 ratings