diff --git a/main.go b/main.go index 9be0b41..9fef0bc 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( dockerTypes "github.com/docker/docker/api/types" dockerClient "github.com/docker/docker/client" + "github.com/iamthefij/dockron/slog" "github.com/robfig/cron/v3" "golang.org/x/net/context" ) @@ -67,10 +68,10 @@ func (job ContainerStartJob) Run() { job.context, job.containerID, ) - PanicErr(err, "Could not get container details for job %s", job.name) + slog.PanicErr(err, "Could not get container details for job %s", job.name) if containerJSON.State.Running { - LogWarning("Container is already running. Skipping %s", job.name) + slog.LogWarning("Container is already running. Skipping %s", job.name) return } @@ -80,24 +81,24 @@ func (job ContainerStartJob) Run() { job.containerID, dockerTypes.ContainerStartOptions{}, ) - PanicErr(err, "Could not start container for jobb %s", job.name) + slog.PanicErr(err, "Could not start container for jobb %s", job.name) // Check results of job for check := true; check; check = containerJSON.State.Running { - LogDebug("Still running %s", job.name) + slog.LogDebug("Still running %s", job.name) containerJSON, err = job.client.ContainerInspect( job.context, job.containerID, ) - PanicErr(err, "Could not get container details for job %s", job.name) + slog.PanicErr(err, "Could not get container details for job %s", job.name) time.Sleep(1 * time.Second) } - LogDebug("Done execing %s. %+v", job.name, containerJSON.State) + slog.LogDebug("Done execing %s. %+v", job.name, containerJSON.State) // Log exit code if failed if containerJSON.State.ExitCode != 0 { - LogError( + slog.LogError( "Exec job %s existed with code %d", job.name, containerJSON.State.ExitCode, @@ -137,10 +138,10 @@ func (job ContainerExecJob) Run() { job.context, job.containerID, ) - PanicErr(err, "Could not get container details for job %s", job.name) + slog.PanicErr(err, "Could not get container details for job %s", job.name) if !containerJSON.State.Running { - LogWarning("Container not running. Skipping %s", job.name) + slog.LogWarning("Container not running. Skipping %s", job.name) return } @@ -151,19 +152,19 @@ func (job ContainerExecJob) Run() { Cmd: []string{"sh", "-c", strings.TrimSpace(job.shellCommand)}, }, ) - PanicErr(err, "Could not create container exec job for %s", job.name) + slog.PanicErr(err, "Could not create container exec job for %s", job.name) err = job.client.ContainerExecStart( job.context, execID.ID, dockerTypes.ExecStartCheck{}, ) - PanicErr(err, "Could not start container exec job for %s", job.name) + slog.PanicErr(err, "Could not start container exec job for %s", job.name) // Wait for job results execInfo := dockerTypes.ContainerExecInspect{Running: true} for execInfo.Running { - LogDebug("Still execing %s", job.name) + slog.LogDebug("Still execing %s", job.name) execInfo, err = job.client.ContainerExecInspect( job.context, execID.ID, @@ -173,23 +174,23 @@ func (job ContainerExecJob) Run() { } time.Sleep(1 * time.Second) } - LogDebug("Done execing %s. %+v", job.name, execInfo) + slog.LogDebug("Done execing %s. %+v", job.name, execInfo) // Log exit code if failed if execInfo.ExitCode != 0 { - LogError("Exec job %s existed with code %d", job.name, execInfo.ExitCode) + slog.LogError("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) { - LogDebug("Scanning containers for new schedules...") + slog.LogDebug("Scanning containers for new schedules...") containers, err := client.ContainerList( context.Background(), dockerTypes.ContainerListOptions{All: true}, ) - PanicErr(err, "Failure querying docker containers") + slog.PanicErr(err, "Failure querying docker containers") for _, container := range containers { // Add start job @@ -261,7 +262,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 - LogDebug("Job %s is already scheduled. Skipping", job.Name()) + slog.LogDebug("Job %s is already scheduled. Skipping", job.Name()) delete(existingJobs, job.UniqueName()) continue } @@ -277,7 +278,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerCronJob) { ) } else { // TODO: Track something for a healthcheck here - LogError( + slog.LogError( "Could not schedule %s (%s) with schedule '%s'. %v\n", job.Name(), job.UniqueName(), @@ -304,7 +305,7 @@ func main() { var watchInterval time.Duration flag.DurationVar(&watchInterval, "watch", defaultWatchInterval, "Interval used to poll Docker for changes") var showVersion = flag.Bool("version", false, "Display the version of dockron and exit") - flag.BoolVar(&DebugLevel, "debug", false, "Show debug logs") + flag.BoolVar(&slog.DebugLevel, "debug", false, "Show debug logs") flag.Parse() // Print version if asked diff --git a/slog/Readme.md b/slog/Readme.md new file mode 100644 index 0000000..e9e5d6c --- /dev/null +++ b/slog/Readme.md @@ -0,0 +1,11 @@ +# slog + +A super simple go logger + +I know there are many go loggers out there that offer various logging features such as file rotation, granular verbosity settings, colored and JSON output, etc. + +_Slog is not one of them._ + +Slog lets you hide or show debug logs as well as provides a simpler way to log messages with Warning and Error prefixes for consistency. + +Also provided are a few simple methods for handling returned `error` variables, logging them out and optionally panicing or fatally exiting. diff --git a/logging.go b/slog/slog.go similarity index 99% rename from logging.go rename to slog/slog.go index 5615c41..29c975d 100644 --- a/logging.go +++ b/slog/slog.go @@ -1,4 +1,4 @@ -package main +package slog import ( "log"