Skip to content

Commit

Permalink
Make minimum log level configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
wscalf committed Jul 2, 2024
1 parent d9aeb2b commit b929089
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 45 deletions.
49 changes: 38 additions & 11 deletions cmd/kessel-relations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"flag"
"github.com/go-kratos/kratos/v2/config/env"
"os"
"strings"

"github.com/go-kratos/kratos/v2/config/env"

"github.com/project-kessel/relations-api/internal/conf"

Expand Down Expand Up @@ -50,15 +52,7 @@ func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App {

func main() {
flag.Parse()
logger := log.With(log.NewStdLogger(os.Stdout),
"ts", log.DefaultTimestamp,
"caller", log.DefaultCaller,
"service.id", id,
"service.name", Name,
"service.version", Version,
"trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(),
)

c := config.New(
config.WithSource(
env.NewSource("SPICEDB_"),
Expand All @@ -84,7 +78,7 @@ func main() {
// bc.Data.SpiceDb.Token = preshared
//}

app, cleanup, err := wireApp(bc.Server, bc.Data, logger)
app, cleanup, err := wireApp(bc.Server, bc.Data, createLogger(bc.Server))
if err != nil {
panic(err)
}
Expand All @@ -95,3 +89,36 @@ func main() {
panic(err)
}
}

func createLogger(c *conf.Server) log.Logger {
logger := log.With(log.NewStdLogger(os.Stdout),
"ts", log.DefaultTimestamp,
"caller", log.DefaultCaller,
"service.id", id,
"service.name", Name,
"service.version", Version,
"trace.id", tracing.TraceID(),
"span.id", tracing.SpanID(),
)

var level log.Level
if c.MinLogLevel == nil {
level = log.LevelInfo
} else {
switch strings.ToUpper(*c.MinLogLevel) {
case "DEBUG":
level = log.LevelDebug
case "INFO":
level = log.LevelInfo
case "WARN":
level = log.LevelWarn
case "ERROR":
level = log.LevelError
case "FATAL":
level = log.LevelFatal
}
}
logger = log.NewFilter(logger, log.FilterLevel(level))

return logger
}
1 change: 1 addition & 0 deletions configs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ server:
grpc:
addr: "${GRPCADDR:0.0.0.0:9000}"
timeout: 1s
minLogLevel: Info
data:
spiceDb:
useTLS: false
Expand Down
81 changes: 47 additions & 34 deletions internal/conf/conf.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/conf/conf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ message Server {
}
HTTP http = 1;
GRPC grpc = 2;
optional string minLogLevel = 5;
}

message Data {
Expand Down

0 comments on commit b929089

Please sign in to comment.