Skip to content

Commit

Permalink
feat(template): set zap loglevel via environment var (#92)
Browse files Browse the repository at this point in the history
* Set ZAP loglevel via environment var

Signed-off-by: Steffen Exler <Steffen.Exler@mail.schwarz>

* Update _template/cmd/{{.Base.appName}}/main.go

Co-authored-by: Tobias Brumhard <tobias.brumhard@mail.schwarz>

* Put logger logic into internal folder

Signed-off-by: Steffen Exler <Steffen.Exler@mail.schwarz>

* Rename logger.GetLogger to logger.NewAtLevel

Signed-off-by: Tobias Brumhard <tobias.brumhard@mail.schwarz>

Co-authored-by: Tobias Brumhard <tobias.brumhard@mail.schwarz>
  • Loading branch information
linuxluigi and brumhard committed Apr 30, 2022
1 parent 888b843 commit 4e5479c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion _template/cmd/{{.Base.appName}}/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"{{.Base.moduleName}}/internal/logger"
"go.uber.org/zap"

// This controls the maxprocs environment variable in container runtimes.
Expand All @@ -19,7 +20,7 @@ func main() {
}

func run() error {
logger, err := zap.NewProduction()
logger, err := logger.NewAtLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
return err
}
Expand Down
27 changes: 27 additions & 0 deletions _template/internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package logger

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func NewAtLevel(levelStr string) (*zap.Logger, error) {
logLevel := zapcore.InfoLevel
if levelStr != "" {
var err error
logLevel, err = zapcore.ParseLevel(levelStr)
if err != nil {
return nil, err
}
}

logConf := zap.NewProductionConfig()
logConf.Level = zap.NewAtomicLevelAt(logLevel)

logger, err := logConf.Build()
if err != nil {
return nil, err
}

return logger, nil
}

0 comments on commit 4e5479c

Please sign in to comment.