Canvas comes with a suite of powerful logging tools built in, giving you full, customizable access to all relevant historical data, validation errors, error messages, etc.
There are two main ways to do logging in Canvas:
Block output logging, which can be customized with a search model
In-code logging using logger.log()
Block output is automatically logged, and can be further refined by creating a search model for any given block. This will allow you to select one or more fields from the output of the block that will be logged every time it runs.
This could for example be used to log employee numbers, order numbers, or similar - to know what was transferred, and when it happened.Log custom information using logger.log() in the block code. This allows you to add custom, conditional logging logic into the integration logic.
You could for example log validation errors, create conditional logging, error responses, etc.
In this article we will describe how to set up logging using both methods.
See also the related article for how to search the stored logs: Searching logs
Setting up a block Search Model
To set up a search model, select the block you want to log information for, and head to the Response tab.
Here you will find an icon to “Update Search Model” (you may need to scroll down inside the Response tab to see the icon):
When you click the icon you will be presented with a hierarchy of fields, from which you can select the relevant fields to log:
In this case we have only selected the “badgeNo” field, as we just need to log which employees were transferred.
We recommend that you only select the fields that you strictly need to be logged.
This will prevent wastefull use of storage space as the logs are collected, and limit privacy and security concerns for storage of personal/sensitive data.
Using logger.log()
The logger.log() function can be used in the code for any block. The function accepts both strings and objects, but objects will be stringified. Null and undefined values will be logged as empty strings.
It is a good idea to make sure that the strings you log have a unique format that allow for easy searching.
In the example above, one can easily search through the logs to find all ”Validation error”, or “Validation error, badgeNo 12345”, because those parts of the string are fixed.
If the errors were instead stored without a prefix, just using logger.log(err), it would not be possible to search for all validation errors for just one employee, and it would be difficult to tell the log type apart from other uses of logger.log().
See also the related article for how to search the stored logs: Searching logs