Skip to content

Commit

Permalink
case-sensitive text match with param q
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebd committed Oct 9, 2023
1 parent 989f151 commit 045950f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ Some assumptions could be considered TODO items for future enhancement
* Log events are separated by a newline character
* REST response Content-Type is text/plain; charset=utf-8, regardless of the Accept request header
* All errors are returned to REST callers as HTTP status 500, even if these might be correctable by the caller
* LRU result caching is not implemented

## Endpoints

* `GET /api/v1/logs` - Get list of all log files in `/var/log` that this service can read
* `GET /api/v1/logs/{log}` - Get the contents of the log file specified by `{log}` in `/var/log` (e.g. `/api/v1/logs/messages`)
<br/>Query Parameters:
* `n` - Number of lines to return (default: 25)
* `n` - Number of lines to return (default: 25, all: 0)
* `q` - Case sensitive fixed text filter to apply to each line (default: `""`), more performant than `r`
* `r` - Regex filter to apply to each line (default: `.*`)

Expand Down
4 changes: 3 additions & 1 deletion controller/rest/v1/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func GetLog(w http.ResponseWriter, r *http.Request, p httprouter.Params) {

logFilename := p.ByName("log")

textMatch := r.URL.Query().Get("q")

maxLines, _ := util.PositiveIntParamStrict(w, r, config.GetArguments().NumberOfLogLines, "n")
if maxLines > 0 {
logEvents, err := service.GetLog(config.LogDirectory, logFilename, maxLines)
logEvents, err := service.GetLog(config.LogDirectory, logFilename, textMatch, maxLines)

if err == nil {
// Reverse the slice - we want the most recent events first.
Expand Down
25 changes: 21 additions & 4 deletions service/getLog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
)

// GetLog returns the contents of a log file
func GetLog(directoryPath string, filename string, maxLines int) ([]string, error) {
// directoryPath: the path to the directory containing the log file
// filename: the name of the log file
// textMatch: a string to search for in the log file (case sensitive)
// maxLines: the maximum number of lines to return
func GetLog(directoryPath string, filename string, textMatch string, maxLines int) ([]string, error) {
filepath := strings.Join([]string{directoryPath, filename}, "/")

if !validLogFromName(directoryPath, filename) {
Expand All @@ -21,10 +25,23 @@ func GetLog(directoryPath string, filename string, maxLines int) ([]string, erro
}
result := strings.Split(string(byteSlice), "\n")

// Select lines that match the filters
if textMatch != "" {
filteredResult := make([]string, 0, len(result))
for i, line := range result {
if strings.Contains(line, textMatch) {
filteredResult = append(filteredResult, result[i])
}
}
result = filteredResult
}

// Restrict output to at most maxLines
endIndex := len(result) - 1
startIndex := max(0, endIndex-maxLines)
result = result[startIndex:endIndex]
if maxLines > 0 {
endIndex := len(result) - 1
startIndex := max(0, endIndex-maxLines)
result = result[startIndex:endIndex]
}

return result, nil
}
20 changes: 18 additions & 2 deletions service/getLog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@ import (
// Controllers may reverse the order as required by their clients.

func ExampleGetLog_logDir1_10KiB_log() {
lines, _ := GetLog("testdata/logDir1", "10KiB.log", 2)
lines, _ := GetLog("testdata/logDir1", "10KiB.log", "", 2)
fmt.Println(strings.Join(lines, "\n"))
// Output:
// 2023-10-06T15:18:24.408740Z|info |olaret esanus ivo hey enug tewos ebad it u tuge po elora e iwemat o
// 2023-10-06T15:18:24.408762Z|debug|tucev uho e u ela opif ce igodeto hudegor ivosu ehab eaunopi balohan tagused gicefas
}

func ExampleGetLog_logDir1_99lines_log() {
lines, _ := GetLog("testdata/logDir1", "99lines.log", "9 ", 0)
fmt.Println(strings.Join(lines, "\n"))
// Output:
// line 9 yz
// line 19 yz
// line 29 yz
// line 39 yz
// line 49 yz
// line 59 yz
// line 69 yz
// line 79 yz
// line 89 yz
// line 99 yz
}

func ExampleGetLog_logDir1_1line_log() {
// Requesting more lines than available returns all available lines.
lines, _ := GetLog("testdata/logDir1", "1line.log", 10)
lines, _ := GetLog("testdata/logDir1", "1line.log", "", 10)
fmt.Println(strings.Join(lines, "\n"))
// Output:
// 2023-10-06T15:18:24.406350Z|debug|toyeni vate riwehu ato ped afe ral bo h redi esohet sir moyireh nema lidef
Expand Down
2 changes: 1 addition & 1 deletion service/listLogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
func ExampleListLogs_logDir1() {
fmt.Println(ListLogs("testdata/logDir1"))
// Output:
// [10KiB.log 1line.log] <nil>
// [10KiB.log 1line.log 99lines.log] <nil>
}
90 changes: 90 additions & 0 deletions service/testdata/logDir1/99lines.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
line 1 abc
line 2 def
line 3 ghi
line 4 jkl
line 5 mno
line 6 pqr
line 7 stu
line 8 vwx
line 9 yz
line 11 abc
line 12 def
line 13 ghi
line 14 jkl
line 15 mno
line 16 pqr
line 17 stu
line 18 vwx
line 19 yz
line 21 abc
line 22 def
line 23 ghi
line 24 jkl
line 25 mno
line 26 pqr
line 27 stu
line 28 vwx
line 29 yz
line 31 abc
line 32 def
line 33 ghi
line 34 jkl
line 35 mno
line 36 pqr
line 37 stu
line 38 vwx
line 39 yz
line 41 abc
line 42 def
line 43 ghi
line 44 jkl
line 45 mno
line 46 pqr
line 47 stu
line 48 vwx
line 49 yz
line 51 abc
line 52 def
line 53 ghi
line 54 jkl
line 55 mno
line 56 pqr
line 57 stu
line 58 vwx
line 59 yz
line 61 abc
line 62 def
line 63 ghi
line 64 jkl
line 65 mno
line 66 pqr
line 67 stu
line 68 vwx
line 69 yz
line 71 abc
line 72 def
line 73 ghi
line 74 jkl
line 75 mno
line 76 pqr
line 77 stu
line 78 vwx
line 79 yz
line 81 abc
line 82 def
line 83 ghi
line 84 jkl
line 85 mno
line 86 pqr
line 87 stu
line 88 vwx
line 89 yz
line 91 abc
line 92 def
line 93 ghi
line 94 jkl
line 95 mno
line 96 pqr
line 97 stu
line 98 vwx
line 99 yz

0 comments on commit 045950f

Please sign in to comment.