Skip to content

Commit 27de5ba

Browse files
dubeemdeuser
authored andcommitted
Account for variable length timestamps when stripping logs (#292)
* Account for variable length timestamps for log stripping * Use regex for stripped log extraction * Create activation logs strip method * Add unit test * Review refactor * Use map for unit test
1 parent d19adcd commit 27de5ba

3 files changed

Lines changed: 72 additions & 7 deletions

File tree

commands/activation.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ var activationLogsCmd = &cobra.Command{
202202
return werr
203203
}
204204

205-
printActivationLogs(activation.Logs)
205+
if !Flags.activation.strip {
206+
printActivationLogs(activation.Logs)
207+
} else {
208+
printStrippedActivationLogs(activation.Logs)
209+
}
210+
206211
return nil
207212
},
208213
}

commands/util.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ func makeDefaultHeader(collection interface{}) string {
225225
return defaultHeader
226226
}
227227

228+
func stripTimestamp(log string) (strippedLog string) {
229+
regex := regexp.MustCompile("[a-zA-Z0-9\\s]*(stdout|stderr):\\s(.*)")
230+
match := regex.FindStringSubmatch(log)
231+
232+
if len(match) > 2 && len(match[2]) > 0 {
233+
strippedLog = match[2]
234+
} else {
235+
strippedLog = log
236+
}
237+
238+
return strippedLog
239+
}
240+
228241
func printFullList(collection interface{}) {
229242
switch collection := collection.(type) {
230243
case []whisk.Action:
@@ -280,14 +293,15 @@ func printFullActivationList(activations []whisk.Activation) {
280293
}
281294
}
282295

283-
func printActivationLogs(logs []string) {
296+
func printStrippedActivationLogs(logs []string) {
284297
for _, log := range logs {
285-
if Flags.activation.strip {
286-
fmt.Printf("%s\n", log[39:])
287-
} else {
288-
fmt.Printf("%s\n", log)
289-
}
298+
fmt.Printf("%s\n", stripTimestamp(log))
299+
}
300+
}
290301

302+
func printActivationLogs(logs []string) {
303+
for _, log := range logs {
304+
fmt.Printf("%s\n", log)
291305
}
292306
}
293307

commands/util_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// +build unit
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership.
7+
* The ASF licenses this file to You under the Apache License, Version 2.0
8+
* (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package commands
21+
22+
import (
23+
"github.com/stretchr/testify/assert"
24+
"testing"
25+
)
26+
27+
func TestStripTimestamp(t *testing.T) {
28+
logs := map[string]string{
29+
"2018-05-02T19:33:32.829992819Z stdout: this is stdout stderr: this is still stdout": "this is stdout stderr: this is still stdout",
30+
"2018-05-02T19:33:32.829992819Z stderr: this is stderr stdout: this is still stderr": "this is stderr stdout: this is still stderr",
31+
"2018-05-02T19:33:32.89Z stdout: this is stdout": "this is stdout",
32+
"2018-05-02T19:33:32.89Z stderr: this is stderr": "this is stderr",
33+
"anything stdout: this is stdout": "this is stdout",
34+
"anything stderr: this is stderr": "this is stderr",
35+
"stdout: this is stdout": "this is stdout",
36+
"stderr: this is stderr": "this is stderr",
37+
"this is stdout": "this is stdout",
38+
"this is stderr": "this is stderr",
39+
"something": "something",
40+
"": ""}
41+
assert := assert.New(t)
42+
43+
for log, expected := range logs {
44+
assert.Equal(stripTimestamp(log), expected)
45+
}
46+
}

0 commit comments

Comments
 (0)