replace profiler with logger, update logger option to bool
This commit is contained in:
parent
c4da01853d
commit
cc3c100c62
@ -41,8 +41,8 @@ MONITOR_INTERVAL=5min
|
|||||||
# Database to use
|
# Database to use
|
||||||
DB_DSN=corteza:corteza@tcp(localhost:3306)/corteza?collation=utf8mb4_general_ci
|
DB_DSN=corteza:corteza@tcp(localhost:3306)/corteza?collation=utf8mb4_general_ci
|
||||||
|
|
||||||
# DB query profiler (none, stdout, logger)
|
# Log database queries?
|
||||||
#DB_PROFILER=none
|
#DB_LOGGER=false
|
||||||
|
|
||||||
# Logging level we want to use (values: debug, info, warn, error, dpanic, panic, fatal)
|
# Logging level we want to use (values: debug, info, warn, error, dpanic, panic, fatal)
|
||||||
#LOG_LEVEL=info
|
#LOG_LEVEL=info
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/titpetric/factory"
|
"github.com/titpetric/factory"
|
||||||
|
"github.com/titpetric/factory/logger"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/cortezaproject/corteza-server/pkg/cli/options"
|
"github.com/cortezaproject/corteza-server/pkg/cli/options"
|
||||||
@ -93,14 +94,15 @@ func TryToConnect(ctx context.Context, log *zap.Logger, name string, opt options
|
|||||||
return nil, errors.Errorf("db connection for %q cancelled", name)
|
return nil, errors.Errorf("db connection for %q cancelled", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch opt.Profiler {
|
if opt.Logger {
|
||||||
case "stdout":
|
db.SetLogger(
|
||||||
db.Profiler = &factory.Database.ProfilerStdout
|
NewZapLogger(
|
||||||
case "logger":
|
// Skip 3 levels in call stack to get to the actual function used
|
||||||
// Skip 3 levels in call stack to get to the actual function used
|
log.WithOptions(zap.AddCallerSkip(3)),
|
||||||
db.Profiler = ZapProfiler(log.
|
),
|
||||||
WithOptions(zap.AddCallerSkip(3)),
|
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
db.SetLogger(logger.Silent{})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
32
internal/db/logger.go
Normal file
32
internal/db/logger.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/titpetric/factory/logger"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
zapLogger struct {
|
||||||
|
logger *zap.Logger
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewZapLogger(logger *zap.Logger) *zapLogger {
|
||||||
|
return &zapLogger{
|
||||||
|
logger: logger,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *zapLogger) Log(ctx context.Context, msg string, fields ...logger.Field) {
|
||||||
|
// @todo when factory.DatabaseProfilerContext gets access to context from
|
||||||
|
// db functions, try to extract RequestID with middleware.GetReqID()
|
||||||
|
|
||||||
|
zapFields := []zap.Field{}
|
||||||
|
for _, v := range fields {
|
||||||
|
zapFields = append(zapFields, zap.Any(v.Name(), v.Value()))
|
||||||
|
}
|
||||||
|
|
||||||
|
z.logger.Debug(msg, zapFields...)
|
||||||
|
}
|
||||||
@ -1,36 +0,0 @@
|
|||||||
package db
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/titpetric/factory"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
)
|
|
||||||
|
|
||||||
// zapProfiler logs query statistics to zap.logger
|
|
||||||
type (
|
|
||||||
zapProfiler struct {
|
|
||||||
logger *zap.Logger
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func ZapProfiler(logger *zap.Logger) *zapProfiler {
|
|
||||||
return &zapProfiler{
|
|
||||||
logger: logger,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Post prints the query statistics to stdout
|
|
||||||
func (p zapProfiler) Post(c *factory.DatabaseProfilerContext) {
|
|
||||||
// @todo when factory.DatabaseProfilerContext gets access to context from
|
|
||||||
// db functions, try to extract RequestID with middleware.GetReqID()
|
|
||||||
|
|
||||||
p.logger.Debug(
|
|
||||||
c.Query,
|
|
||||||
zap.Any("args", c.Args),
|
|
||||||
zap.Float64("duration", time.Since(c.Time).Seconds()))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Flush stdout (no-op for this profiler)
|
|
||||||
func (zapProfiler) Flush() {
|
|
||||||
}
|
|
||||||
@ -7,7 +7,7 @@ import (
|
|||||||
type (
|
type (
|
||||||
DBOpt struct {
|
DBOpt struct {
|
||||||
DSN string `env:"DB_DSN"`
|
DSN string `env:"DB_DSN"`
|
||||||
Profiler string `env:"DB_PROFILER"`
|
Logger bool `env:"DB_LOGGER"`
|
||||||
MaxTries int `env:"DB_MAX_TRIES"`
|
MaxTries int `env:"DB_MAX_TRIES"`
|
||||||
Delay time.Duration `env:"DB_CONN_ERR_DELAY"`
|
Delay time.Duration `env:"DB_CONN_ERR_DELAY"`
|
||||||
Timeout time.Duration `env:"DB_CONN_TIMEOUT"`
|
Timeout time.Duration `env:"DB_CONN_TIMEOUT"`
|
||||||
@ -17,7 +17,7 @@ type (
|
|||||||
func DB(pfix string) (o *DBOpt) {
|
func DB(pfix string) (o *DBOpt) {
|
||||||
o = &DBOpt{
|
o = &DBOpt{
|
||||||
DSN: "corteza:corteza@tcp(db:3306)/corteza?collation=utf8mb4_general_ci",
|
DSN: "corteza:corteza@tcp(db:3306)/corteza?collation=utf8mb4_general_ci",
|
||||||
Profiler: "none",
|
Logger: false,
|
||||||
MaxTries: 100,
|
MaxTries: 100,
|
||||||
Delay: 5 * time.Second,
|
Delay: 5 * time.Second,
|
||||||
Timeout: 1 * time.Minute,
|
Timeout: 1 * time.Minute,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user