From 73b7a12eb710a1cf708c798b4f83dc45871cac29 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Mon, 17 Aug 2020 15:34:47 -0700 Subject: [PATCH] Update slog interfaces --- main.go | 42 +++++++++++++++---------------- slog/Readme.md | 32 ++++++++++++++---------- slog/slog.go | 68 ++++++++++++++++++++++++++++++++++---------------- 3 files changed, 86 insertions(+), 56 deletions(-) diff --git a/main.go b/main.go index 04832c1..c77d163 100644 --- a/main.go +++ b/main.go @@ -60,17 +60,17 @@ type ContainerStartJob struct { // Run is executed based on the ContainerStartJob Schedule and starts the // container func (job ContainerStartJob) Run() { - slog.Log("Starting: %s", job.name) + slog.Info("Starting: %s", job.name) // Check if container is already running containerJSON, err := job.client.ContainerInspect( job.context, job.containerID, ) - slog.PanicErr(err, "Could not get container details for job %s", job.name) + slog.PanicOnErr(err, "Could not get container details for job %s", job.name) if containerJSON.State.Running { - slog.LogWarning("Container is already running. Skipping %s", job.name) + slog.Warning("Container is already running. Skipping %s", job.name) return } @@ -80,24 +80,24 @@ func (job ContainerStartJob) Run() { job.containerID, dockerTypes.ContainerStartOptions{}, ) - slog.PanicErr(err, "Could not start container for jobb %s", job.name) + slog.PanicOnErr(err, "Could not start container for job %s", job.name) // Check results of job for check := true; check; check = containerJSON.State.Running { - slog.LogDebug("Still running %s", job.name) + slog.Debug("Still running %s", job.name) containerJSON, err = job.client.ContainerInspect( job.context, job.containerID, ) - slog.PanicErr(err, "Could not get container details for job %s", job.name) + slog.PanicOnErr(err, "Could not get container details for job %s", job.name) time.Sleep(1 * time.Second) } - slog.LogDebug("Done execing %s. %+v", job.name, containerJSON.State) + slog.Debug("Done execing %s. %+v", job.name, containerJSON.State) // Log exit code if failed if containerJSON.State.ExitCode != 0 { - slog.LogError( + slog.Error( "Exec job %s existed with code %d", job.name, containerJSON.State.ExitCode, @@ -132,15 +132,15 @@ type ContainerExecJob struct { // Run is executed based on the ContainerStartJob Schedule and starts the // container func (job ContainerExecJob) Run() { - slog.Log("Execing: %s", job.name) + slog.Info("Execing: %s", job.name) containerJSON, err := job.client.ContainerInspect( job.context, job.containerID, ) - slog.PanicErr(err, "Could not get container details for job %s", job.name) + slog.PanicOnErr(err, "Could not get container details for job %s", job.name) if !containerJSON.State.Running { - slog.LogWarning("Container not running. Skipping %s", job.name) + slog.Warning("Container not running. Skipping %s", job.name) return } @@ -151,19 +151,19 @@ func (job ContainerExecJob) Run() { Cmd: []string{"sh", "-c", strings.TrimSpace(job.shellCommand)}, }, ) - slog.PanicErr(err, "Could not create container exec job for %s", job.name) + slog.PanicOnErr(err, "Could not create container exec job for %s", job.name) err = job.client.ContainerExecStart( job.context, execID.ID, dockerTypes.ExecStartCheck{}, ) - slog.PanicErr(err, "Could not start container exec job for %s", job.name) + slog.PanicOnErr(err, "Could not start container exec job for %s", job.name) // Wait for job results execInfo := dockerTypes.ContainerExecInspect{Running: true} for execInfo.Running { - slog.LogDebug("Still execing %s", job.name) + slog.Debug("Still execing %s", job.name) execInfo, err = job.client.ContainerExecInspect( job.context, execID.ID, @@ -173,23 +173,23 @@ func (job ContainerExecJob) Run() { } time.Sleep(1 * time.Second) } - slog.LogDebug("Done execing %s. %+v", job.name, execInfo) + slog.Debug("Done execing %s. %+v", job.name, execInfo) // Log exit code if failed if execInfo.ExitCode != 0 { - slog.LogError("Exec job %s existed with code %d", job.name, execInfo.ExitCode) + slog.Error("Exec job %s existed with code %d", job.name, execInfo.ExitCode) } } // QueryScheduledJobs queries Docker for all containers with a schedule and // returns a list of ContainerCronJob records to be scheduled func QueryScheduledJobs(client ContainerClient) (jobs []ContainerCronJob) { - slog.LogDebug("Scanning containers for new schedules...") + slog.Debug("Scanning containers for new schedules...") containers, err := client.ContainerList( context.Background(), dockerTypes.ContainerListOptions{All: true}, ) - slog.PanicErr(err, "Failure querying docker containers") + slog.PanicOnErr(err, "Failure querying docker containers") for _, container := range containers { // Add start job @@ -261,7 +261,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerCronJob) { if _, ok := existingJobs[job.UniqueName()]; ok { // Job already exists, remove it from existing jobs so we don't // unschedule it later - slog.LogDebug("Job %s is already scheduled. Skipping", job.Name()) + slog.Debug("Job %s is already scheduled. Skipping", job.Name()) delete(existingJobs, job.UniqueName()) continue } @@ -269,7 +269,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerCronJob) { // Job doesn't exist yet, schedule it _, err := c.AddJob(job.Schedule(), job) if err == nil { - slog.Log( + slog.Info( "Scheduled %s (%s) with schedule '%s'\n", job.Name(), job.UniqueName(), @@ -277,7 +277,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerCronJob) { ) } else { // TODO: Track something for a healthcheck here - slog.LogError( + slog.Error( "Could not schedule %s (%s) with schedule '%s'. %v\n", job.Name(), job.UniqueName(), diff --git a/slog/Readme.md b/slog/Readme.md index 921d9f6..9f1f3ec 100644 --- a/slog/Readme.md +++ b/slog/Readme.md @@ -26,23 +26,29 @@ Also provided are a few simple methods for handling returned `error` variables, FUNCTIONS - func FatalErr(err error, format string, v ...interface{}) - FatalErr if error provided, will log out details of an error and exi + func Debug(format string, v ...interface{}) + Debug will log with a DEBUG prefix if DebugLevel is se + + func Error(format string, v ...interface{}) + Error will log with a ERROR prefix + + func FatalOnErr(err error, format string, v ...interface{}) + FatalOnErr if error provided, will log out details of an error and exi + + func Info(format string, v ...interface{}) + Info formats logs with an INFO prefix func Log(format string, v ...interface{}) Log formats logs directly to the main logger - func LogDebug(format string, v ...interface{}) - LogDebug will log with a DEBUG prefix if DebugLevel is se + func PanicOnErr(err error, format string, v ...interface{}) + PanicOnErr if error provided, will log out details of an error and exi - func LogError(format string, v ...interface{}) - LogError will log with a ERROR prefix + func SetFlags(flag int) + SetFlags allows changing the logger flags using flags found in `log` - func LogWarning(format string, v ...interface{}) - LogWarning will log with a WARNING prefix + func WarnOnErr(err error, format string, v ...interface{}) + WarnOnErr if error provided, will provide a warning if an error is provided - func PanicErr(err error, format string, v ...interface{}) - PanicErr if error provided, will log out details of an error and exi - - func WarnErr(err error, format string, v ...interface{}) - WarnErr if error provided, will provide a warning if an error is provided + func Warning(format string, v ...interface{}) + Warning will log with a WARNING prefix diff --git a/slog/slog.go b/slog/slog.go index e84bb1f..fdb5eac 100644 --- a/slog/slog.go +++ b/slog/slog.go @@ -12,53 +12,77 @@ var ( // DebugLevel indicates if we should log at the debug level DebugLevel = true - // Loggers for various levels - loggerDebug = log.New(os.Stderr, "DEBUG", log.LstdFlags) - loggerWarning = log.New(os.Stderr, "WARNING", log.LstdFlags) - loggerError = log.New(os.Stderr, "ERROR", log.LstdFlags) + // Default set of flags to use + defaultFlags = log.LstdFlags | log.Lmsgprefix + + // Loggers for various levels. Prefixes are padded to align logged content + loggerInfo = log.New(os.Stderr, "INFO ", defaultFlags) + loggerWarning = log.New(os.Stderr, "WARNING ", defaultFlags) + loggerError = log.New(os.Stderr, "ERROR ", defaultFlags) + loggerDebug = log.New(os.Stderr, "DEBUG ", defaultFlags) + + // Convenience for calling functions for all loggers in one method + allLoggers = []*log.Logger{ + loggerInfo, + loggerWarning, + loggerError, + loggerDebug, + } ) +// SetFlags allows changing the logger flags using flags found in `log` +func SetFlags(flag int) { + for _, logger := range allLoggers { + logger.SetFlags(flag) + } +} + // Log formats logs directly to the main logger func Log(format string, v ...interface{}) { log.Printf(format, v...) } -// LogDebug will log with a DEBUG prefix if DebugLevel is set -func LogDebug(format string, v ...interface{}) { +// Info formats logs with an INFO prefix +func Info(format string, v ...interface{}) { + loggerInfo.Printf(format, v...) +} + +// Warning will log with a WARNING prefix +func Warning(format string, v ...interface{}) { + loggerWarning.Printf(format, v...) +} + +// Error will log with a ERROR prefix +func Error(format string, v ...interface{}) { + loggerError.Printf(format, v...) +} + +// Debug will log with a DEBUG prefix if DebugLevel is set +func Debug(format string, v ...interface{}) { if !DebugLevel { return } loggerDebug.Printf(format, v...) } -// LogWarning will log with a WARNING prefix -func LogWarning(format string, v ...interface{}) { - loggerWarning.Printf(format, v...) -} - -// LogError will log with a ERROR prefix -func LogError(format string, v ...interface{}) { - loggerError.Printf(format, v...) -} - -// WarnErr if error provided, will provide a warning if an error is provided -func WarnErr(err error, format string, v ...interface{}) { +// WarnOnErr if error provided, will provide a warning if an error is provided +func WarnOnErr(err error, format string, v ...interface{}) { if err != nil { loggerWarning.Printf(format, v...) loggerError.Print(err) } } -// FatalErr if error provided, will log out details of an error and exit -func FatalErr(err error, format string, v ...interface{}) { +// FatalOnErr if error provided, will log out details of an error and exit +func FatalOnErr(err error, format string, v ...interface{}) { if err != nil { loggerError.Printf(format, v...) loggerError.Fatal(err) } } -// PanicErr if error provided, will log out details of an error and exit -func PanicErr(err error, format string, v ...interface{}) { +// PanicOnErr if error provided, will log out details of an error and exit +func PanicOnErr(err error, format string, v ...interface{}) { if err != nil { loggerError.Printf(format, v...) loggerError.Panic(err)