Skip to content

Commit

Permalink
gateway: Introduce Logger type and expose on logger config.
Browse files Browse the repository at this point in the history
This is a mutually exclusive option to `debug` which enables logging of our
default logger when one is not provided by the user.

Apollo-Orig-Commit-AS: apollographql/apollo-server@780d7f4
  • Loading branch information
abernix committed Mar 16, 2020
1 parent 499051c commit c6a8f8e
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions gateway-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
GraphQLExecutionResult,
GraphQLRequestContext,
Logger,
WithRequired,
} from 'apollo-server-types';
import { InMemoryLRUCache } from 'apollo-server-caching';
Expand All @@ -19,7 +20,7 @@ import {
} from 'graphql';
import { GraphQLSchemaValidationError } from 'apollo-graphql';
import { composeAndValidate, ServiceDefinition } from '@apollo/federation';
import loglevel, { Logger } from 'loglevel';
import loglevel from 'loglevel';
import loglevelDebug from 'loglevel-debug';

import { buildQueryPlan, buildOperationContext } from './buildQueryPlan';
Expand All @@ -46,7 +47,10 @@ import { fetch } from 'apollo-server-env';

export type ServiceEndpointDefinition = Pick<ServiceDefinition, 'name' | 'url'>;

type LoggerConfig = { debug?: boolean; }
// debug and logger are mutually exclusive options.
type LoggerConfig =
| { debug?: boolean; logger?: never; }
| { debug?: never; logger?: Logger; }

type GatewayConfigBase = {
// TODO: expose the query plan in a more flexible JSON format in the future
Expand Down Expand Up @@ -166,7 +170,7 @@ export class ApolloGateway implements GraphQLService {
public schema?: GraphQLSchema;
protected serviceMap: DataSourceCache = Object.create(null);
protected config: GatewayConfig;
protected logger: Logger;
private logger: Logger;
protected queryPlanStore?: InMemoryLRUCache<QueryPlan>;
private engineConfig: GraphQLServiceEngineConfig | undefined;
private pollingTimer?: NodeJS.Timer;
Expand Down Expand Up @@ -214,15 +218,22 @@ export class ApolloGateway implements GraphQLService {
...config,
};

// Setup logging facilities, scoped under the appropriate name.
this.logger = loglevel.getLogger(`apollo-gateway:`);
// Setup logging facilities
if (this.config.logger) {
this.logger = this.config.logger;
} else {
// If the user didn't prvoide their own logger, we'll initialize one.
const loglevelLogger = loglevel.getLogger(`apollo-gateway:`);

// Support DEBUG environment variable, à la https://npm.im/debug/.
loglevelDebug(this.logger);
// Support DEBUG environment variable, à la https://npm.im/debug/.
loglevelDebug(loglevelLogger);

// And also support the `debug` option, if it's truthy.
if (this.config.debug === true) {
this.logger.enableAll();
// And also support the `debug` option, if it's truthy.
if (this.config.debug === true) {
loglevelLogger.enableAll();
}

this.logger = loglevelLogger;
}

if (isLocalConfig(this.config)) {
Expand Down

0 comments on commit c6a8f8e

Please sign in to comment.