Tutorial: Improving code quality with the visual debug tool's color-coded logs
Tags: programming, tutorial, debuging, beginners
Published: 2023-09-28 20:58:56

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

<img alt="20 gifs de Los Simpsons que representan a los aprovechados que se acercan a  los emprendedores" class="r48jcc pT0Scc iPVvYb" data-img="https://media.giphy.com/media/citBl9yPwnUOs/giphy.gif" data-lg="https://media.giphy.com/media/citBl9yPwnUOs/giphy.gif" data-md="https://media.giphy.com/media/citBl9yPwnUOs/giphy.gif" data-sm="https://media.giphy.com/media/citBl9yPwnUOs/giphy.gif" data-xs="https://media.giphy.com/media/citBl9yPwnUOs/giphy.gif" jsaction="VQAsE" jsname="kn3ccd" src="https://media.giphy.com/media/citBl9yPwnUOs/giphy.gif" /> 

🧩👨‍💻 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:

 <basefont name="Consolas"><font color="#000080">Method </font><font color="#000000">adjuntarInforme(</font><font color="#ff00ff"><font color="#000000">..., </font></font><font color="#ff00ff">documentoBinario </font><font color="#000080">As </font><font color="#000080">%Stream.FileBinary</font><font color="#000000">) [ </font><font color="#000080">Final</font><font color="#000000">, </font><font color="#000080">ProcedureBlock </font><font color="#000000">= 1, </font><font color="#000080">SoapBindingStyle </font><font color="#000000">= document, </font><font color="#000080">SoapBodyUse </font><font color="#000000">= literal, </font><font color="#000080">WebMethod </font><font color="#000000">]</font><br /><font color="#000000">{</font><br /><strong> <font color="#0000ff">Do </font><font color="#000080">##class</font><font color="#000000">(</font><font color="#008080">Ens.Util.Log</font><font color="#000000">).</font><font color="#0000ff">LogAssert</font><font color="#000000">(</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">"Assert it enters method adjuntarInforme"</font><font color="#000000">)</font><br /> <font color="#0000ff">Do </font><font color="#000080">##class</font><font color="#000000">(</font><font color="#008080">Ens.Util.Log</font><font color="#000000">).</font><font color="#0000ff">LogInfo</font><font color="#000000">(</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">"Info documentoBinario.Read(): "</font><font color="#000000">_</font><font color="#ff00ff">documentoBinario</font><font color="#000000">.</font><font color="#0000ff">Read</font><font color="#000000">())</font></strong><br /> <font color="#0000ff">do </font><font color="#ff00ff">documentoBinario</font><font color="#000000">.</font><font color="#0000ff">Rewind</font><font color="#000000">()</font><br /><strong> <font color="#0000ff">Do </font><font color="#000080">##class</font><font color="#000000">(</font><font color="#008080">Ens.Util.Log</font><font color="#000000">).</font><font color="#0000ff">LogAlert</font><font color="#000000">(</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">"Alert"</font><font color="#000000">)</font><br /> <font color="#0000ff">Do </font><font color="#000080">##class</font><font color="#000000">(</font><font color="#008080">Ens.Util.Log</font><font color="#000000">).</font><font color="#0000ff">LogWarning</font><font color="#000000">(</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">"Warning"</font><font color="#000000">)</font><br /> <font color="#0000ff">Do </font><font color="#000080">##class</font><font color="#000000">(</font><font color="#008080">Ens.Util.Log</font><font color="#000000">).</font><font color="#0000ff">LogError</font><font color="#000000">(</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">"Error"</font><font color="#000000">)</font><br /> <font color="#0000ff">Do </font><font color="#000080">##class</font><font color="#000000">(</font><font color="#008080">Ens.Util.Log</font><font color="#000000">).</font><font color="#0000ff">LogStatus</font><font color="#000000">(</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">""</font><font color="#000000">,</font><font color="#008000">"1"</font><font color="#000000">)</font></strong><br />[...]</p> 

<p>
<basefont name="Consolas">  <font color="#0000ff">do </font><font color="#000000">(..</font><font color="#0000ff">WebMethod</font><font color="#000000">(</font><font color="#008000">"adjuntarInforme"</font><font color="#000000">,...)</font><br />}</p> 
  
<p>
     
</p>
  
<p>
As a result when we execute this method we will observe in the Visual Trace:
</p>
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxtfpy8oo0pdd5e5ql2h.png)
  
