watsonx Code Assistant

watsonx Code Assistant

AI-Generated Recommendations — Write High Quality Code Using Simple Language Prompts Regardless of Your Experience Level.

 View Only

“LogAnalyzer” - A WCA based log parser and debug assistant tool

By SACHIN SANT posted Tue April 01, 2025 06:50 PM

  

“LogAnalyzer” - A WCA based log parser and debug assistant tool

Data Analytical Tool for Parsing Data from CR/CI Frameworks and Logs from Linux on Power Based Server Systems.

Authors: Krishan Saraswat, Misbah Anjum N, Sachin Sant, Tasmiya Nalatwad

The “Log Analyzer” is a tool designed to streamline data parsing from Continuous Regression (CR) or Continuous Integration (CI) runs in Jenkins. Developed as part of the Linux on Power Functional Verification Test team, it aids in analysing test results from CR and CI runs, which are integral to verifying the functionality of Linux components. The tool summarizes failures, captures call traces during system crashes, and parses debug logs to identify commands executed in test cases, particularly for distro CR failures and errors

Introduction

The Linux on Power test team focuses on the quality of various supported Linux distribution releases on IBM Power architecture. Over the last several years, the team has built a strong automation strategy to reduce manual execution effort. Following are some examples of end-to-end automation framework

  • Linux on Power upstream Linux kernel continuous integration (CI) test framework focused on community-based Linux OS, I/O, and KVM code validation
  • End-to-end automated continuous regression (CR) test framework to facilitate optimal testing of multiple releases of Linux under different Firmware & Power Hardware environments.

Problem statement

These CI/CR frameworks are utilized to test various Linux functionalities, including OS, I/O, and KVM. As a part of a typical Linux release test cycle, several automated runs are scheduled. Each run contains about 25,000+ test-cases/variations.

Figure 1 – Test run result log generation and analysis

As captured in the above diagram, the process of analysing each run with multiple logs, identifying failures and debugging issues becomes highly time-consuming and effort-intensive (approx 30 hours). After every Jenkins based automated run, the next task would be to manually review system logs or console logs for any system crashes or call traces, detect test-case failures and errors, and eventually determine whether the failures occurred are product based actual failures or simply automation scripting related errors. This entire process of debugging the issues and then documenting/raising product defects in the respective databases is extremely tedious. Thus, there is a need for better automation that can accelerate the debugging process, reduce human errors, and free up developers/testers to focus on higher-priority tasks.

Proposed Solution

To address the challenge of manual log analysis and debugging, the team leveraged IBM watsonx Code Assistant (WCA), that streamlines the process of writing log-parsing scripts. Using this chat bot, we broke down the task into smaller micro-tasks, providing step-by-step guidance (via Prompt Engineering) to the AI assistant on what we need to achieve.

Solving Problem via WCA

Figure 2 – Automated log analysis using WCA generated python code

By leveraging WCA, we reduced the time required to develop the log parsing script from 7-8 hours to just 2-3 hours. This efficiency boost laid the foundation for the “LogAnalyzer project”, significantly improving log analysis and debugging automation.

IBM watsonx Code Assistant

watsonx Code Assistant (WCA) is IBM's AI-powered coding assistant designed to help developers write, debug, and optimize code efficiently. The chat bot provides coding assistance for popular programming languages like Java, Python, C++, JavaScript, Go, and more whilst seamlessly integrating into IDEs like Visual Studio Code and Eclipse, enabling developers to receive real-time code suggestions and recommendations based on natural language inputs

WCA Features

  1. Accelerating code generation: WCA can generate code suggestions, unit tests, and even entire functions or methods, allowing developers to work more efficiently and focus on higher-level tasks
  2. Code Explanation: WCA uses generative AI to analyze and summarize code, providing insights into its functionality and purpose. This feature enhances transparency and facilitates better collaboration among development team
  3. Documenting code: WCA can generate comment lines that document what the code does, making it easier for developers to understand and maintain their codebase.
  4. Enhancing code quality: WCA's ability to generate unit tests and explain code helps ensure that applications are accurate, reliable, and maintainable.

The WCA is integrated into VS Code as an extension, providing a unified environment where developers can interact with the AI chatbot, generate code, review documentation, and run unit tests-all within a single interface. Instead of manually writing explanations for different parts of the parsing script, the tool automatically provides clear descriptions of function inputs, outputs, and logic. Additionally, unit test generation ensures that each function is tested independently, catching potential issues early in the development cycle. This eliminates the need for extensive debugging and accelerates the verification process

