Skip to content

Commit

Permalink
more logging, configurable via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
gernotstarke committed Nov 27, 2023
1 parent 6832631 commit 3b14ebc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
5 changes: 3 additions & 2 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ primary_region = "ams"
min_machines_running = 0
processes = ["app"]

## using the environment to differentiate between PRODUCTION (aka fly.io) and DEVELOPMENT (aka localhost)

[env]
ENVIRONMENT = "PRODUCTION"
## using the environment to differentiate between PRODUCTION (aka fly.io) and DEVELOPMENT (aka localhost)
ENVIRONMENT = "PRODUCTION"
LOGLEVEL = "DEBUG"
4 changes: 2 additions & 2 deletions internal/github/issuesAndBugs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func IssuesAndBugsCountForSite(thisSite string) (nrOfIssues int, nrOfBugs int) {
nrOfBugs = int(query.Repository.Bugs.TotalCount)
nrOfIssues = int(query.Repository.Issues.TotalCount)

log.Debug().Msgf("Number of open issues: %d\n", nrOfIssues)
log.Debug().Msgf("Number of open bugs: %d\n", nrOfBugs)
log.Debug().Msgf("Number of open issues on %s: %d\n", thisSite, nrOfIssues)
log.Debug().Msgf("Number of open bugs on %s: %d\n", thisSite, nrOfBugs)

// this kind of return takes the named result parameters and returns those...
return
Expand Down
39 changes: 29 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"strings"
)

const AppVersion = "0.3.4"
const AppVersion = "0.3.4b"

// version history
// 0.3.4b take log level from envirionment variable LOGLEVEL
// 0.3.4 removed all fmt.print*, migrated to zerolog
// 0.3.3 fixed issue #46
// 0.3.1 slight refactoring
Expand All @@ -24,14 +25,9 @@ func init() {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: zerolog.TimeFormatUnix}
log.Logger = zerolog.New(output).With().Timestamp().Caller().Logger()

}

func main() {

// log levels for zerolog:
// check if LOGLEVEL is configured in environment

// zerolog allows for logging at the following levels (from highest to lowest):

// panic (zerolog.PanicLevel, 5)
// fatal (zerolog.FatalLevel, 4)
// error (zerolog.ErrorLevel, 3)
Expand All @@ -40,15 +36,38 @@ func main() {
// debug (zerolog.DebugLevel, 0)
// trace (zerolog.TraceLevel, -1)

loglevel := os.Getenv("LOGLEVEL")
switch loglevel {
case "":
// no loglevel has been set, default to warning
zerolog.SetGlobalLevel(zerolog.WarnLevel)
loglevel = "WARN"
case "TRACE":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "WARN":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "ERROR":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
loglevel = "WARN"
}

log.Info().Msgf("log level set to %s", loglevel)
}

func main() {

// find out runtime environment:
// PROD or PRODUCTION -> fly.io, running in the cloud
// DEV or DEVELOPMENT -> localhost, running on local machine
environment := strings.ToUpper(os.Getenv("ENVIRONMENT"))
if strings.HasPrefix(environment, "PROD") {
log.Info().Msg("Running on fly.io")
// Set the minimum level to WarnLevel to log only warnings and errors
zerolog.SetGlobalLevel(zerolog.WarnLevel)

} else {
log.Info().Msg("Running on localhost")
}
Expand Down

0 comments on commit 3b14ebc

Please sign in to comment.