From 1fad5ed6c40b035e05d4af52b67cdc6fcfa015b1 Mon Sep 17 00:00:00 2001 From: Johnson Liang Date: Mon, 5 Jun 2023 14:13:28 +0800 Subject: [PATCH 1/2] feat: better organized request logs --- .env.sample | 2 +- src/index.js | 45 +++++++++++++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.env.sample b/.env.sample index 83a927c3..4f556659 100644 --- a/.env.sample +++ b/.env.sample @@ -104,5 +104,5 @@ GCS_MEDIA_FOLDER=media/ # OPENAI_API_KEY= -# When LOG_REQUESTS exists, it also shows incoming GraphQL request, variables, and resolved user info +# When LOG_REQUESTS exists, it also shows incoming GraphQL operation, variables, and resolved user info LOG_REQUESTS= diff --git a/src/index.js b/src/index.js index 84f593a1..9799eb81 100644 --- a/src/index.js +++ b/src/index.js @@ -84,6 +84,16 @@ router.get('/', ctx => { app.use(koaStatic(path.join(__dirname, '../static/'))); app.use(router.routes(), router.allowedMethods()); +const LOG_STR_LENGTH = 200; +/** + * For JSON.stringify in GraphQL logger + */ +function truncatingLogReplacer(key, value) { + if (typeof value !== 'string' || value.length < LOG_STR_LENGTH) return value; + + return value.slice(0, LOG_STR_LENGTH) + '...'; +} + export const apolloServer = new ApolloServer({ schema, introspection: true, // Allow introspection in production as well @@ -128,18 +138,29 @@ export const apolloServer = new ApolloServer({ : [ { /* istanbul ignore next */ - requestDidStart({ - request: { query, variables, operationName }, - context: { appId, userId, appUserId }, - }) { - console.log({ - query, - variables, - operationName, - appId, - appUserId, - userId, - }); + requestDidStart(...args) { + const [ + { + context: { userId, appUserId, appId }, + request: { operationName, query, variables, http }, + }, + ] = args; + + console.log( + JSON.stringify( + { + msg: 'GraphQL request did start', + operationName, + query, + variables, + userId, + appUserId, + appId, + headers: Object.fromEntries(http.headers.entries()), + }, + truncatingLogReplacer + ) + ); }, }, ], From 3db4125569d6fd6f269270a6337fa45688ffb4b8 Mon Sep 17 00:00:00 2001 From: Johnson Liang Date: Mon, 5 Jun 2023 14:27:51 +0800 Subject: [PATCH 2/2] fix(index): Ignore logger coverage --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index 9799eb81..3feca635 100644 --- a/src/index.js +++ b/src/index.js @@ -88,6 +88,7 @@ const LOG_STR_LENGTH = 200; /** * For JSON.stringify in GraphQL logger */ +/* istanbul ignore next */ function truncatingLogReplacer(key, value) { if (typeof value !== 'string' || value.length < LOG_STR_LENGTH) return value;