AzFunc 'context' object
olileger opened this issue · comments
Hi Yves
I haven't find any sample that show a usage of the Azure Functions "context" object.
This one is passed through the function as arg when you create a new HTTP Trigger Function : module.exports = function (context, req)
and allow you to .log()
information, very useful.
Could you tell me how could I get this object within the context of using your library please ?
Many thanks :)
Kind Regards
Olivier
Documentation related to the context object : https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#context-object
The context object is always the first parameter to a function and should always be included because it has methods such as context.done and context.log which are required to correctly use the runtime. You can name the object whatever you like (i.e. ctx or c).
Olivier
By reading the ExpressAdapter.js
file we could see the attachment of the context
object to req
& res
After testing it works but I prefer to get your confirmation.
Thx
Olivier
You're right.. I've added some documentation about this "feature" 😉
https://github.com/yvele/azure-function-express#log-via-context
any idea how to make it work with logger like morgan ?
Tried something like this:
const { Writable } = require('stream');
app.use((req, res, next) => {
process.stdout = new Writable({
write(chunk, encoding, callback) {
req.context.log(chunk.toString());
callback();
}
});
next();
});
app.use(morgan('combined'));
but didn't work
FWIW this seems to have worked:
const { Writable } = require('stream');
app.use((req, res, next) => {
const myStream = new Writable({
write(chunk, encoding, callback) {
req.context.log(chunk.toString());
callback();
}
});
morgan('combined', { stream: myStream })(req, res, next);
});