Edit by @abernix: Documentation for the work introduced in #3894 would be beneficial. PR welcomed.
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
loggerpreference toApolloServer,ApolloGateway(and other, more granular interfaces, like as an option toApolloEngineReporting).By default, it uses
console, but a customLoggeryou pass in (on theloggerproperty) merely needs to implement the levels provided on thatLoggerinterface you linked to (i.e.warn,debug,info, anderror).
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:
index.jsnpm init -y && npm install apollo-server graphql && node index.jsI 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.