Skip to content

Commit a25ec45

Browse files
committed
chore: add plugin for logging context
1 parent 6152fb3 commit a25ec45

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

.changeset/khaki-coats-do.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@callstack/byorg-core': patch
3+
---
4+
5+
core: add plugin for logging context

packages/core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export { VercelChatModelAdapter } from './ai/vercel.js';
2828

2929
export type { Command, CommandsPluginConfig } from './plugins/commands.js';
3030
export { createCommandsPlugin } from './plugins/commands.js';
31-
export { loggingPlugin } from './plugins/logging.js';
31+
export { loggingPlugin, contextLoggerBuilder } from './plugins/logging.js';
3232

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

packages/core/src/plugins/logging.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { ApplicationPlugin, MessageResponse } from '../index.js';
1+
import { inspect } from 'util';
2+
import { logger } from '@callstack/byorg-utils';
3+
import { ApplicationPlugin, MessageResponse, RequestContext } from '../index.js';
24

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

@@ -23,3 +25,27 @@ export const loggingPlugin: ApplicationPlugin = {
2325
}
2426
},
2527
};
28+
29+
export const contextLoggerBuilder = (fieldsToLog: (keyof RequestContext)[]): ApplicationPlugin => {
30+
return {
31+
name: 'context-logger',
32+
middleware: (context, next): Promise<MessageResponse> => {
33+
const toLog: Record<string, unknown> = {};
34+
35+
for (const field of fieldsToLog) {
36+
if (field in context) {
37+
toLog[field] = context[field];
38+
} else {
39+
logger.debug(`No ${field} in context.`);
40+
}
41+
}
42+
43+
if (Object.keys(toLog).length > 0) {
44+
console.log(inspect(toLog, false, null, true));
45+
}
46+
47+
// Continue middleware chain
48+
return next();
49+
},
50+
};
51+
};

0 commit comments

Comments
 (0)