Solved

log errors and activites in SDK

  • 17 August 2023
  • 1 reply
  • 25 views

Badge +2

How do I log errors and activities while working with the Cognite SDK. I need to keep track of what the code is doing, diagnose issues, and improve the overall reliability of the application. If I am designing a function, how to handle all these logging in my code to better log errors and validations within the application. Please advise. 

Also share practical code samples from SDK

 

 

 

icon

Best answer by HaydenH 17 August 2023, 08:50

View original

1 reply

@eashwar11 That depends on what you mean by logging and “functions”. If you mean what you are doing locally, that is not what CDF is designed for, then I would just use the logging library in Python. However, if you mean if there is a way to capture the output of your function, then there are multiple ways.

First, if you have included print statements in your Cognite Functions code, then you can check upon them in the Fusion UI or with the SDK. Unfortunately as of writing, logging with the standard logging library in Python is not supported. First, if you arrive at the home page, and click on “Explore” in the top bar, the menu below should appear, of which you can then click on “Use Cognite Functions”, this will take you to the next screen.

I have anonymised where the Function name would be, but this is the screen you would be taken to if you click on the above option. As you can see, all of the Cognite Functions that are running in a schedule are visible, as well as Responses and Logs.

Responses are the “end result” of your Cognite Function from your handler. Most will set this to be a JSON/dictionary type response. Usually something like {“status”: “OK”, “response”: 200} for success and usually a 400 type error with a description of the issue. The button on the right of this will contain logs, which will capture anything that you have printed in your code. If you were looking to have some kind of alerting, then I think you might be interested in using the sendgrid library. If you wanted to see a visual indicator of failure, this will be reflected in the call status, which will show a failure in red instead of “Completed” as above. If you wanted this to happen based on certain outcomes, you could simply raise an error in your code:

if some_condition_that_needs_to_be_flagged:
raise SomeError("useful message")

This would then be reflected in the call status if the condition in the if statement is met. If you want to retrieve a function and its responses, then you can use the examples here. In your case, you might retrieve the calls from a given function, and so you could do:

client.functions.calls.list(function_external_id=my_xid)
# retrieving responses and logs
client.functions.calls.get_response(call_id=2, function_id=1)
client.functions.calls.get_logs(call_id=2, function_id=1)

Hope this helps. :)

P.S: You could make some helper logger class that just printed things based on a level flag and that would probably cover most use cases if the print does not suffice.

Reply