Skip to content

Commit

Permalink
enhance(logging): more clear key-value pairs (#8375)
Browse files Browse the repository at this point in the history
* enhance(logging): more clear key-value pairs

* Add prefix
  • Loading branch information
ardatan authored Feb 12, 2025
1 parent b243fc4 commit b44f8b7
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 58 deletions.
16 changes: 16 additions & 0 deletions .changeset/heavy-swans-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@graphql-mesh/merger-bare': patch
'@omnigraph/json-schema': patch
'@graphql-mesh/plugin-http-cache': patch
'@graphql-mesh/transport-grpc': patch
'@graphql-mesh/transport-rest': patch
'@graphql-mesh/runtime': patch
'@graphql-mesh/config': patch
'@graphql-mesh/types': patch
'@graphql-mesh/utils': patch
'@omnigraph/grpc': patch
'@graphql-mesh/compose-cli': patch
'@graphql-mesh/cli': patch
---

More clear key-value pairs in the logs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query SearchMoviesByCast {
query SearchMoviesByCastQuery {
exampleSearchMoviesByCast(input: { castName: "Tom Cruise" }) @stream {
name
year
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
subscription SearchMoviesByCast {
subscription SearchMoviesByCastSubscription {
exampleSearchMoviesByCast(input: { castName: "Tom Cruise" }) {
name
year
Expand Down
2 changes: 1 addition & 1 deletion packages/compose-cli/src/getComposedSchemaFromConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function getComposedSchemaFromConfig(config: MeshComposeCLIConfig,
const subgraphConfigsForComposition: SubgraphConfig[] = await Promise.all(
config.subgraphs.map(async subgraphCLIConfig => {
const { name: subgraphName, schema$ } = subgraphCLIConfig.sourceHandler(ctx);
const log = logger.child(`[${subgraphName}]`);
const log = logger.child({ subgraph: subgraphName });
log.debug(`Loading subgraph`);
let subgraphSchema: GraphQLSchema;
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/compose-cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function run({
program = program.allowUnknownOption().allowExcessArguments();
const opts = program.parse().opts();

const log = rootLog.child(` ${productName}`);
const log = rootLog.child(productName);

let importedConfig: MeshComposeCLIConfig;
if (!opts.configPath) {
Expand Down
10 changes: 6 additions & 4 deletions packages/legacy/cli/src/commands/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function serveMesh(
let diedWorkers = 0;
cluster.on('exit', (worker, code, signal) => {
if (!mainProcessKilled) {
logger.child(`Worker ${worker.id}`).error(`died with ${signal || code}. Restarting...`);
logger.child({ worker: worker.id }).error(`died with ${signal || code}. Restarting...`);
diedWorkers++;
if (diedWorkers === forkNum) {
logger.error('All workers died. Exiting...');
Expand All @@ -119,13 +119,15 @@ export async function serveMesh(
});
} else {
if (cluster.isWorker) {
logger.addPrefix?.(`Worker ${cluster.worker?.id}`);
logger.addPrefix?.({
worker: cluster.worker.id,
});
}
logger.info(`Starting GraphQL Mesh...`);

logger.info(`${cliParams.serveMessage}: ${serverUrl}`);
registerTerminateHandler(eventName => {
const eventLogger = logger.child(`${eventName} 💀`);
const eventLogger = logger.child({ terminateEvent: eventName });
eventLogger.info(`Destroying GraphQL Mesh...`);
getBuiltMesh()
.then(mesh => mesh.destroy())
Expand All @@ -149,7 +151,7 @@ export async function serveMesh(
});

registerTerminateHandler(eventName => {
const eventLogger = logger.child(`${eventName} 💀`);
const eventLogger = logger.child({ terminateEvent: eventName });
eventLogger.debug(`Stopping HTTP Server`);
mapMaybePromise(stop(), () => {
eventLogger.debug(`HTTP Server has been stopped`);
Expand Down
24 changes: 14 additions & 10 deletions packages/legacy/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function validateConfig(
filepath: string,
initialLoggerPrefix: string,
throwOnInvalidConfig = false,
logger = new DefaultLogger(initialLoggerPrefix).child('config'),
): asserts config is YamlConfig.Config {
if (jsonSchema) {
const ajv = new Ajv({
Expand All @@ -32,7 +33,7 @@ export function validateConfig(
);
throw aggregateError;
}
const logger = new DefaultLogger(initialLoggerPrefix).child('config');
logger = logger.child('config');
logger.warn('Configuration file is not valid!');
logger.warn("This is just a warning! It doesn't have any effects on runtime.");
ajv.errors.forEach(error => {
Expand All @@ -59,7 +60,7 @@ export async function findConfig(options?: {
const {
configName = 'mesh',
dir: configDir = '',
initialLoggerPrefix = '🕸️ Mesh',
initialLoggerPrefix = '',
importFn,
} = options || {};
const dir = path.isAbsolute(configDir) ? configDir : path.join(process.cwd(), configDir);
Expand Down Expand Up @@ -98,8 +99,9 @@ export async function findAndParseConfig(options?: ConfigProcessOptions) {
const {
configName = 'mesh',
dir: configDir = '',
initialLoggerPrefix = '🕸️ Mesh',
initialLoggerPrefix = '',
importFn,
logger = new DefaultLogger(initialLoggerPrefix),
...restOptions
} = options || {};
const dir = path.isAbsolute(configDir) ? configDir : path.join(process.cwd(), configDir);
Expand Down Expand Up @@ -132,15 +134,17 @@ export async function findAndParseConfig(options?: ConfigProcessOptions) {
}

const config = results.config;
validateConfig(config, results.filepath, initialLoggerPrefix, restOptions.throwOnInvalidConfig);
return processConfig(config, { dir, initialLoggerPrefix, importFn, ...restOptions });
validateConfig(
config,
results.filepath,
initialLoggerPrefix,
restOptions.throwOnInvalidConfig,
logger,
);
return processConfig(config, { dir, initialLoggerPrefix, importFn, logger, ...restOptions });
}

function customLoader(
ext: 'json' | 'yaml' | 'js',
importFn = include,
initialLoggerPrefix = '🕸️ Mesh',
) {
function customLoader(ext: 'json' | 'yaml' | 'js', importFn = include, initialLoggerPrefix = '') {
const logger = new DefaultLogger(initialLoggerPrefix).child('config');
function loader(filepath: string, content: string) {
if (process.env) {
Expand Down
5 changes: 4 additions & 1 deletion packages/legacy/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface GraphQLMeshCLIParams {

export const DEFAULT_CLI_PARAMS: GraphQLMeshCLIParams = {
commandName: 'mesh',
initialLoggerPrefix: '🕸️ Mesh',
initialLoggerPrefix: '',
configName: 'mesh',
artifactsDir: process.env.MESH_ARTIFACTS_DIRNAME || '.mesh',
serveMessage: 'Serving GraphQL Mesh',
Expand Down Expand Up @@ -303,6 +303,7 @@ export function createBuiltMeshHTTPHandler<TServerContext = {}>(): MeshHTTPHandl
configName: cliParams.configName,
additionalPackagePrefixes: cliParams.additionalPackagePrefixes,
initialLoggerPrefix: cliParams.initialLoggerPrefix,
logger,
});
logger = meshConfig.logger;

Expand Down Expand Up @@ -387,6 +388,7 @@ export function createBuiltMeshHTTPHandler<TServerContext = {}>(): MeshHTTPHandl
generateCode: true,
initialLoggerPrefix: cliParams.initialLoggerPrefix,
throwOnInvalidConfig: args.throwOnInvalidConfig,
logger,
});
logger = meshConfig.logger;

Expand Down Expand Up @@ -455,6 +457,7 @@ export function createBuiltMeshHTTPHandler<TServerContext = {}>(): MeshHTTPHandl
configName: cliParams.configName,
additionalPackagePrefixes: cliParams.additionalPackagePrefixes,
initialLoggerPrefix: cliParams.initialLoggerPrefix,
logger,
});
logger = meshConfig.logger;
const sourceIndex = meshConfig.sources.findIndex(
Expand Down
21 changes: 11 additions & 10 deletions packages/legacy/config/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { GetMeshOptions, MeshResolvedSource } from '@graphql-mesh/runtime';
import { FsStoreStorageAdapter, InMemoryStoreStorageAdapter, MeshStore } from '@graphql-mesh/store';
import type {
ImportFn,
Logger,
MeshHandlerLibrary,
MeshMergerLibrary,
MeshPlugin,
Expand Down Expand Up @@ -61,6 +62,7 @@ export type ConfigProcessOptions = {
generateCode?: boolean;
initialLoggerPrefix?: string;
throwOnInvalidConfig?: boolean;
logger?: Logger;
};

export type ProcessedConfig = GetMeshOptions & {
Expand Down Expand Up @@ -218,7 +220,7 @@ export async function processConfig(
cache,
pubsub,
store: sourcesStore.child(${JSON.stringify(source.name)}),
logger: logger.child(${JSON.stringify(source.name)}),
logger: logger.child({ source: ${JSON.stringify(source.name)} ),
importFn,
});`,
);
Expand All @@ -230,7 +232,7 @@ export async function processConfig(
cache,
pubsub,
store: sourcesStore.child(source.name),
logger: logger.child(source.name),
logger: logger.child({ source: source.name }),
importFn,
});
}),
Expand Down Expand Up @@ -374,7 +376,7 @@ export async function processConfig(
);
codes.add(`additionalEnvelopPlugins[${pluginIndex}] = await ${importName}({
...(${JSON.stringify(pluginConfig, null, 2)}),
logger: logger.child(${JSON.stringify(pluginName)}),
logger: logger.child({ plugin: ${JSON.stringify(pluginName)} }),
cache,
pubsub,
baseDir,
Expand All @@ -398,7 +400,7 @@ export async function processConfig(
codes.add(
`additionalEnvelopPlugins[${pluginIndex}] = await ${importName}({
...(${JSON.stringify(pluginConfig, null, 2)}),
logger: logger.child(${JSON.stringify(pluginName)}),
logger: logger.child({ plugin: ${JSON.stringify(pluginName)} }),
cache,
pubsub,
baseDir,
Expand All @@ -410,7 +412,7 @@ export async function processConfig(
}
return pluginFactory({
...pluginConfig,
logger: logger.child(pluginName),
logger: logger.child({ plugin: pluginName }),
cache,
pubsub,
baseDir: dir,
Expand Down Expand Up @@ -549,24 +551,23 @@ export async function processConfig(
additionalPrefixes: additionalPackagePrefixes,
});

const mergerLoggerPrefix = `${mergerName}Merger`;
if (options.generateCode) {
codes.add(
`const Merger = await import(${JSON.stringify(mergerModuleName)}).then(handleImport);`,
);
codes.add(`const merger = new Merger({
cache,
pubsub,
logger: logger.child(${JSON.stringify(mergerLoggerPrefix)}),
store: rootStore.child(${JSON.stringify(mergerLoggerPrefix)})
logger: logger.child({ merger: ${JSON.stringify(mergerName)} }),
store: rootStore.child(${JSON.stringify(mergerName)})
})`);
}

const merger = new Merger({
cache,
pubsub,
logger: logger.child(mergerLoggerPrefix),
store: rootStore.child(mergerLoggerPrefix),
logger: logger.child({ merger: mergerName }),
store: rootStore.child(mergerName),
});

if (config.additionalEnvelopPlugins) {
Expand Down
2 changes: 1 addition & 1 deletion packages/legacy/config/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export async function resolveLogger(
importFn: ImportFn,
cwd: string,
additionalPackagePrefixes: string[],
initialLoggerPrefix = '🕸️ Mesh',
initialLoggerPrefix = '',
): Promise<{
importCode: string;
code: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/legacy/mergers/bare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class BareMerger implements MeshMerger {
this.options.logger.debug(
`Switching to Stitching merger due to the transforms and additional resolvers`,
);
this.options.logger = this.options.logger.child('Stitching Proxy');
this.options.logger = this.options.logger.child({ proxy: 'stitching' });
this.stitchingMerger = this.stitchingMerger || new StitchingMerger(this.options);
return this.stitchingMerger.getUnifiedSchema(mergerCtx);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/legacy/runtime/src/get-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
const {
pubsub = new PubSub(),
cache,
logger = new DefaultLogger('🕸️ Mesh'),
logger = new DefaultLogger(''),
additionalEnvelopPlugins = [],
sources,
merger,
Expand Down Expand Up @@ -163,7 +163,7 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
await Promise.allSettled(
sources.map(async (apiSource, index) => {
const apiName = apiSource.name;
const sourceLogger = logger.child(apiName);
const sourceLogger = logger.child({ source: apiName });
sourceLogger.debug(`Generating the schema`);
try {
const source = await apiSource.handler.getMeshSource({
Expand Down
4 changes: 2 additions & 2 deletions packages/legacy/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ export type Logger = {
info: (...args: any[]) => void;
error: (...args: any[]) => void;
debug: (...lazyArgs: LazyLoggerMessage[]) => void;
child: (name: string) => Logger;
addPrefix?: (prefix: string) => Logger;
child: (name: string | Record<string, string | number>) => Logger;
addPrefix?: (prefix: string | Record<string, string | number>) => Logger;
};

export type SelectionSetParam = SelectionSetNode | DocumentNode | string | SelectionSetNode;
Expand Down
8 changes: 4 additions & 4 deletions packages/legacy/utils/src/in-context-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function getInContextSDK(
const inContextSDK: Record<string, any> = {};
const sourceMap = unifiedSchema.extensions.sourceMap as Map<RawSourceOutput, GraphQLSchema>;
for (const rawSource of rawSources) {
const rawSourceLogger = logger?.child(`${rawSource.name}`);
const rawSourceLogger = logger?.child({ source: rawSource.name });

const rawSourceContext: any = {
rawSource,
Expand Down Expand Up @@ -83,9 +83,9 @@ export function getInContextSDK(
const rootTypeFieldMap = rootType.getFields();
for (const fieldName in rootTypeFieldMap) {
const rootTypeField = rootTypeFieldMap[fieldName];
const inContextSdkLogger = rawSourceLogger?.child(
`InContextSDK.${rootType.name}.${fieldName}`,
);
const inContextSdkLogger = rawSourceLogger?.child({
inContextSdk: `${rootType.name}.${fieldName}`,
});
const namedReturnType = getNamedType(rootTypeField.type);
const shouldHaveSelectionSet = !isLeafType(namedReturnType);
rawSourceContext[rootType.name][fieldName] = ({
Expand Down
Loading

0 comments on commit b44f8b7

Please sign in to comment.