Article
· Mar 16, 2023 4m read

Tutorial: Improving code quality with the visual debug tool's color-coded logs

🐞🐛▶ There is a helpful tool which allows us to debug code visually with different colours.

20 gifs de Los Simpsons que representan a los aprovechados que se acercan a  los emprendedores

🧩👨‍💻 For example if we have a Business Process we can track where the code execution flow goes by writting several different variations of LOGS.

We could debug a Proxy/ WSCLIENTE; the class which mission is to connect our Operations with the external system, via different kinds of levels of LOGS, being a example as follows:

Method adjuntarInforme(..., documentoBinario As %Stream.FileBinary) [ Final, ProcedureBlock = 1, SoapBindingStyle = document, SoapBodyUse = literal, WebMethod ]
{
 Do ##class(Ens.Util.Log).LogAssert("","","Assert it enters method adjuntarInforme")
 Do ##class(Ens.Util.Log).LogInfo("","","Info documentoBinario.Read(): "_documentoBinario.Read())

 do documentoBinario.Rewind()
 Do ##class(Ens.Util.Log).LogAlert("","","Alert")
 Do ##class(Ens.Util.Log).LogWarning("","","Warning")
 Do ##class(Ens.Util.Log).LogError("","","Error")
 Do ##class(Ens.Util.Log).LogStatus("","","1")

[...]

 do (..WebMethod("adjuntarInforme",...)
}

 

As a result when we execute this method we will observe in the Visual Trace:

As you would expect each step writtes the intended output as being shown below:

Assert:

Info:

Alert:

Warning:

Error:

Status:

 

In addition we could use the previous different levels of logs inside any other class. For example we will use it in a Service as follows:

The code is the following one:

Method adjuntarInforme(..., documentoBinario As %Stream.FileBinary)
{
 $$$LOGASSERT("Assert it enters method adjuntarInforme")
 $$$LOGINFO("Info documentoBinario.Read(): "_documentoBinario.Read())

 do documentoBinario.Rewind()
 $$$LOGALERT("Alert")
 $$$LOGWARNING("Warning")
 $$$LOGERROR("Error")
 $$$LOGSTATUS("1")

[...]

}

As you would spot, we can use a shorthand version of each log, because of it does recognizes those macros in Services, Processes and Operation; however it does not work in WSCLIENTS.

Then we would observe in the Service's LOG:

This way we can observe easily each log's level with a different colour which is more visually friendly.

In addition, have you ever wondered how could we output the real error inside a <catchall> in a Business Process?

We can do it with the following code:

$$$LOGINFO($System.Status.GetErrorText(context.%LastError))

Inside a Process, we name the <catchall> Handle error

and into it we put:

  $$$LOGWARNING($System.Status.GetErrorText(context.%LastError))

 

So then, in the visual trace we observe:

 

The warning being spawned by the Process:

 

In conclusion, the visual debug tool using different colors for each log level is a valuable resource to track the code execution flow and identify errors efficiently. By using macros, such as $$$LOGASSERT, $$$LOGINFO, $$$LOGALERT, $$$LOGWARNING, $$$LOGERROR, and $$$LOGSTATUS, we can easily include logs in Services, Processes, and Operations. Furthermore, the tool allows us to output the real error inside a <catchall> in a Business Process, making it easier to identify and fix the problem. This tool is a great example of how technology can simplify the debugging process, saving developers time and effort.
 

There are several use cases for the visual debug tool using different colors for each log level. One of the most common scenarios is when developing complex applications that require integration with external systems. In this case, the tool can help us debug the code that connects our operations with the external system, as we can track the execution flow and identify errors easily.

Another use case is when working with multi-threaded applications. Debugging a multi-threaded application can be challenging, as different threads can execute simultaneously, making it difficult to identify the source of the problem. With the visual debug tool, we can add logs to each thread and distinguish them by color, making it easier to track the execution flow and identify the thread that caused the error.

The visual debug tool can also be useful when working with distributed systems, such as microservices architectures. In a distributed system, each microservice can have its own logs, making it difficult to track the execution flow and identify errors that span multiple services. With the visual debug tool, we can add logs to each service and distinguish them by color, making it easier to track the execution flow and identify errors that occur across multiple services.

In addition, the tool can be helpful when working with legacy code or code written by someone else. In this case, we may not have a clear understanding of the code's execution flow or where to add logs to identify errors. By using the visual debug tool, we can track the code's execution flow and identify the areas where we need to add logs to identify errors.

Finally, the visual debug tool can be useful when working with performance issues. By adding logs to different parts of the code, we can track the execution time and identify the areas that are causing performance issues. By using different colors for each log level, we can quickly identify the areas that require optimization.

To summarize, the visual debug tool using different colors for each log level can be useful in several scenarios, including developing complex applications, multi-threaded applications, distributed systems, legacy code, and performance issues. By using the tool, we can track the code's execution flow, identify errors, and optimize performance, making it a valuable resource for developers.

 

I hope it have helped you!

 

Thanks to my teammates in the Oficina de Interoperabilidad de Canarias, because of their help and advice.

Discussion (0)1
Log in or sign up to continue