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.
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