Slice Log Filter Challenge

GolangGolangBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("Golang")) -.-> go/DataTypesandStructuresGroup(["Data Types and Structures"]) go(("Golang")) -.-> go/FunctionsandControlFlowGroup(["Functions and Control Flow"]) go/DataTypesandStructuresGroup -.-> go/strings("Strings") go/DataTypesandStructuresGroup -.-> go/slices("Slices") go/FunctionsandControlFlowGroup -.-> go/for("For") subgraph Lab Skills go/strings -.-> lab-436686{{"Slice Log Filter Challenge"}} go/slices -.-> lab-436686{{"Slice Log Filter Challenge"}} go/for -.-> lab-436686{{"Slice Log Filter Challenge"}} end

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 filterLogs function 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.go file.
  • 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

  1. Iterate Logs: Use a for loop to check each log entry.
  2. Find Keyword: Look for the pattern "<KEYWORD>: " in the log.
  3. Extract Message: Use slicing to get the part after the keyword and ": ".
  4. Handle Empty Inputs: Return an empty slice if logs or keyword is empty.
  5. Append Results: Use append to add matching messages to a new slice.
โœจ Check Solution and Practice

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.