Introduction
In this challenge, you will develop a slice-based log filtering function that processes and extracts relevant log messages. Efficient log filtering is crucial in the fast-paced world of cybersecurity for identifying potential security threats. The goal is to implement the filterLogs function to filter log messages based on a given keyword, while ensuring the function is case-sensitive and handles empty slices and keywords gracefully. Additionally, the function should return only the message content, excluding timestamps or other metadata.
Implement Slice Log Filter Function
Develop a slice-based log filtering function to process and extract only the log message content (excluding timestamps) for a given keyword.
Tasks
- Implement the
filterLogsfunction that takes a slice of log entries and a keyword. - The function should return a new slice containing only the message content of log entries that include the specified keyword.
- Ensure the function is case-sensitive.
- Use Go slice operations to filter and process the log entries.
- Handle empty slices and empty keywords gracefully.
Requirements
- Create the implementation in the
~/project/logfilter.gofile. - Ensure the function does not modify the original slice and returns a new slice with processed results.
- Do not use any external libraries for filtering.
- Extract and return only the message content after the keyword (e.g.,
"Disk is almost full"instead of"2023-06-15 ERROR: Disk is almost full"). - Use string manipulation functions to process the logs.
Examples
Given the following log entries:
logs := []string{
"2023-06-15 ERROR: Security breach detected",
"2023-06-15 INFO: System startup completed",
"2023-06-15 WARN: Unauthorized access attempt",
"2023-06-15 ERROR: Disk is almost full",
"2023-06-15 INFO: User login",
"2023-06-15 WARN: System temperature too high",
}
go run logfilter.go
Output:
Error Logs: [Security breach detected Disk is almost full]
Warning Logs: [Unauthorized access attempt System temperature too high]
Info Logs: [System startup completed User login]
Hints
- Iterate Logs: Use a
forloop to check each log entry. - Find Keyword: Look for the pattern
"<KEYWORD>: "in the log. - Extract Message: Use slicing to get the part after the keyword and
": ". - Handle Empty Inputs: Return an empty slice if logs or keyword is empty.
- Append Results: Use
appendto add matching messages to a new slice.
Summary
In summary, this challenge requires you to implement a filterLogs function that efficiently processes and extracts relevant log messages (excluding metadata) from a slice of log strings based on a given keyword. The function should be case-sensitive, handle empty slices and keywords gracefully, and demonstrate proficiency in using Go slice and string manipulation operations to filter and return processed log messages.



