Update slog interfaces
continuous-integration/drone/push Build is failing Details

This commit is contained in:
IamTheFij 2020-08-17 15:34:47 -07:00
parent 32d681703c
commit 73b7a12eb7
3 changed files with 86 additions and 56 deletions

42
main.go
View File

@ -60,17 +60,17 @@ type ContainerStartJob struct {
// Run is executed based on the ContainerStartJob Schedule and starts the // Run is executed based on the ContainerStartJob Schedule and starts the
// container // container
func (job ContainerStartJob) Run() { func (job ContainerStartJob) Run() {
slog.Log("Starting: %s", job.name) slog.Info("Starting: %s", job.name)
// Check if container is already running // Check if container is already running
containerJSON, err := job.client.ContainerInspect( containerJSON, err := job.client.ContainerInspect(
job.context, job.context,
job.containerID, 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 { 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 return
} }
@ -80,24 +80,24 @@ func (job ContainerStartJob) Run() {
job.containerID, job.containerID,
dockerTypes.ContainerStartOptions{}, 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 // Check results of job
for check := true; check; check = containerJSON.State.Running { 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( containerJSON, err = job.client.ContainerInspect(
job.context, job.context,
job.containerID, 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) 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 // Log exit code if failed
if containerJSON.State.ExitCode != 0 { if containerJSON.State.ExitCode != 0 {
slog.LogError( slog.Error(
"Exec job %s existed with code %d", "Exec job %s existed with code %d",
job.name, job.name,
containerJSON.State.ExitCode, containerJSON.State.ExitCode,
@ -132,15 +132,15 @@ type ContainerExecJob struct {
// Run is executed based on the ContainerStartJob Schedule and starts the // Run is executed based on the ContainerStartJob Schedule and starts the
// container // container
func (job ContainerExecJob) Run() { func (job ContainerExecJob) Run() {
slog.Log("Execing: %s", job.name) slog.Info("Execing: %s", job.name)
containerJSON, err := job.client.ContainerInspect( containerJSON, err := job.client.ContainerInspect(
job.context, job.context,
job.containerID, 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 { if !containerJSON.State.Running {
slog.LogWarning("Container not running. Skipping %s", job.name) slog.Warning("Container not running. Skipping %s", job.name)
return return
} }
@ -151,19 +151,19 @@ func (job ContainerExecJob) Run() {
Cmd: []string{"sh", "-c", strings.TrimSpace(job.shellCommand)}, 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( err = job.client.ContainerExecStart(
job.context, job.context,
execID.ID, execID.ID,
dockerTypes.ExecStartCheck{}, 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 // Wait for job results
execInfo := dockerTypes.ContainerExecInspect{Running: true} execInfo := dockerTypes.ContainerExecInspect{Running: true}
for execInfo.Running { for execInfo.Running {
slog.LogDebug("Still execing %s", job.name) slog.Debug("Still execing %s", job.name)
execInfo, err = job.client.ContainerExecInspect( execInfo, err = job.client.ContainerExecInspect(
job.context, job.context,
execID.ID, execID.ID,
@ -173,23 +173,23 @@ func (job ContainerExecJob) Run() {
} }
time.Sleep(1 * time.Second) 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 // Log exit code if failed
if execInfo.ExitCode != 0 { 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 // QueryScheduledJobs queries Docker for all containers with a schedule and
// returns a list of ContainerCronJob records to be scheduled // returns a list of ContainerCronJob records to be scheduled
func QueryScheduledJobs(client ContainerClient) (jobs []ContainerCronJob) { func QueryScheduledJobs(client ContainerClient) (jobs []ContainerCronJob) {
slog.LogDebug("Scanning containers for new schedules...") slog.Debug("Scanning containers for new schedules...")
containers, err := client.ContainerList( containers, err := client.ContainerList(
context.Background(), context.Background(),
dockerTypes.ContainerListOptions{All: true}, dockerTypes.ContainerListOptions{All: true},
) )
slog.PanicErr(err, "Failure querying docker containers") slog.PanicOnErr(err, "Failure querying docker containers")
for _, container := range containers { for _, container := range containers {
// Add start job // Add start job
@ -261,7 +261,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerCronJob) {
if _, ok := existingJobs[job.UniqueName()]; ok { if _, ok := existingJobs[job.UniqueName()]; ok {
// Job already exists, remove it from existing jobs so we don't // Job already exists, remove it from existing jobs so we don't
// unschedule it later // 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()) delete(existingJobs, job.UniqueName())
continue continue
} }
@ -269,7 +269,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerCronJob) {
// Job doesn't exist yet, schedule it // Job doesn't exist yet, schedule it
_, err := c.AddJob(job.Schedule(), job) _, err := c.AddJob(job.Schedule(), job)
if err == nil { if err == nil {
slog.Log( slog.Info(
"Scheduled %s (%s) with schedule '%s'\n", "Scheduled %s (%s) with schedule '%s'\n",
job.Name(), job.Name(),
job.UniqueName(), job.UniqueName(),
@ -277,7 +277,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerCronJob) {
) )
} else { } else {
// TODO: Track something for a healthcheck here // TODO: Track something for a healthcheck here
slog.LogError( slog.Error(
"Could not schedule %s (%s) with schedule '%s'. %v\n", "Could not schedule %s (%s) with schedule '%s'. %v\n",
job.Name(), job.Name(),
job.UniqueName(), job.UniqueName(),

View File

@ -26,23 +26,29 @@ Also provided are a few simple methods for handling returned `error` variables,
FUNCTIONS FUNCTIONS
func FatalErr(err error, format string, v ...interface{}) func Debug(format string, v ...interface{})
FatalErr if error provided, will log out details of an error and exi 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{}) func Log(format string, v ...interface{})
Log formats logs directly to the main logger Log formats logs directly to the main logger
func LogDebug(format string, v ...interface{}) func PanicOnErr(err error, format string, v ...interface{})
LogDebug will log with a DEBUG prefix if DebugLevel is se PanicOnErr if error provided, will log out details of an error and exi
func LogError(format string, v ...interface{}) func SetFlags(flag int)
LogError will log with a ERROR prefix SetFlags allows changing the logger flags using flags found in `log`
func LogWarning(format string, v ...interface{}) func WarnOnErr(err error, format string, v ...interface{})
LogWarning will log with a WARNING prefix WarnOnErr if error provided, will provide a warning if an error is provided
func PanicErr(err error, format string, v ...interface{}) func Warning(format string, v ...interface{})
PanicErr if error provided, will log out details of an error and exi Warning will log with a WARNING prefix
func WarnErr(err error, format string, v ...interface{})
WarnErr if error provided, will provide a warning if an error is provided

View File

@ -12,53 +12,77 @@ var (
// DebugLevel indicates if we should log at the debug level // DebugLevel indicates if we should log at the debug level
DebugLevel = true DebugLevel = true
// Loggers for various levels // Default set of flags to use
loggerDebug = log.New(os.Stderr, "DEBUG", log.LstdFlags) defaultFlags = log.LstdFlags | log.Lmsgprefix
loggerWarning = log.New(os.Stderr, "WARNING", log.LstdFlags)
loggerError = log.New(os.Stderr, "ERROR", log.LstdFlags) // 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 // Log formats logs directly to the main logger
func Log(format string, v ...interface{}) { func Log(format string, v ...interface{}) {
log.Printf(format, v...) log.Printf(format, v...)
} }
// LogDebug will log with a DEBUG prefix if DebugLevel is set // Info formats logs with an INFO prefix
func LogDebug(format string, v ...interface{}) { 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 { if !DebugLevel {
return return
} }
loggerDebug.Printf(format, v...) loggerDebug.Printf(format, v...)
} }
// LogWarning will log with a WARNING prefix // WarnOnErr if error provided, will provide a warning if an error is provided
func LogWarning(format string, v ...interface{}) { func WarnOnErr(err error, 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{}) {
if err != nil { if err != nil {
loggerWarning.Printf(format, v...) loggerWarning.Printf(format, v...)
loggerError.Print(err) loggerError.Print(err)
} }
} }
// FatalErr if error provided, will log out details of an error and exit // FatalOnErr if error provided, will log out details of an error and exit
func FatalErr(err error, format string, v ...interface{}) { func FatalOnErr(err error, format string, v ...interface{}) {
if err != nil { if err != nil {
loggerError.Printf(format, v...) loggerError.Printf(format, v...)
loggerError.Fatal(err) loggerError.Fatal(err)
} }
} }
// PanicErr if error provided, will log out details of an error and exit // PanicOnErr if error provided, will log out details of an error and exit
func PanicErr(err error, format string, v ...interface{}) { func PanicOnErr(err error, format string, v ...interface{}) {
if err != nil { if err != nil {
loggerError.Printf(format, v...) loggerError.Printf(format, v...)
loggerError.Panic(err) loggerError.Panic(err)