Merge pull request #222 from ncdc/fix-log-location-hook

Fix log location hook prefix stripping
pull/217/head
Steve Kriss 2017-11-29 11:07:43 -08:00 committed by GitHub
commit 5b8562e73c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 6 deletions

View File

@ -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
}

View File

@ -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"))
}