Skip to content

Commit

Permalink
Make log level configurable (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq authored Jul 4, 2023
1 parent dfffcc1 commit 1092064
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 44 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ Execution environment. Affects:

Optional, can be set to `PRODUCTION` or `DEVELOPMENT`. Defaults to `PRODUCTION`.

### `NOTIFICATIONS_LOG_LEVEL`

Log level.

Optional, can be set to `TRACE`, `DEBUG`, `ERROR` or `DISABLED`. Defaults to `DEBUG`.

### `FIRESTORE_EMULATOR_HOST`

Expand Down
31 changes: 19 additions & 12 deletions cmd/notification-service/di/inject_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@ package di

import (
"github.com/ThreeDotsLabs/watermill"
"github.com/boreq/errors"
"github.com/google/wire"
"github.com/planetary-social/go-notification-service/internal/logging"
"github.com/planetary-social/go-notification-service/service/config"
"github.com/sirupsen/logrus"
)

var loggingSet = wire.NewSet(
newSystemLogger,

logging.NewLogrusLoggingSystem,
wire.Bind(new(logging.LoggingSystem), new(logging.LogrusLoggingSystem)),

newLogrus,
newLogger,

logging.NewWatermillAdapter,
wire.Bind(new(watermill.LoggerAdapter), new(*logging.WatermillAdapter)),
)

func newSystemLogger(system logging.LoggingSystem) logging.Logger {
return logging.NewSystemLogger(system, "root")
}
func newLogger(config config.Config) (logging.Logger, error) {
if config.LogLevel() == logging.LevelDisabled {
return logging.NewDevNullLogger(), nil
}

func newLogrus() *logrus.Logger {
v := logrus.New()
v.SetLevel(logrus.DebugLevel)
return v
switch config.LogLevel() {
case logging.LevelTrace:
v.SetLevel(logrus.TraceLevel)
case logging.LevelDebug:
v.SetLevel(logrus.DebugLevel)
case logging.LevelError:
v.SetLevel(logrus.ErrorLevel)
default:
return nil, errors.New("unsupported log level")
}

return logging.NewSystemLogger(logging.NewLogrusLoggingSystem(v), "root"), nil
}
66 changes: 34 additions & 32 deletions cmd/notification-service/di/wire_gen.go

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

26 changes: 26 additions & 0 deletions service/adapters/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/boreq/errors"
"github.com/planetary-social/go-notification-service/internal/logging"
"github.com/planetary-social/go-notification-service/service/config"
)

Expand All @@ -21,6 +22,7 @@ const (
envAPNSCertificatePath = "APNS_CERTIFICATE_PATH"
envAPNSCertificatePassword = "APNS_CERTIFICATE_PASSWORD"
envEnvironment = "ENVIRONMENT"
envLogLevel = "LOG_LEVEL"
)

type EnvironmentConfigLoader struct {
Expand All @@ -36,6 +38,11 @@ func (c *EnvironmentConfigLoader) Load() (config.Config, error) {
return config.Config{}, errors.Wrap(err, "error loading the environment setting")
}

logLevel, err := c.loadLogLevel()
if err != nil {
return config.Config{}, errors.Wrap(err, "error loading the log level")
}

var firestoreCredentialsJSON []byte
if p := c.getenv(envFirestoreCredentialsJSONPath); p != "" {
f, err := os.Open(p)
Expand All @@ -60,6 +67,7 @@ func (c *EnvironmentConfigLoader) Load() (config.Config, error) {
c.getenv(envAPNSCertificatePath),
c.getenv(envAPNSCertificatePassword),
environment,
logLevel,
)
}

Expand All @@ -80,3 +88,21 @@ func (c *EnvironmentConfigLoader) loadEnvironment() (config.Environment, error)
func (c *EnvironmentConfigLoader) getenv(key string) string {
return os.Getenv(fmt.Sprintf("%s_%s", envPrefix, key))
}

func (c *EnvironmentConfigLoader) loadLogLevel() (logging.Level, error) {
v := strings.ToUpper(os.Getenv(envLogLevel))
switch v {
case "TRACE":
return logging.LevelTrace, nil
case "DEBUG":
return logging.LevelDebug, nil
case "ERROR":
return logging.LevelError, nil
case "DISABLED":
return logging.LevelDisabled, nil
case "":
return logging.LevelDebug, nil
default:
return 0, fmt.Errorf("invalid log level requested '%s'", v)
}
}
8 changes: 8 additions & 0 deletions service/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/boreq/errors"
"github.com/planetary-social/go-notification-service/internal/logging"
)

type Environment struct {
Expand All @@ -27,6 +28,7 @@ type Config struct {
apnsCertificatePassword string

environment Environment
logLevel logging.Level
}

func NewConfig(
Expand All @@ -38,6 +40,7 @@ func NewConfig(
apnsCertificatePath string,
apnsCertificatePassword string,
environment Environment,
logLevel logging.Level,
) (Config, error) {
c := Config{
nostrListenAddress: nostrListenAddress,
Expand All @@ -48,6 +51,7 @@ func NewConfig(
apnsCertificatePath: apnsCertificatePath,
apnsCertificatePassword: apnsCertificatePassword,
environment: environment,
logLevel: logLevel,
}

c.setDefaults()
Expand Down Expand Up @@ -90,6 +94,10 @@ func (c *Config) Environment() Environment {
return c.environment
}

func (c *Config) LogLevel() logging.Level {
return c.logLevel
}

func (c *Config) setDefaults() {
if c.nostrListenAddress == "" {
c.nostrListenAddress = ":8008"
Expand Down

0 comments on commit 1092064

Please sign in to comment.