Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add plugin for logging context #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this optional, so that users skip the param to log the whole context

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));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modify logger to use inspect inside, detect if inspect is available.


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