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.
  • Loading branch information
abernix committed Mar 16, 2020
1 parent 8a19ecc commit 780d7f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
31 changes: 21 additions & 10 deletions packages/apollo-gateway/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
8 changes: 8 additions & 0 deletions packages/apollo-server-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,11 @@ export type GraphQLExecutionResult = {
errors?: ReadonlyArray<GraphQLError>;
extensions?: Record<string, any>;
};

export type Logger = {
// Ordered from least-severe to most-severe.
debug(message?: any): void;
info(message?: any): void;
warn(message?: any): void;
error(message?: any): void;
}

0 comments on commit 780d7f4

Please sign in to comment.