Logging
Less than to read
Logging to the console is common tool to use when developing a web application. Not only does it aid with debugging, but also provides developers with real-time feedback, helps facilitate performance testing and can potentially to provide a deper understanding of application flow and behaviour. The Web Amendability model has been produced with this in mind, and provides developers the option to log key data out to the console.
Implementing logging
There are several ways to implement the use of logging.
Basic logging
- Within an action, use the
log
key to specify a message.
{
"log": "Starting currencyCheck action."
}
- This will output “Starting currencyCheck action.” to the console.
Logging dynamic data
To log out a dynamic value, particularly from the state, use the get
mechanism.
{
"log": {
"get": {
"state": "currency_id"
}
}
}
- This logs out the current value of
currency_id
from the current state.
Conditional logging
It is also possible to implement condition-based logging through the use of if
statements.
The Sage 200 Schema provides exists
and notexists
conditions.
{
"if": {
"notexists": {
"get": {
"state": "currency_id"
}
},
"then": [
{
"log": "currency id does not exist!"
}
]
}
}
In this example, the condition checks to determine the presence of the currency_id
in the state. If the id isn’t present, log out the message.
Event-driven logging
You can utilise events to determine when specific logging should occur, either by manually specifying it, or through the use of a defined action.
"events": [
{
"onLoad": [
{
"action": "currencyCheck"
}
]
}
]
In this example the currencyCheck
action (and it’s pre-defined logging) is executed when the page loads.
Define an Action
- Actions are listed under the actions field by their name.
- For instance, we have an action named currencyCheck in the provided schema, which you can see below:
{
"version": "1.0",
"pages": ["sales/new-customer"],
"uiExtensions": [
{
"extensionId": "AmendCustomerDetails-outer",
"panel": "right",
"controls": [
{
"Button": {
"title": "Financial Pod",
"size": "medium",
"children": [
{
"Textbox": {
"id": "financialPod1",
"label": "Bank details1"
}
}
]
}
}
]
}
],
"events": [
{
"onLoad": [
{
"action": "currencyCheck"
}
]
}
],
"actions": {
"currencyCheck": [
{
"log": "Starting currencyCheck action"
},
{
"log": {
"get": {
"state": "currency_id"
}
}
},
{
"if": {
"notexists": {
"get": {
"state": "currency_id"
}
},
"then": [
{
"log": "currency id does not exist 1"
}
],
"else": [
{
"log": "currency id does exist 2"
}
]
}
},
{
"log": {
"get": {
"state": "currency_id"
}
}
},
{
"log": "Done first if, on to second if now"
},
{
"if": {
"exists": {
"get": {
"state": "currency_id"
}
},
"then": [
{
"log": "currency id exists 3"
}
],
"else": [
{
"log": "currency id is not existing 4"
}
]
}
},
{
"log": {
"get": {
"state": "currency_id"
}
}
}
]
}
}