From 99f67db39d5b136e6f9576059ec7c033dae46a63 Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Wed, 29 Nov 2017 12:56:01 -0500 Subject: [PATCH] Fix log location hook prefix stripping The log location hook was matching github.com/heptio/ark and stripping off that + 1 more char. This meant that github.com/heptio/ark-plugin-example/foo.go was being listed as plugin-example/foo.go instead of github.com/heptio/ark-plugin-example/foo.go. Signed-off-by: Andy Goldstein --- pkg/util/logging/log_location_hook.go | 17 ++++++++----- pkg/util/logging/log_location_hook_test.go | 28 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 pkg/util/logging/log_location_hook_test.go diff --git a/pkg/util/logging/log_location_hook.go b/pkg/util/logging/log_location_hook.go index 428627e46..1fce82c4b 100644 --- a/pkg/util/logging/log_location_hook.go +++ b/pkg/util/logging/log_location_hook.go @@ -28,7 +28,7 @@ const ( logSourceField = "logSource" logSourceSetMarkerField = "@logSourceSetBy" logrusPackage = "github.com/sirupsen/logrus" - arkPackage = "github.com/heptio/ark" + arkPackage = "github.com/heptio/ark/" arkPackageLen = len(arkPackage) ) @@ -90,11 +90,7 @@ func (h *LogLocationHook) Fire(entry *logrus.Entry) error { // we're in Ark server and not logging something that has the marker // set (which would indicate the log statement is coming from a plugin). if h.loggerName != "" || getLogSourceSetMarker(entry) == "" { - file := frame.File - if index := strings.Index(file, arkPackage); index != -1 { - // strip off .../github.com/heptio/ark/ so we just have pkg/... - file = frame.File[index+arkPackageLen+1:] - } + file := removeArkPackagePrefix(frame.File) entry.Data[logSourceField] = fmt.Sprintf("%s:%d", file, frame.Line) } @@ -123,3 +119,12 @@ func getLogSourceSetMarker(entry *logrus.Entry) string { return fmt.Sprintf("%s", nameVal) } + +func removeArkPackagePrefix(file string) string { + if index := strings.Index(file, arkPackage); index != -1 { + // strip off .../github.com/heptio/ark/ so we just have pkg/... + return file[index+arkPackageLen:] + } + + return file +} diff --git a/pkg/util/logging/log_location_hook_test.go b/pkg/util/logging/log_location_hook_test.go new file mode 100644 index 000000000..b132f73ac --- /dev/null +++ b/pkg/util/logging/log_location_hook_test.go @@ -0,0 +1,28 @@ +/* +Copyright 2017 the Heptio Ark contributors. + +Licensed 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 logging + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRemoveArkPackagePrefix(t *testing.T) { + assert.Equal(t, "pkg/foo.go", removeArkPackagePrefix("github.com/heptio/ark/pkg/foo.go")) + assert.Equal(t, "github.com/heptio/ark-plugin-example/foo.go", removeArkPackagePrefix("github.com/heptio/ark-plugin-example/foo.go")) +}