From 6b9d9a6a3b6b3cc410d5a089e13ce69a008ccb6d Mon Sep 17 00:00:00 2001 From: Joel Scheuner Date: Fri, 13 Oct 2023 16:31:46 +0200 Subject: [PATCH] Add custom LocalStack LogsEgressAPI using logCollector --- cmd/localstack/logs_egress_api.go | 31 +++++++++++++++++++++++++++++++ cmd/localstack/main.go | 8 +++++--- 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 cmd/localstack/logs_egress_api.go diff --git a/cmd/localstack/logs_egress_api.go b/cmd/localstack/logs_egress_api.go new file mode 100644 index 0000000..ec567d0 --- /dev/null +++ b/cmd/localstack/logs_egress_api.go @@ -0,0 +1,31 @@ +package main + +import ( + "io" + "os" +) + +// This LocalStack LogsEgressAPI builder allows to customize log capturing, in our case using the logCollector. + +type LocalStackLogsEgressAPI struct { + logCollector *LogCollector +} + +func NewLocalStackLogsEgressAPI(logCollector *LogCollector) *LocalStackLogsEgressAPI { + return &LocalStackLogsEgressAPI{ + logCollector: logCollector, + } +} + +// The interface StdLogsEgressAPI for the functions below is defined in the under cmd/localstack/logs_egress_api.go +// The default implementation is a NoOpLogsEgressAPI + +func (s *LocalStackLogsEgressAPI) GetExtensionSockets() (io.Writer, io.Writer, error) { + // os.Stderr can not be used for the stderrWriter because stderr is for internal logging (not customer visible). + return io.MultiWriter(s.logCollector, os.Stdout), io.MultiWriter(s.logCollector, os.Stdout), nil +} + +func (s *LocalStackLogsEgressAPI) GetRuntimeSockets() (io.Writer, io.Writer, error) { + // os.Stderr can not be used for the stderrWriter because stderr is for internal logging (not customer visible). + return io.MultiWriter(s.logCollector, os.Stdout), io.MultiWriter(s.logCollector, os.Stdout), nil +} diff --git a/cmd/localstack/main.go b/cmd/localstack/main.go index 3103487..1d09ae1 100644 --- a/cmd/localstack/main.go +++ b/cmd/localstack/main.go @@ -148,11 +148,12 @@ func main() { UserLogger().Debugln("Process running as non-root user.") } - logCollector := NewLogCollector() - // file watcher for hot-reloading fileWatcherContext, cancelFileWatcher := context.WithCancel(context.Background()) + logCollector := NewLogCollector() + localStackLogsEgressApi := NewLocalStackLogsEgressAPI(logCollector) + // build sandbox sandbox := rapidcore. NewSandboxBuilder(). @@ -164,7 +165,8 @@ func main() { stopDnsServer() }). SetExtensionsFlag(true). - SetInitCachingFlag(true) + SetInitCachingFlag(true). + SetLogsEgressAPI(localStackLogsEgressApi) // xray daemon endpoint := "http://" + lsOpts.LocalstackIP + ":" + lsOpts.EdgePort