Dynamic Data Parsing via JSON Based Input Functionality

After building the initial parsing script with WCA - AI assistance, the next step was to generalize it for multiple CI/CD or CR workflows and manual log parsing. Instead of hardcoding parsing rules into the script, we introduced a flexible data structure to define what needs to be extracted dynamically. By utilizing JSON as the configuration format, we allowed key-value pairs to represent parsing rules, supporting both single and multi-string lists. The integration of Python’s re library further optimized the parsing process, ensuring accurate data extraction from various console logs. Another challenge was extending the tool’s capabilities to support log parsing from multiple directories and file systems, whether on a local machine or a remote server. To address this, we added the ability to traverse nested directories and parse logs from multiple sources seamlessly.

Figure 3 – Dynamic data parsing via JSON based input

This enhancement allowed the Log Analyzer script to efficiently process large-scale logs, extracting relevant data from complex file structures. By combining JSON-based rule definitions with directory traversal and remote access support, we transformed the script into a dynamic, scalable parsing tool capable of extracting logs, strings, and structured data from diverse environments—all with a single JSON input. Thus, the debugging time for analysing failures and errors per automated run could be drastically reduced from 30+ hours to mere 1 hour.

WCA assisted LogAnalyzer tool development

IBM watsonx Code Assistant was extensively used during entire development and test phase. Following approach was used to achieve end goal.

  • Defining Prompts for Code Generation
  • Testing the Code via Unit Test Functionality
  • Using Code Explanation Feature to understand the Code generated by WCA
  • Using Code Documentation Feature to update the Code generated via doc strings
  • Generalizing the Parsing via JSON Feature Enhancement

Following video explains how WCA was used during development of LogAnalyzer tool.

Captured here are few examples of how WCA was used for code generation via prompt engineering, generate unit test cases, and document code.

Prompt to extract call traces

The following prompt was given to extract call traces related to hard-lockup seen in system console logs. 

Consider a console.log as input file. It contains number of lines. Write me a .py script which reads this input file and has a function called extract_hardlockup(file). This function reads the file line by line and extracts part of the file starting from the line which contains string "CPU *** self-detected hard LOCKUP" where *** symbolizes any character till the line which contains string "*** --- interrupt: ***". The thing to note is that the ending string should be the second occurrence of that string in the console.log. So basically, start from the first string to the second occurrence of the second string. Save this extracted info as brief_crash and display it on the screen.

Figure 4 – Prompt to extract call traces related to hard-lockups

This generated snippet could be reused to parse varied call traces via provided start and end patterns.

# Assisted by watsonx Code Assistant

def extract_hardlockup(file):

with open(file, 'r') as f:

lines = f.readlines()

start_index = -1

end_index = -1

for i, line in enumerate(lines):

if "CPU *** self-detected hard LOCKUP" in line:

start_index = i

if "*** --- interrupt: ***" in line:

end_index = i

Break

if start_index != -1 and end_index != -1:

extracted_lines = lines[start_index+1:end_index]

return ''.join(extracted_lines)

else:

return "No hard lockup found in the file."

file = "console.log"

brief_crash = extract_hardlockup(file)

print(brief_crash)

Prompt for code to identify test failures and errors

This prompt is used to parse Failures and Errors in console log having Test Cases Executed. 

Write a python program that parses a text-based log file for failed and errored tests using patterns "FAIL:" and "ERROR:", respectively, and writes the line containing the said patters to a result file with Markdown formatting for code.

The strings can also be changed to parse any kind of pattern building a base for general string parsing from the console log.

# Assisted by watsonx Code Assistant

def parse_log_file(input_file, output_file):

with open(input_file, 'r') as f:

lines = f.readlines()

failed_tests = []

errored_tests = []

for line in lines:

if "FAIL:" in line:

failed_tests.append(line)

elif "ERROR:" in line:

errored_tests.append(line)

with open(output_file, 'w') as f:

f.write("# Failed Tests\n\n")

for test in failed_tests:

f.write(f"```\n{test}\n```\n\n")

# Assisted by watsonx Code Assistant

f.write("\n# Errored Tests\n\n")

for test in errored_tests:

f.write(f"```\n{test}\n```\n\n")

input_file = "log.txt"

output_file = "results.md"

parse_log_file(input_file, output_file)

Prompt to generate unit test

