23

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.

yen
  • 1,769
  • 2
  • 15
  • 43

2 Answers2

30

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!

Marie Hoeger
  • 1,261
  • 9
  • 11
  • 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 '20 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

Brian Rosamilia
  • 1,448
  • 14
  • 24
  • 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 '23 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. – Brian Rosamilia Apr 01 '23 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 02 '23 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. – Brian Rosamilia Apr 02 '23 at 23:41
  • @iqoOopi more information on the downside of my library can be found here https://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! https://github.com/BrianRosamilia/azure-function-log-intercept/blob/master/__tests__/index.js – Brian Rosamilia Apr 02 '23 at 23:44
  • Hi @Brian, I'm a bit confused, let's assume the following case: 1. In my function App, I have functions A and B. 2. A will run for 5mins while B will run 1mins. 3. A will start at 9:00 AM, while B will start at 9:01 AM, so B will start while A is still running. In this case, Will A's log be put under B's name as the global console method will be rewritten by B? – iqoOopi Apr 03 '23 at 18:53
  • @iqoOopi Yes it will. But people who are used to console.log logging will likely be expecting interleaved logging anyway, and have mitigated it already by filtering by request id's, user id, or somesuch anyway. If you can use context, you should. If you can't you've likely mitigated what context fixes anyway. – Ryan Leach Aug 19 '23 at 19:31