Replies: 3 comments
-
You should be able to access it using something like. let providedLogger: Logger = client["logger"]; This is the built-in logger middleware implementation, so you should be able to reference it for what you want it to do. |
Beta Was this translation helpful? Give feedback.
-
Regarding using middleware, you can access the function's arguments (and therefore the logger) with the export const logMiddleware = new InngestMiddleware({
init() {
return {
onFunctionRun() {
return {
transformInput({ ctx }) {
// `ctx.logger` is the logger
},
};
},
};
},
name: "Logger ALS Middleware",
}); I'd probably then use the The likely cause of some confusion is that the |
Beta Was this translation helpful? Give feedback.
-
This didn't quite work as I expected that it would. This is my middleware: import {
inngestLoggerStorage,
type Logger,
} from '#app/asyncLocalStorages/inngestLoggerStorage';
import { InngestMiddleware } from 'inngest';
/**
* @see https://github.com/orgs/inngest/discussions/989
*/
export const logMiddleware = new InngestMiddleware({
init() {
let logger: Logger;
return {
onFunctionRun() {
return {
afterExecution() {
inngestLoggerStorage.exit(logger);
},
beforeExecution() {
inngestLoggerStorage.enterWith(logger);
},
transformInput({ ctx }) {
// @ts-expect-error – we know that ctx.logger is a Logger
logger = ctx.logger.logger.log as Logger;
},
};
},
};
},
name: 'Logger ALS Middleware',
}); and my export const log = (message: string, ...args: unknown[]) => {
const inngestLogger = inngestLoggerStorage.getStore();
if (inngestLogger) {
inngestLogger(message, ...args);
} else {
console.log('falling back to console.log', message, ...args);
}
}; This gives output:
Looks like the first part is correct (?). But then logger instance disappears? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I use a custom logging utility within my codebase. It would be pretty hard to rewrite the entire codebase to interop with Inngest logger. What I would like to do instead is inject the logger into the async context and check inside my logger if that logger is available.
I need this logger to be injected at a start of every function execution. What's the best way to do it?
I tried writing a middleware, but it does not seem like the logger is available:
Produces:
In principal this works, just need to figure out how to get a reference to the logger. CC @djfarrelly @IGassmann @jpwilliams
Beta Was this translation helpful? Give feedback.
All reactions