You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by md...@apache.org on 2018/05/08 19:46:29 UTC

[incubator-openwhisk-cli] branch master updated: Account for variable length timestamps when stripping logs (#292)

This is an automated email from the ASF dual-hosted git repository.

mdeuser pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new 27de5ba  Account for variable length timestamps when stripping logs (#292)
27de5ba is described below

commit 27de5ba9990c65ce7c94b181ee246607f83f296f
Author: James Dubee <jw...@us.ibm.com>
AuthorDate: Tue May 8 15:46:26 2018 -0400

    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
---
 commands/activation.go |  7 ++++++-
 commands/util.go       | 26 ++++++++++++++++++++------
 commands/util_test.go  | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 7 deletions(-)

diff --git a/commands/activation.go b/commands/activation.go
index a360270..dcf1ffa 100644
--- a/commands/activation.go
+++ b/commands/activation.go
@@ -202,7 +202,12 @@ var activationLogsCmd = &cobra.Command{
 			return werr
 		}
 
-		printActivationLogs(activation.Logs)
+		if !Flags.activation.strip {
+			printActivationLogs(activation.Logs)
+		} else {
+			printStrippedActivationLogs(activation.Logs)
+		}
+
 		return nil
 	},
 }
diff --git a/commands/util.go b/commands/util.go
index 570a93e..a33ad23 100644
--- a/commands/util.go
+++ b/commands/util.go
@@ -225,6 +225,19 @@ func makeDefaultHeader(collection interface{}) string {
 	return defaultHeader
 }
 
+func stripTimestamp(log string) (strippedLog string) {
+	regex := regexp.MustCompile("[a-zA-Z0-9\\s]*(stdout|stderr):\\s(.*)")
+	match := regex.FindStringSubmatch(log)
+
+	if len(match) > 2 && len(match[2]) > 0 {
+		strippedLog = match[2]
+	} else {
+		strippedLog = log
+	}
+
+	return strippedLog
+}
+
 func printFullList(collection interface{}) {
 	switch collection := collection.(type) {
 	case []whisk.Action:
@@ -280,14 +293,15 @@ func printFullActivationList(activations []whisk.Activation) {
 	}
 }
 
-func printActivationLogs(logs []string) {
+func printStrippedActivationLogs(logs []string) {
 	for _, log := range logs {
-		if Flags.activation.strip {
-			fmt.Printf("%s\n", log[39:])
-		} else {
-			fmt.Printf("%s\n", log)
-		}
+		fmt.Printf("%s\n", stripTimestamp(log))
+	}
+}
 
+func printActivationLogs(logs []string) {
+	for _, log := range logs {
+		fmt.Printf("%s\n", log)
 	}
 }
 
diff --git a/commands/util_test.go b/commands/util_test.go
new file mode 100644
index 0000000..afdba35
--- /dev/null
+++ b/commands/util_test.go
@@ -0,0 +1,46 @@
+//// +build unit
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package commands
+
+import (
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestStripTimestamp(t *testing.T) {
+	logs := map[string]string{
+		"2018-05-02T19:33:32.829992819Z stdout: this is stdout stderr: this is still stdout": "this is stdout stderr: this is still stdout",
+		"2018-05-02T19:33:32.829992819Z stderr: this is stderr stdout: this is still stderr": "this is stderr stdout: this is still stderr",
+		"2018-05-02T19:33:32.89Z stdout: this is stdout":                                     "this is stdout",
+		"2018-05-02T19:33:32.89Z stderr: this is stderr":                                     "this is stderr",
+		"anything stdout: this is stdout":                                                    "this is stdout",
+		"anything stderr: this is stderr":                                                    "this is stderr",
+		"stdout: this is stdout":                                                             "this is stdout",
+		"stderr: this is stderr":                                                             "this is stderr",
+		"this is stdout":                                                                     "this is stdout",
+		"this is stderr":                                                                     "this is stderr",
+		"something":                                                                          "something",
+		"":                                                                                   ""}
+	assert := assert.New(t)
+
+	for log, expected := range logs {
+		assert.Equal(stripTimestamp(log), expected)
+	}
+}

-- 
To stop receiving notification emails like this one, please contact
mdeuser@apache.org.