Simplify/improve logging configuration
This commit is contained in:
parent
690f23b6da
commit
acffdc1c65
@ -43,3 +43,9 @@ DB_DSN=corteza:corteza@tcp(localhost:3306)/corteza?collation=utf8mb4_general_ci
|
||||
|
||||
# DB query profiler (none, stdout, logger)
|
||||
#DB_PROFILER=none
|
||||
|
||||
# Logging level we want to use (values: debug, info, warn, error, dpanic, panic, fatal)
|
||||
#LOG_LEVEL=info
|
||||
|
||||
# Enable debug logger (more verbose,
|
||||
#LOG_DEBUG=false
|
||||
|
||||
@ -6,7 +6,6 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
cfg := system.Configure()
|
||||
cmd := cfg.MakeCLI(cli.Context())
|
||||
cli.HandleError(cmd.Execute())
|
||||
|
||||
@ -6,21 +6,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/internal/auth"
|
||||
"github.com/cortezaproject/corteza-server/internal/http"
|
||||
"github.com/cortezaproject/corteza-server/internal/mail"
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli/options"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
)
|
||||
|
||||
func InitGeneralServices(logOpt *options.LogOpt, smtpOpt *options.SMTPOpt, jwtOpt *options.JWTOpt, httpClientOpt *options.HttpClientOpt) {
|
||||
// Reset logger's level to whatever we want
|
||||
var logLevel = zap.InfoLevel
|
||||
_ = logLevel.Set(logOpt.Level)
|
||||
logger.DefaultLevel.SetLevel(logLevel)
|
||||
|
||||
func InitGeneralServices(smtpOpt *options.SMTPOpt, jwtOpt *options.JWTOpt, httpClientOpt *options.HttpClientOpt) {
|
||||
auth.SetupDefault(jwtOpt.Secret, int(jwtOpt.Expiry/time.Minute))
|
||||
mail.SetupDialer(smtpOpt.Host, smtpOpt.Port, smtpOpt.User, smtpOpt.Pass, smtpOpt.From)
|
||||
http.SetupDefaults(
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
package options
|
||||
|
||||
type (
|
||||
// Logger's output leve is configured here, but
|
||||
// dev/prod configuration happens earlier
|
||||
LogOpt struct {
|
||||
Level string `env:"LOG_LEVEL"`
|
||||
}
|
||||
)
|
||||
|
||||
func Log(pfix string) (o *LogOpt) {
|
||||
o = &LogOpt{
|
||||
Level: "info",
|
||||
}
|
||||
|
||||
fill(o, pfix)
|
||||
|
||||
return
|
||||
}
|
||||
@ -43,7 +43,6 @@ type (
|
||||
Log *zap.Logger
|
||||
|
||||
// General options
|
||||
LogOpt *options.LogOpt
|
||||
SmtpOpt *options.SMTPOpt
|
||||
JwtOpt *options.JWTOpt
|
||||
HttpClientOpt *options.HttpClientOpt
|
||||
@ -175,7 +174,6 @@ func (c *Config) Init() {
|
||||
c.DatabaseName = c.ServiceName
|
||||
}
|
||||
|
||||
c.LogOpt = options.Log(c.EnvPrefix)
|
||||
c.SmtpOpt = options.SMTP(c.EnvPrefix)
|
||||
c.JwtOpt = options.JWT(c.EnvPrefix)
|
||||
c.HttpClientOpt = options.HttpClient(c.EnvPrefix)
|
||||
@ -221,7 +219,7 @@ func (c *Config) MakeCLI(ctx context.Context) (cmd *cobra.Command) {
|
||||
}
|
||||
|
||||
defer sentry.Recover()
|
||||
InitGeneralServices(c.LogOpt, c.SmtpOpt, c.JwtOpt, c.HttpClientOpt)
|
||||
InitGeneralServices(c.SmtpOpt, c.JwtOpt, c.HttpClientOpt)
|
||||
|
||||
err = c.RootCommandDBSetup.Run(ctx, cmd, c)
|
||||
if err != nil {
|
||||
|
||||
@ -2,9 +2,14 @@ package logger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
// Make sure we read the ENV from .env
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli/options"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -26,19 +31,27 @@ func MakeDebugLogger() *zap.Logger {
|
||||
|
||||
func Init() {
|
||||
var (
|
||||
err error
|
||||
isProduction bool
|
||||
err error
|
||||
|
||||
// Set INFO as defaut log level
|
||||
logLevel = zapcore.InfoLevel
|
||||
|
||||
// Do we want to enable debug logger
|
||||
// with a bit more dev-friendly output
|
||||
debuggingLogger = options.EnvBool("", "LOG_DEBUG", false)
|
||||
)
|
||||
|
||||
if env := os.Getenv("ENVIRONMENT"); strings.Index(env, "prod") == 0 {
|
||||
// Try to guess if production logging from environment
|
||||
isProduction = true
|
||||
} else if vh := os.Getenv("VIRTUAL_HOST"); len(vh) > 0 {
|
||||
// Try to guess if in production logging from the fact that VIRTUAL_HOST env is set
|
||||
isProduction = true
|
||||
if debuggingLogger {
|
||||
logLevel = zapcore.DebugLevel
|
||||
}
|
||||
|
||||
if !isProduction {
|
||||
if ll, has := os.LookupEnv("LOG_LEVEL"); has {
|
||||
_ = logLevel.Set(ll)
|
||||
}
|
||||
|
||||
DefaultLevel.SetLevel(logLevel)
|
||||
|
||||
if debuggingLogger {
|
||||
defaultLogger = MakeDebugLogger()
|
||||
return
|
||||
}
|
||||
@ -48,7 +61,6 @@ func Init() {
|
||||
|
||||
// We do not want sampling
|
||||
conf.Sampling = nil
|
||||
conf.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
|
||||
|
||||
defaultLogger, err = conf.Build()
|
||||
if err != nil {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user