Fix exposing build system paths in logs

This commit is contained in:
Ingo Oppermann
2023-05-03 10:34:07 +02:00
parent 74110dae54
commit 8e2874a456
2 changed files with 20 additions and 12 deletions

View File

@@ -15,11 +15,11 @@ init:
## build: Build core (default) ## build: Build core (default)
build: build:
CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} go build -o core${BINSUFFIX} CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} go build -o core${BINSUFFIX} -trimpath
# github workflow workaround # github workflow workaround
build_linux: build_linux:
CGO_ENABLED=0 GOOS=linux GOARCH=${OSARCH} go build -o core CGO_ENABLED=0 GOOS=linux GOARCH=${OSARCH} go build -o core -trimpath
## swagger: Update swagger API documentation (requires github.com/swaggo/swag) ## swagger: Update swagger API documentation (requires github.com/swaggo/swag)
swagger: swagger:
@@ -69,19 +69,19 @@ lint:
## import: Build import binary ## import: Build import binary
import: import:
cd app/import && CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} go build -o ../../import -ldflags="-s -w" cd app/import && CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} go build -o ../../import -trimpath -ldflags="-s -w"
# github workflow workaround # github workflow workaround
import_linux: import_linux:
cd app/import && CGO_ENABLED=0 GOOS=linux GOARCH=${OSARCH} go build -o ../../import -ldflags="-s -w" cd app/import && CGO_ENABLED=0 GOOS=linux GOARCH=${OSARCH} go build -o ../../import -trimpath -ldflags="-s -w"
## ffmigrate: Build ffmpeg migration binary ## ffmigrate: Build ffmpeg migration binary
ffmigrate: ffmigrate:
cd app/ffmigrate && CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} go build -o ../../ffmigrate -ldflags="-s -w" cd app/ffmigrate && CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} go build -o ../../ffmigrate -trimpath -ldflags="-s -w"
# github workflow workaround # github workflow workaround
ffmigrate_linux: ffmigrate_linux:
cd app/ffmigrate && CGO_ENABLED=0 GOOS=linux GOARCH=${OSARCH} go build -o ../../ffmigrate -ldflags="-s -w" cd app/ffmigrate && CGO_ENABLED=0 GOOS=linux GOARCH=${OSARCH} go build -o ../../ffmigrate -trimpath -ldflags="-s -w"
## coverage: Generate code coverage analysis ## coverage: Generate code coverage analysis
coverage: coverage:
@@ -94,11 +94,11 @@ commit: vet fmt lint test build
## release: Build a release binary of core ## release: Build a release binary of core
release: release:
CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} go build -o core -ldflags="-s -w -X github.com/datarhei/core/v16/app.Commit=$(COMMIT) -X github.com/datarhei/core/v16/app.Branch=$(BRANCH) -X github.com/datarhei/core/v16/app.Build=$(BUILD)" CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} go build -o core -trimpath -ldflags="-s -w -X github.com/datarhei/core/v16/app.Commit=$(COMMIT) -X github.com/datarhei/core/v16/app.Branch=$(BRANCH) -X github.com/datarhei/core/v16/app.Build=$(BUILD)"
# github workflow workaround # github workflow workaround
release_linux: release_linux:
CGO_ENABLED=0 GOOS=linux GOARCH=${OSARCH} go build -o core -ldflags="-s -w -X github.com/datarhei/core/v16/app.Commit=$(COMMIT) -X github.com/datarhei/core/v16/app.Branch=$(BRANCH) -X github.com/datarhei/core/v16/app.Build=$(BUILD)" CGO_ENABLED=0 GOOS=linux GOARCH=${OSARCH} go build -o core -trimpath -ldflags="-s -w -X github.com/datarhei/core/v16/app.Commit=$(COMMIT) -X github.com/datarhei/core/v16/app.Branch=$(BRANCH) -X github.com/datarhei/core/v16/app.Build=$(BUILD)"
## docker: Build standard Docker image ## docker: Build standard Docker image
docker: docker:

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"runtime" "runtime"
"runtime/debug"
"strings" "strings"
"time" "time"
) )
@@ -101,8 +102,9 @@ type Logger interface {
// logger is an implementation of the Logger interface. // logger is an implementation of the Logger interface.
type logger struct { type logger struct {
output Writer output Writer
component string component string
modulePath string
} }
// New returns an implementation of the Logger interface. // New returns an implementation of the Logger interface.
@@ -111,13 +113,18 @@ func New(component string) Logger {
component: component, component: component,
} }
if info, ok := debug.ReadBuildInfo(); ok {
l.modulePath = info.Path
}
return l return l
} }
func (l *logger) clone() *logger { func (l *logger) clone() *logger {
clone := &logger{ clone := &logger{
output: l.output, output: l.output,
component: l.component, component: l.component,
modulePath: l.modulePath,
} }
return clone return clone
@@ -213,6 +220,7 @@ func (e *Event) WithComponent(component string) Logger {
func (e *Event) Log(format string, args ...interface{}) { func (e *Event) Log(format string, args ...interface{}) {
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
file = strings.TrimPrefix(file, e.logger.modulePath)
n := e.clone() n := e.clone()