diff --git a/.changeset/khaki-coats-do.md b/.changeset/khaki-coats-do.md new file mode 100644 index 0000000..3add5cb --- /dev/null +++ b/.changeset/khaki-coats-do.md @@ -0,0 +1,5 @@ +--- +'@callstack/byorg-core': patch +--- + +core: add plugin for logging context diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 15b1ebf..b8401cd 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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'; diff --git a/packages/core/src/plugins/logging.ts b/packages/core/src/plugins/logging.ts index 38ddbde..d18fc99 100644 --- a/packages/core/src/plugins/logging.ts +++ b/packages/core/src/plugins/logging.ts @@ -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(); @@ -23,3 +25,27 @@ export const loggingPlugin: ApplicationPlugin = { } }, }; + +export const contextLoggerBuilder = (fieldsToLog: (keyof RequestContext)[]): ApplicationPlugin => { + return { + name: 'context-logger', + middleware: (context, next): Promise => { + const toLog: Record = {}; + + 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(); + }, + }; +};