This unit test case was generated to test the prompt output generated by WCA. Through this unit test case, we could tweak the code and the prompt to suite our use-cases. 

# Assisted by watsonx Code Assistant

import pytest

from unittest.mock import patch, mock_open

from your_module import parse_log_file

@patch('builtins.open', new_callable=mock_open, read_data='''

FAIL: Test 1

ERROR: Test 2

PASS: Test 3

ERROR: Test 4

''')

def test_parse_log_file(mock_file):

input_file = mock_file

output_file = 'output.txt'

      parse_log_file(input_file, output_file)

mock_file.assert_called_once_with(input_file, 'r')

mock_file.return_value.readlines.assert_called_once()

with open(output_file, 'r') as f:

content = f.read()

assert content == '# Failed Tests\n\n```\nFAIL: Test 1\n```\n\n' \

                       '# Errored Tests\n\n```\nERROR: Test 2\n```\n\n' \

                       '```\nERROR: Test 4\n```\n\n'

Example Code Explanation

The code explanation generated was used to understand and educate other team members on the workflow of the scripts. Based on the feedback, we could then change the prompt and generate new test cases respectively to suite our use-cases.

This Python script defines a function named parse_log_file that reads a log file, identifies lines containing "FAIL:" or "ERROR:", and writes them to separate sections in an output file.

Here's a breakdown of the code:

The function parse_log_file takes two arguments: input_file (the name of the log file to read) and output_file (the name of the output file to write).

The function opens the input file in read mode using a with statement, which ensures that the file is properly closed after its suite finishes, even if an exception is raised. The contents of the file are read into a list called lines.

Two empty lists, failed_tests and errored_tests, are initialized to store lines containing "FAIL:" and "ERROR:", respectively.

The function then iterates over each line in lines. If a line contains "FAIL:", it is appended to the failed_tests list. If a line contains "ERROR:", it is appended to the errored_tests list.

The function opens the output file in write mode using another with statement. This ensures that the file is properly closed after its suite finishes.

The function writes a header for "Failed Tests" to the output file, followed by each line from the failed_tests list, formatted with triple backticks (```) for code blocks.

The function writes a header for "Errored Tests" to the output file, followed by each line from the errored_tests list, also formatted with triple backticks (```) for code blocks.

In summary, this function reads a log file, separates lines containing "FAIL:" and "ERROR:", and writes them to an output file with appropriate headers and code block formatting. This can be useful for organizing and presenting test results in a clear and structured manner.

Example JSON Snippet

The following is a snippet from the global.json used in the final LogAnalyzer project. This snippet is used to parse system logs, test logs, data, etc, from single or multiple files by traversing directory structures. The logs can either be present locally or in a remote server where the script will ssh into the system to collect 0 to n number of logs and save it locally in the system.

{

"have-other-logs" : "yes/no",

"remote-logs" : "yes/no",

"logs" : {

        "<pattern-name-xyz>" : {

         "logs-directory" : "<xyz>",

         "single-file-name" : "<xyz>",

         "multi-file-name-pattern" : "<xyz>",

         "get-file-only-if-pattern-present" : [

             "<xyz>"

         ],

         "filter-lines-having-pattern" : [

              "<xyz>"

         ],

 

         "snippet" : {

             "start-pattern" : "<xyz>",

             "end-pattern" : "<xyz>",

             "stack-trace-pattern-start" : "<xyz>",

             "stack-trace-pattern-end" : "<xyz>"

         },

 

         "multi-snippet-per-log" : "yes/no",

         "multi-snippet" : {

"remove-duplicated" : "yes/no",

               "start-pattern" : "<xyz>",

               "end-pattern" : "<xyz>",

               "snippet-pattern": "<xyz>"

         }

     }

},

 

"remote-server-details" : {

     "host-ip" : "x.x.x.x",

     "user" : "<xyz>",

     "password" : "<xyz>"

}

}

Summary

By automating repetitive tasks and providing real-time recommendations, IBM watsonx Code Assistant enables developers to work more efficiently and focus on innovation. The team experienced it first hand -- by leveraging WCA, we reduced the time required to develop the log parsing script from 7-8 hours to just 2-3 hours. This efficiency boost laid the foundation for the “LogAnalyzer project”, significantly improving log analysis and debugging automation.

Overall, IBM watsonx Code Assistant aims to make coding faster, more efficient, and more collaborative, ultimately leading to increased productivity and better software development outcomes.

References

0 comments
33 views

Permalink