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

docs: logging with a custom logger #4072

Closed
riginoommen opened this issue May 6, 2020 · 8 comments
Closed

docs: logging with a custom logger #4072

riginoommen opened this issue May 6, 2020 · 8 comments
Labels
🙏 help-wanted 📝 documentation Focuses on changes to the documentation (docs)

Comments

@riginoommen
Copy link

riginoommen commented May 6, 2020

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.

@abernix
Copy link
Member

abernix commented May 6, 2020

#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).

@abernix abernix changed the title Can you elaborate the usage of the Logger with apollo-server documentation docs: logging May 6, 2020
@abernix abernix changed the title docs: logging docs: logging with a custom logger May 6, 2020
@abernix abernix added 📝 documentation Focuses on changes to the documentation (docs) 🙏 help-wanted labels May 6, 2020
@riginoommen
Copy link
Author

#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

@abernix
Copy link
Member

abernix commented May 6, 2020

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

@abernix abernix closed this as completed May 6, 2020
@abernix abernix reopened this May 7, 2020
@riginoommen
Copy link
Author

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

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

@jess-ika
Copy link

jess-ika commented Aug 4, 2020

Hello, any news on that ?

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

@jabbar86
Copy link

jabbar86 commented Sep 1, 2020

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

@kubejm
Copy link
Contributor

kubejm commented Sep 11, 2020

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.

@glasser
Copy link
Member

glasser commented Oct 20, 2022

The logger option is documented in the API reference.

@glasser glasser closed this as completed Oct 20, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🙏 help-wanted 📝 documentation Focuses on changes to the documentation (docs)
Projects
None yet
Development

No branches or pull requests

6 participants