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

Use hooks for third party logging #228

Closed
cheng93 opened this issue Feb 27, 2024 · 4 comments · Fixed by #253
Closed

Use hooks for third party logging #228

cheng93 opened this issue Feb 27, 2024 · 4 comments · Fixed by #253
Assignees
Milestone

Comments

@cheng93
Copy link

cheng93 commented Feb 27, 2024

Investigative information

We would like to hook a third party logging library (pino, winston) in context.log, so that we can send it to another sink, that isn't application insights (in our case it's seq).

Is it possible to hook this in?

In .NET we can use ILogger / ILoggerFactory, but I'm unsure how to do this with node

@ejizba
Copy link
Contributor

ejizba commented Feb 27, 2024

Hooks are probably your best bet: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=javascript%2Cwindows%2Cazure-cli&pivots=nodejs-model-v4#hooks

I think something like this would work if you want to completely replace app insights:

app.hook.preInvocation((context) => {
    context.invocationContext.log = (...args) => {
        // your logic here
    };
});

If you want your seq stuff and app insights at the same time, it gets a little more clunky, but I think this would work:

app.hook.preInvocation((context) => {
    const newContext = {
        ...context.invocationContext,
        log: (...args) => {
            // your logic here

            context.invocationContext.log(...args);
        },
    };
    context.inputs.push(newContext);
});

Hooks are relatively new, so if there's anything missing please file a feature request

@ejizba ejizba changed the title Third party logging library Use hooks for third party logging Feb 27, 2024
@ejizba
Copy link
Contributor

ejizba commented Feb 27, 2024

I feel like this might be a common scenario, so I'll count this as its own feature request to come up with a "recommended" design for people and make sure its nice and easy

@ejizba
Copy link
Contributor

ejizba commented May 2, 2024

Just merged a PR that will be the new recommended way to do this. It relies on some changes in Azure which will take a month or two to roll out, so I'll update here once that finishes.

Here's what the new feature will look like:

const customLogger = new CustomLogger();

app.hook.log((logContext) => {
    customLogger.emit({
        body: logContext.message,
        severity: logContext.level,
    });
});

@ejizba
Copy link
Contributor

ejizba commented May 16, 2024

Fyi this is ready to be used. You need Host v4.34+ which should be done rolling out in Azure or core tools v4.0.5801 if running locally which was just released today. Oh and v4.5.0 of this package which we also just released today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants