Skip to content

Commit

Permalink
chore: add plugin for logging context
Browse files Browse the repository at this point in the history
  • Loading branch information
Q1w1N committed Dec 18, 2024
1 parent 8546256 commit d6f7b56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-coats-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@callstack/byorg-core': patch
---

core: add plugin for logging context
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export { VercelChatModelAdapter } from './ai/vercel.js';

export type { Command, CommandsPluginConfig } from './plugins/commands.js';
export { createCommandsPlugin } from './plugins/commands.js';
export { loggingPlugin } from './plugins/logging.js';
export { loggingPlugin, contextLoggerBuilder } from './plugins/logging.js';

export type { ApplicationTool } from './tools.js';

Expand Down
28 changes: 27 additions & 1 deletion packages/core/src/plugins/logging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ApplicationPlugin, MessageResponse } from '../index.js';
import { inspect } from 'util';
import { logger } from '@callstack/byorg-utils';
import { ApplicationPlugin, MessageResponse, RequestContext } from '../index.js';

const getFormattedNow = () => new Date().toISOString();

Expand All @@ -23,3 +25,27 @@ export const loggingPlugin: ApplicationPlugin = {
}
},
};

export const contextLoggerBuilder = (fieldsToLog: (keyof RequestContext)[]): ApplicationPlugin => {
return {
name: 'context-logger',
middleware: (context, next): Promise<MessageResponse> => {
const toLog: Record<string, unknown> = {};

for (const field of fieldsToLog) {
if (field in context) {
toLog[field] = context[field];
} else {
logger.debug(`No ${field} in context.`);
}
}

if (Object.keys(toLog).length > 0) {
console.log(inspect(toLog, false, null, true));
}

// Continue middleware chain
return next();
},
};
};

0 comments on commit d6f7b56

Please sign in to comment.