Skip to content

Commit

Permalink
Add ability to better control log level
Browse files Browse the repository at this point in the history
  • Loading branch information
aebruno committed Feb 9, 2023
1 parent 2dc5862 commit 5fef342
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ nfpms:
- src: ./scripts/nfpm/mokey.toml.default
dst: /etc/mokey/mokey.toml
type: "config|noreplace"
- src: ./scripts/nfpm/mokey.env
dst: /etc/default/mokey
type: "config|noreplace"
- src: ./scripts/nfpm/mokey.service
dst: /usr/lib/systemd/system/mokey.service
checksum:
Expand Down
23 changes: 13 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"io/ioutil"
golog "log"
"os"
Expand All @@ -15,9 +16,7 @@ import (
var (
cfgFile string
cfgFileUsed string
trace bool
debug bool
verbose bool
logLevel string

Root = &cobra.Command{
Use: "mokey",
Expand All @@ -37,25 +36,29 @@ func init() {
cobra.OnInitialize(initConfig)

Root.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
Root.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug messages")
Root.PersistentFlags().BoolVar(&trace, "trace", false, "Enable trace messages")
Root.PersistentFlags().BoolVar(&verbose, "verbose", false, "Enable verbose messages")
Root.PersistentFlags().StringVar(&logLevel, "loglevel", "info", "Set log level")

Root.PersistentPreRunE = func(command *cobra.Command, args []string) error {
return SetupLogging()
}
}

func SetupLogging() error {
if trace {
switch logLevel {
case "trace":
logrus.SetLevel(logrus.TraceLevel)
} else if debug {
case "debug":
logrus.SetLevel(logrus.DebugLevel)
} else if verbose {
case "info":
logrus.SetLevel(logrus.InfoLevel)
} else {
case "warn":
logrus.SetLevel(logrus.WarnLevel)
case "error":
logrus.SetLevel(logrus.ErrorLevel)
default:
return fmt.Errorf("Unknown log level: %s", logLevel)
}

golog.SetOutput(ioutil.Discard)

if cfgFileUsed != "" {
Expand Down
4 changes: 4 additions & 0 deletions scripts/nfpm/mokey.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MOKEY_ARGS="--config=/etc/mokey/mokey.toml --loglevel=info"

# --loglevel="info"
# Log level: trace,debug,info,warn,error
3 changes: 2 additions & 1 deletion scripts/nfpm/mokey.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ After=syslog.target network.target
Type=simple
User=mokey
Group=mokey
EnvironmentFile=/etc/default/mokey
WorkingDirectory=/var/lib/mokey
ExecStart=/usr/bin/mokey serve --debug -c /etc/mokey/mokey.toml
ExecStart=/usr/bin/mokey serve $MOKEY_ARGS
Restart=on-abort
StateDirectory=mokey
ConfigurationDirectory=mokey
Expand Down
2 changes: 1 addition & 1 deletion server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (r *Router) accountCreate(user *ipa.User, password, passwordConfirm, captch
"first": userRec.First,
"last": userRec.Last,
"homedir": userRec.HomeDir,
}).Warn("New user account created")
}).Debug("New user account created")

// Disable new users until they have verified their email address
err = r.adminClient.UserDisable(userRec.Username)
Expand Down
2 changes: 1 addition & 1 deletion server/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (e *Emailer) sendEmail(user *ipa.User, ctx *fiber.Ctx, subject, tmpl string
log.WithFields(log.Fields{
"email": user.Email,
"username": user.Username,
}).Info("Sending email to user")
}).Debug("Sending email to user")

if data == nil {
data = make(map[string]interface{})
Expand Down

0 comments on commit 5fef342

Please sign in to comment.