Skip to content

Commit

Permalink
log internal errors only if HUMANLOG_LOG_LEVEL appropriately set
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme committed Jan 15, 2023
1 parent d6dcf3b commit a7354d3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
54 changes: 54 additions & 0 deletions cmd/humanlog/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"log"
"os"
)

type loglevel int

const (
debuglvl loglevel = iota
infolvl
warnlvl
errorlvl
)

var logLevel = func() loglevel {
switch os.Getenv("HUMANLOG_LOG_LEVEL") {
case "debug":
return debuglvl
case "info":
return infolvl
case "warn":
return warnlvl
case "error":
return errorlvl
default:
return errorlvl
}
}()

func logdebug(format string, args ...interface{}) {
if logLevel <= debuglvl {
log.Printf("[debug] "+format, args...)
}
}

func loginfo(format string, args ...interface{}) {
if logLevel <= infolvl {
log.Printf("[info] "+format, args...)
}
}

func logwarn(format string, args ...interface{}) {
if logLevel <= warnlvl {
log.Printf("[warn] "+format, args...)
}
}

func logerror(format string, args ...interface{}) {
if logLevel <= errorlvl {
log.Printf("[error] "+format, args...)
}
}
6 changes: 3 additions & 3 deletions cmd/humanlog/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func checkForUpdate(ctx context.Context, cfg *config.Config, state *state.State)
return nil, nil, false, err
}
if err := updateFromResMeta(state, msg.Meta, &nextSV, &lastCheckAt); err != nil {
log.Printf("failed to persist internal state: %v", err)
logwarn("failed to persist internal state: %v", err)
}

return msg.NextVersion, msg.NextArtifact, currentSV.LT(nextSV), nil
Expand All @@ -206,12 +206,12 @@ func asyncCheckForUpdate(ctx context.Context, req *checkForUpdateReq, cfg *confi
return
}
// TODO: log to diagnostic file?
log.Printf("failed to check for update: %v", err)
logwarn("failed to check for update: %v", err)
return
}
nexVersion, err := nextVersion.AsSemver()
if err != nil {
log.Printf("next version is not a valid semver: %v", err)
logwarn("next version is not a valid semver: %v", err)
return
}
out <- &checkForUpdateRes{
Expand Down

0 comments on commit a7354d3

Please sign in to comment.