Skip to content

Commit

Permalink
fix: consider log level env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwep committed Oct 20, 2024
1 parent 9d1ab45 commit cfea7d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
14 changes: 0 additions & 14 deletions core/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"github.com/joho/godotenv"
"go.uber.org/zap"
"os"
"path"
Expand All @@ -10,7 +9,6 @@ import (
"runtime"
"strconv"
"strings"
"testing"
"time"
)

Expand All @@ -24,7 +22,6 @@ type AppConfig struct {
AppBuildDate string
AppBuildCommit string
AppGinMode string
AppLogMode string
AppPort string
AppUsersToCreate []User
AppUserPattern *regexp.Regexp
Expand All @@ -34,16 +31,6 @@ type AppConfig struct {
}

var Config = func() AppConfig {
envFile := path.Join(currentDir(), ".env")

if testing.Testing() {
envFile = path.Join(currentDir(), ".env.test")
}

if err := godotenv.Load(envFile); err != nil {
Logger.Debug(".env file skipped")
}

config := AppConfig{
DbPath: resolvePath(os.Getenv("GENESIS_DB_PATH")),
BaseUrl: os.Getenv("GENESIS_BASE_URL"),
Expand All @@ -54,7 +41,6 @@ var Config = func() AppConfig {
AppBuildDate: os.Getenv("GENESIS_BUILD_DATE"),
AppBuildCommit: os.Getenv("GENESIS_BUILD_COMMIT"),
AppGinMode: os.Getenv("GENESIS_GIN_MODE"),
AppLogMode: os.Getenv("GENESIS_LOG_MODE"),
AppPort: os.Getenv("GENESIS_PORT"),
AppUsersToCreate: parseInitialUserList(os.Getenv("GENESIS_CREATE_USERS")),
AppUserPattern: regexp.MustCompile(os.Getenv("GENESIS_USERNAME_PATTERN")),
Expand Down
23 changes: 22 additions & 1 deletion core/logger.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
package core

import (
"github.com/joho/godotenv"
"go.uber.org/zap"
"log"
"os"
"path"
"testing"
)

var Logger = func() *zap.Logger {
logger, err := zap.NewProductionConfig().Build(zap.AddCallerSkip(1))
envFile := path.Join(currentDir(), ".env")
if testing.Testing() {
envFile = path.Join(currentDir(), ".env.test")
}

envSkipped := godotenv.Load(envFile)

var cfg zap.Config
if os.Getenv("GENESIS_LOG_MODE") == "production" {
cfg = zap.NewProductionConfig()
} else {
cfg = zap.NewDevelopmentConfig()
}

logger, err := cfg.Build(zap.AddCallerSkip(1))
if err != nil {
log.Fatal(err)
}

if envSkipped != nil {
logger.Debug(".env file skipped")
}

return logger
}()

0 comments on commit cfea7d7

Please sign in to comment.