24

In the nodejs examples for Azure function apps, there is a passed in context obj to the function and it is possible to do context.log in the same manner as you can with console.log to output messages.

What is the difference between these two methods and does it matter which you use? Thx.

0

2 Answers 2

31

This documentation should answer your question :)

In Functions, you use the context.log methods to write trace output to the console. In Functions v2.x, trace outputs using console.log are captured at the Function App level. This means that outputs from console.log are not tied to a specific function invocation, and hence aren't displayed in a specific function's logs. They do, however, propagate to Application Insights. In Functions v1.x, you cannot use console.log to write to the console.

Long story short - context.log is best!

1
  • Thanks. Note: sometimes after a successful run the context.log output doesn't show up in the Logs pane but refreshing the browser and re-running the function seems to help that.
    – jbobbins
    Mar 14, 2020 at 20:11
7

You can redirect console.log to context.log with my npm package so you dont have to pass down the context everywhere.

https://www.npmjs.com/package/azure-function-log-intercept

Source here if you just want to create your own module

https://github.com/BrianRosamilia/azure-function-log-intercept/blob/master/index.js

7
  • you will run into concurrency issue if you do that as azure functions share the global variable for all the functions under the same function app. Bascially means, if you have function A, B,C under one function app. If you redirect console.log then you might log B's logging under A or C...
    – iqoOopi
    Mar 31 at 17:52
  • @iqoOopi No major issues. There is one minor issue in my repo that will probably never be closed. Since writing this post, Microsoft themselves have used my package. It's safe to use. Apr 1 at 18:59
  • 1
    Thanks Brian, I’m curious why this is not a major issue, I read from MS docu that all functions under one function app will share process and global variables. Since the first step to use your library is to overwrite console.log obj, this will for sure cause racing problem if multi functions got triggered. Could you please explain me why is it safe?
    – iqoOopi
    Apr 2 at 19:07
  • @iqoOopi The console methods are being rewritten with a fresh function on every request. The only thing context.log is adding is a tracking identifier to let you track the end-to-end operations in a single request, if you need this, then yes you need to pass context everywhere and my library will not help you. However, this is not needed 99% of the time, especially if your requests are authenticated and your logs already identify the user, why would you need this proprietary tracing? Environment logging + structured logs and you can solve this easily in your own, repeatable, way. Apr 2 at 23:41
  • @iqoOopi more information on the downside of my library can be found here github.com/BrianRosamilia/azure-function-log-intercept/issues/4 but still people enjoy using it because no one wants to pass this context object throughout their entire codebase just to do logging. With regards to race conditions, it doesn't matter how many times you replace it with a new version as long as you are doing so with completely new values each time. My tests actually prove this too! github.com/BrianRosamilia/azure-function-log-intercept/blob/… Apr 2 at 23:44

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.