From 736f772f01d575135c3399db6063e5bba7917930 Mon Sep 17 00:00:00 2001 From: llogen Date: Mon, 11 Oct 2021 13:05:21 +0200 Subject: [PATCH 1/3] server/server.go: Added a flag to define a log file that saves the log seperatly. Signed-off-by: llogen --- cmds/contest/server/server.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cmds/contest/server/server.go b/cmds/contest/server/server.go index 7c7b4747..b34a2337 100644 --- a/cmds/contest/server/server.go +++ b/cmds/contest/server/server.go @@ -8,12 +8,14 @@ package server import ( "flag" "fmt" + "io" "os" "sync" "syscall" "time" "github.com/benbjohnson/clock" + "github.com/sirupsen/logrus" "github.com/linuxboot/contest/pkg/api" "github.com/linuxboot/contest/pkg/config" @@ -47,6 +49,7 @@ var ( flagTargetLocker *string flagInstanceTag *string flagLogLevel *string + flagLogFile *string flagPauseTimeout *time.Duration flagResumeJobs *bool flagTargetLockDuration *time.Duration @@ -61,6 +64,7 @@ func initFlags(cmd string) { flagTargetLocker = flagSet.String("targetLocker", "auto", "Target locker implementation to use, \"auto\" follows DBURI setting") flagInstanceTag = flagSet.String("instanceTag", "", "A tag for this instance. Server will only operate on jobs with this tag and will add this tag to the jobs it creates.") flagLogLevel = flagSet.String("logLevel", "debug", "A log level, possible values: debug, info, warning, error, panic, fatal") + flagLogFile = flagSet.String("logFile", "", "A path to a log file, where the logs will saved seperatly") flagPauseTimeout = flagSet.Duration("pauseTimeout", 0, "SIGINT/SIGTERM shutdown timeout (seconds), after which pause will be escalated to cancellaton; -1 - no escalation, 0 - do not pause, cancel immediately") flagResumeJobs = flagSet.Bool("resumeJobs", false, "Attempt to resume paused jobs") flagTargetLockDuration = flagSet.Duration("targetLockDuration", config.DefaultTargetLockDuration, @@ -153,6 +157,15 @@ func Main(pluginConfig *PluginConfig, cmd string, args []string, sigs <-chan os. log := ctx.Logger() defer cancel() + if *flagLogFile != "" { + f, err := os.OpenFile(*flagLogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) + if err != nil { + return err + } + defer f.Close() + log.OriginalLogger().(*logrus.Entry).Logger.SetOutput(io.MultiWriter(os.Stderr, f)) + } + pluginRegistry := pluginregistry.NewPluginRegistry(ctx) if err := registerPlugins(pluginRegistry, pluginConfig); err != nil { return fmt.Errorf("failed to register plugins: %w", err) From a566ef7bb1aff7e85705787ffc5d1aacb829a104 Mon Sep 17 00:00:00 2001 From: llogen Date: Mon, 11 Oct 2021 13:19:34 +0200 Subject: [PATCH 2/3] server/server.go: Added comment. Signed-off-by: llogen --- cmds/contest/server/server.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmds/contest/server/server.go b/cmds/contest/server/server.go index b34a2337..ee651b4c 100644 --- a/cmds/contest/server/server.go +++ b/cmds/contest/server/server.go @@ -157,6 +157,7 @@ func Main(pluginConfig *PluginConfig, cmd string, args []string, sigs <-chan os. log := ctx.Logger() defer cancel() + // set the log output to file if *flagLogFile != "" { f, err := os.OpenFile(*flagLogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) if err != nil { From c977da7c7a5775acb3c19323f566eae22e6dac24 Mon Sep 17 00:00:00 2001 From: llogen Date: Mon, 11 Oct 2021 13:05:21 +0200 Subject: [PATCH 3/3] server/server.go: Added a flag to define a log file that saves the log seperatly. Signed-off-by: llogen --- cmds/contest/server/server.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmds/contest/server/server.go b/cmds/contest/server/server.go index 8fcba46b..16f98506 100644 --- a/cmds/contest/server/server.go +++ b/cmds/contest/server/server.go @@ -8,12 +8,14 @@ package server import ( "flag" "fmt" + "io" "os" "sync" "syscall" "time" "github.com/benbjohnson/clock" + "github.com/sirupsen/logrus" "github.com/linuxboot/contest/pkg/api" "github.com/linuxboot/contest/pkg/config" @@ -47,6 +49,7 @@ var ( flagTargetLocker *string flagInstanceTag *string flagLogLevel *string + flagLogFile *string flagPauseTimeout *time.Duration flagResumeJobs *bool flagTargetLockDuration *time.Duration @@ -61,6 +64,7 @@ func initFlags(cmd string) { flagTargetLocker = flagSet.String("targetLocker", "auto", "Target locker implementation to use, \"auto\" follows DBURI setting") flagInstanceTag = flagSet.String("instanceTag", "", "A tag for this instance. Server will only operate on jobs with this tag and will add this tag to the jobs it creates.") flagLogLevel = flagSet.String("logLevel", "debug", "A log level, possible values: debug, info, warning, error, panic, fatal") + flagLogFile = flagSet.String("logFile", "", "A path to a log file, where the logs will saved seperatly") flagPauseTimeout = flagSet.Duration("pauseTimeout", 0, "SIGINT/SIGTERM shutdown timeout (seconds), after which pause will be escalated to cancellaton; -1 - no escalation, 0 - do not pause, cancel immediately") flagResumeJobs = flagSet.Bool("resumeJobs", false, "Attempt to resume paused jobs") flagTargetLockDuration = flagSet.Duration("targetLockDuration", config.DefaultTargetLockDuration, @@ -156,6 +160,16 @@ func Main(pluginConfig *PluginConfig, cmd string, args []string, sigs <-chan os. // Let's store storage engine in context storageEngineVault := storage.NewSimpleEngineVault() + // Store logs in external file if flag is set + if *flagLogFile != "" { + f, err := os.OpenFile(*flagLogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) + if err != nil { + return err + } + defer f.Close() + log.OriginalLogger().(*logrus.Entry).Logger.SetOutput(io.MultiWriter(os.Stderr, f)) + } + pluginRegistry := pluginregistry.NewPluginRegistry(ctx) if err := registerPlugins(pluginRegistry, pluginConfig); err != nil { return fmt.Errorf("failed to register plugins: %w", err)