Skip to content

Commit

Permalink
impl: make log level configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Oct 23, 2023
1 parent 44e98d9 commit eb247c9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions internal/options/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (

"github.com/joho/godotenv"
ldap "github.com/netresearch/simple-ldap-go"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

type Opts struct {
LogLevel zerolog.Level

LDAP ldap.Config
ReadonlyUser string
ReadonlyPassword string
Expand Down Expand Up @@ -47,6 +50,16 @@ func envDurationOrDefault(name string, d time.Duration) time.Duration {
return v
}

func envLogLevelOrDefault(name string, d zerolog.Level) string {
raw := envStringOrDefault(name, d.String())

if _, err := zerolog.ParseLevel(raw); err != nil {
log.Fatal().Msgf("err: could not parse environment variable \"%s\" (containing \"%s\") as log level: %v", name, raw, err)
}

return raw
}

func envBoolOrDefault(name string, d bool) bool {
raw := envStringOrDefault(name, fmt.Sprintf("%v", d))

Expand All @@ -64,6 +77,8 @@ func Parse() *Opts {
}

var (
fLogLevel = flag.String("log-level", envLogLevelOrDefault("LOG_LEVEL", zerolog.InfoLevel), "Log level. Valid values are: trace, debug, info, warn, error, fatal, panic.")

fLdapServer = flag.String("ldap-server", envStringOrDefault("LDAP_SERVER", ""), "LDAP server URI, has to begin with `ldap://` or `ldaps://`. If this is an ActiveDirectory server, this *has* to be `ldaps://`.")
fIsActiveDirectory = flag.Bool("active-directory", envBoolOrDefault("LDAP_IS_AD", false), "Mark the LDAP server as ActiveDirectory.")
fBaseDN = flag.String("base-dn", envStringOrDefault("LDAP_BASE_DN", ""), "Base DN of your LDAP directory.")
Expand All @@ -79,6 +94,11 @@ func Parse() *Opts {
flag.Parse()
}

logLevel, err := zerolog.ParseLevel(*fLogLevel)
if err != nil {
log.Fatal().Err(err).Msg("could not parse log level")
}

panicWhenEmpty("ldap-server", fLdapServer)
panicWhenEmpty("base-dn", fBaseDN)
panicWhenEmpty("readonly-user", fReadonlyUser)
Expand All @@ -95,6 +115,8 @@ func Parse() *Opts {
}

return &Opts{
LogLevel: logLevel,

LDAP: ldapConfig,
ReadonlyUser: *fReadonlyUser,
ReadonlyPassword: *fReadonlyPassword,
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

opts := options.Parse()
log.Logger = log.Logger.Level(opts.LogLevel)

app, err := web.NewApp(opts)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"css:dev": "pnpm css:build -w --verbose",
"go:start": "go run .",
"go:build": "go build",
"go:dev": "nodemon --signal SIGTERM -e go,html,css -w \"./**\" -x pnpm go:start --persist-sessions --session-path session.bbolt"
"go:dev": "nodemon --signal SIGTERM -e go,html,css -w \"./**\" -x pnpm go:start --persist-sessions --session-path session.bbolt --log-level debug"
},
"devDependencies": {
"@tailwindcss/forms": "^0.5.6",
Expand Down

0 comments on commit eb247c9

Please sign in to comment.