From 5fef3421827ea2b0eebaed22775f8fd6cde649ee Mon Sep 17 00:00:00 2001 From: "Andrew E. Bruno" Date: Thu, 9 Feb 2023 11:53:30 -0500 Subject: [PATCH] Add ability to better control log level --- .goreleaser.yaml | 3 +++ cmd/root.go | 23 +++++++++++++---------- scripts/nfpm/mokey.env | 4 ++++ scripts/nfpm/mokey.service | 3 ++- server/account.go | 2 +- server/email.go | 2 +- 6 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 scripts/nfpm/mokey.env diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 1d55f66..a434db1 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -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: diff --git a/cmd/root.go b/cmd/root.go index 8ba86b5..64a4bba 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "io/ioutil" golog "log" "os" @@ -15,9 +16,7 @@ import ( var ( cfgFile string cfgFileUsed string - trace bool - debug bool - verbose bool + logLevel string Root = &cobra.Command{ Use: "mokey", @@ -37,9 +36,7 @@ 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() @@ -47,15 +44,21 @@ func init() { } 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 != "" { diff --git a/scripts/nfpm/mokey.env b/scripts/nfpm/mokey.env new file mode 100644 index 0000000..701511f --- /dev/null +++ b/scripts/nfpm/mokey.env @@ -0,0 +1,4 @@ +MOKEY_ARGS="--config=/etc/mokey/mokey.toml --loglevel=info" + +# --loglevel="info" +# Log level: trace,debug,info,warn,error diff --git a/scripts/nfpm/mokey.service b/scripts/nfpm/mokey.service index 3030ccd..c3befc6 100644 --- a/scripts/nfpm/mokey.service +++ b/scripts/nfpm/mokey.service @@ -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 diff --git a/server/account.go b/server/account.go index 8b374d0..7878a47 100644 --- a/server/account.go +++ b/server/account.go @@ -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) diff --git a/server/email.go b/server/email.go index e3c1860..287df0d 100644 --- a/server/email.go +++ b/server/email.go @@ -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{})