Skip to content

Commit

Permalink
Merge pull request #305 from cofacts/logging
Browse files Browse the repository at this point in the history
feat: better organized request logs
  • Loading branch information
MrOrz authored Jun 7, 2023
2 parents 9b3d696 + 3db4125 commit 045d786
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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=
46 changes: 34 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ 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
*/
/* istanbul ignore next */
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
Expand Down Expand Up @@ -128,18 +139,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
)
);
},
},
],
Expand Down

0 comments on commit 045d786

Please sign in to comment.