<p>
As you would expect each step writtes the intended output as being shown below:
</p>
  
<p>
Assert:
</p>
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i97aw9lodh0ite5qfc8h.png)
  
<p>
Info:
</p>
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl1rzf59y0cos2tqhu97.png)
  
<p>
Alert:
</p>
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri7oy0wxn4lrutrempj8.png)
  
<p>
Warning:
</p>
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8phhnouhg5wsv5bmxu1.png)
  
<p>
Error:
</p>
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5ax7ssx2gtjr1wq2515.png)
  
<p>
Status:
</p>
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wt6cgh2kvzs4snugeoz2.png)
  
<p>
     
</p>
  
<p>
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:
</p>
  
<p>
The code is the following one:
</p>
  
<p>
<basefont name="Consolas"> <font color="#000080">Method </font><font color="#000000">adjuntarInforme(</font><font color="#ff00ff"><font color="#000000">...</font></font><font color="#000000">, </font><font color="#ff00ff">documentoBinario </font><font color="#000080">As </font><font color="#000080">%Stream.FileBinary)</font><br /><font color="#000000">{</font><br /><strong> <font color="#0000ff">$$$LOGASSERT</font><font color="#000000">(</font><font color="#008000">"Assert it enters method adjuntarInforme"</font><font color="#000000">)</font><br /> <font color="#0000ff">$$$LOGINFO</font><font color="#000000">(</font><font color="#008000">"Info documentoBinario.Read(): "</font><font color="#000000">_</font><font color="#ff00ff">documentoBinario</font><font color="#000000">.</font><font color="#0000ff">Read</font><font color="#000000">())</font></strong><br /> <font color="#0000ff">do </font><font color="#ff00ff">documentoBinario</font><font color="#000000">.</font><font color="#0000ff">Rewind</font><font color="#000000">()</font><br /><strong> <font color="#0000ff">$$$LOGALERT</font><font color="#000000">(</font><font color="#008000">"Alert"</font><font color="#000000">)</font><br /> <font color="#0000ff">$$$LOGWARNING</font><font color="#000000">(</font><font color="#008000">"Warning"</font><font color="#000000">)</font><br /> <font color="#0000ff">$$$LOGERROR</font><font color="#000000">(</font><font color="#008000">"Error"</font><font color="#000000">)</font><br /> <font color="#0000ff">$$$LOGSTATUS</font><font color="#000000">(</font><font color="#008000">"1"</font><font color="#000000">)</font></strong>&lt;/p> 
    
<p>
<font color="#000000">[...]</font>
</p>
    
<p>
<font color="#000000">}</font>
</p>
    
<p>
<font color="#000000">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.</font>
</p>
    
<p>
<font color="#000000">Then we would observe in the Service's LOG:</font>
</p>
    

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svili01ua3o7esdkm1xj.png)
    
<p>
This way we can observe easily each log's level with a different colour which is more visually friendly.
</p>
    
<p>
In addition, have you ever wondered how could we output the real error inside a &lt;catchall> in a Business Process?
</p>
    
<p>
We can do it with the following code:
</p>
    
<p>
      $$$LOGINFO(<strong>$System.Status.GetErrorText(context.%LastError)</strong>)
</p>
    
<p>
Inside a Process, we name the &lt;catchall> Handle error
</p>
    

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m42ddyhigong1s2s52cn.png)
    
<p>
and into it we put:
</p>
    
<p>
        $$$LOGWARNING(<strong>$System.Status.GetErrorText(context.%LastError)</strong>)
</p>
    
<p>
       
</p>
    
<p>
So then, in the visual trace we observe:
</p>
    

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ojf095zivjnna5b3db5.png)  
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhl8tgqk6hm6chwv1mqf.png)
<p>
The warning being spawned by the Process:
</p>
    

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3ic497sdvheuft6dhyt.png)
    
<p>
       
</p>
    
<p>
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 &lt;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.<br /> 
</p>
    
<p>
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.
</p>
    
<p>
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.
</p>
    
<p>
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.
</p>
    
<p>
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.
</p>
    
<p>
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.
</p>
    
<p>
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.
</p>
    
<p>
       
</p>
    
<p>
<strong>I hope it have helped you!</strong>
</p>
    
<p>
       
</p>
    
<p>
Thanks to my teammates in the Oficina de Interoperabilidad de Canarias, because of their help and advice.
</p></body></html>