Browse Source

Merge pull request #188 from dtm-labs/revert-187-main

Revert "Write log to file"
pull/191/head
yedf2 4 years ago
committed by GitHub
parent
commit
3b2ce02eb5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      bench/main.go
  2. 10
      conf.sample.yml
  3. 40
      dtmcli/logger/log.go
  4. 20
      dtmcli/logger/logger_test.go
  5. 13
      dtmsvr/config/config.go
  6. 11
      dtmsvr/svr.go
  7. 1
      go.mod
  8. 2
      main.go

2
bench/main.go

@ -33,7 +33,7 @@ func main() {
}
logger.Infof("starting bench server")
config.MustLoadConfig("")
logger.InitLog(conf.Log.Level)
logger.InitLog(conf.LogLevel)
if busi.BusiConf.Driver != "" {
dtmcli.SetCurrentDBType(busi.BusiConf.Driver)
svr.PrepareBenchDB()

10
conf.sample.yml

@ -48,15 +48,7 @@
# RetryInterval: 10 # the subtrans branch will be retried after this interval
# RequestTimeout: 3 # the timeout of HTTP/gRPC request in dtm
# Log:
# Level: 'info' # default: info. can be debug|info|warn|error
# Output: 'console' # default: console. can be console|file
# FileName: '/tmp/dtm.log' # default: /tmp/dtm.log.
# FileMaxSize: 10 # default: 10, unit: MB.
# FileMaxBackups: 5 # default: 5.
# FileMaxAge: 30 # default: 30, unit: days.
# FileCompress: 0 # default: 0. can by 0|1, means false|true
# LogLevel: 'info' # default: info. can be debug|info|warn|error
# HttpPort: 36789
# GrpcPort: 36790

40
dtmcli/logger/log.go

@ -1,12 +1,9 @@
package logger
import (
"fmt"
"log"
"net/url"
"os"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
@ -27,14 +24,6 @@ type Logger interface {
Errorf(format string, args ...interface{})
}
type lumberjackSink struct {
*lumberjack.Logger
}
func (lumberjackSink) Sync() error {
return nil
}
// WithLogger replaces default logger
func WithLogger(log Logger) {
logger = log
@ -43,38 +32,17 @@ func WithLogger(log Logger) {
// InitLog is an initialization for a logger
// level can be: debug info warn error
func InitLog(level string) {
config := loadConfig(level)
p, err := config.Build(zap.AddCallerSkip(1))
FatalIfError(err)
logger = p.Sugar()
}
// InitRotateLog is an initialization for a rotated logger by lumberjack
func InitRotateLog(logLevel string, ll *lumberjack.Logger) {
config := loadConfig(logLevel)
config.OutputPaths = []string{fmt.Sprintf("lumberjack:%s", ll.Filename), "stdout"}
err := zap.RegisterSink("lumberjack", func(*url.URL) (zap.Sink, error) {
return lumberjackSink{
Logger: ll,
}, nil
})
FatalIfError(err)
p, err := config.Build(zap.AddCallerSkip(1))
FatalIfError(err)
logger = p.Sugar()
}
func loadConfig(logLevel string) zap.Config {
config := zap.NewProductionConfig()
err := config.Level.UnmarshalText([]byte(logLevel))
err := config.Level.UnmarshalText([]byte(level))
FatalIfError(err)
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if os.Getenv("DTM_DEBUG") != "" {
config.Encoding = "console"
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
return config
p, err := config.Build(zap.AddCallerSkip(1))
FatalIfError(err)
logger = p.Sugar()
}
// Debugf log to level debug

20
dtmcli/logger/logger_test.go

@ -4,7 +4,6 @@ import (
"os"
"testing"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
)
@ -29,22 +28,3 @@ func TestWithLogger(t *testing.T) {
FatalfIf(false, "nothing")
FatalIfError(nil)
}
func TestInitRotateLog(t *testing.T) {
os.Setenv("DTM_DEBUG", "1")
ll := lumberjack.Logger{
Filename: "test.log",
MaxSize: 1,
MaxBackups: 1,
MaxAge: 1,
Compress: false,
}
InitRotateLog("debug", &ll)
Debugf("a debug msg")
Infof("a info msg")
Warnf("a warn msg")
Errorf("a error msg")
FatalfIf(false, "nothing")
FatalIfError(nil)
_ = os.Remove("test.log")
}

13
dtmsvr/config/config.go

@ -29,17 +29,6 @@ type MicroService struct {
EndPoint string `yaml:"EndPoint"`
}
// Log config customize log
type Log struct {
Level string `yaml:"Level" default:"info"`
Output string `yaml:"Output" default:"console"`
FileName string `yaml:"FileName" default:"/tmp/dtm.log"`
FileMaxSize int64 `yaml:"FileMaxSize" default:"10"`
FileMaxBackups int64 `yaml:"FileMaxBackups" default:"5"`
FileMaxAge int64 `yaml:"FileMaxAge" default:"30"`
FileCompress int64 `yaml:"FileCompress" default:"0"`
}
// Store defines storage relevant info
type Store struct {
Driver string `yaml:"Driver" default:"boltdb"`
@ -83,7 +72,7 @@ type configType struct {
MicroService MicroService `yaml:"MicroService"`
UpdateBranchSync int64 `yaml:"UpdateBranchSync"`
UpdateBranchAsyncGoroutineNum int64 `yaml:"UpdateBranchAsyncGoroutineNum" default:"1"`
Log Log `yaml:"Log"`
LogLevel string `yaml:"LogLevel" default:"info"`
}
// Config 配置

11
dtmsvr/svr.go

@ -20,23 +20,12 @@ import (
"github.com/dtm-labs/dtm/dtmutil"
"github.com/dtm-labs/dtmdriver"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/natefinch/lumberjack"
"google.golang.org/grpc"
)
// StartSvr StartSvr
func StartSvr() {
logger.Infof("start dtmsvr")
if conf.Log.Output == "file" {
ll := lumberjack.Logger{
Filename: conf.Log.FileName,
MaxSize: int(conf.Log.FileMaxSize),
MaxBackups: int(conf.Log.FileMaxBackups),
MaxAge: int(conf.Log.FileMaxAge),
Compress: conf.Log.FileCompress != 0,
}
logger.InitRotateLog(conf.Log.Level, &ll)
}
dtmcli.GetRestyClient().SetTimeout(time.Duration(conf.RequestTimeout) * time.Second)
dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx2, cancel := context.WithTimeout(ctx, time.Duration(conf.RequestTimeout)*time.Second)

1
go.mod

@ -16,7 +16,6 @@ require (
github.com/lib/pq v1.10.3
github.com/lithammer/shortuuid v2.0.3+incompatible
github.com/lithammer/shortuuid/v3 v3.0.7
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/onsi/gomega v1.16.0
github.com/prometheus/client_golang v1.11.0
github.com/stretchr/testify v1.7.0

2
main.go

@ -59,7 +59,7 @@ func main() {
}
config.MustLoadConfig(*confFile)
if *isDebug {
config.Config.Log.Level = "debug"
config.Config.LogLevel = "debug"
}
if *isReset {
dtmsvr.PopulateDB(false)

Loading…
Cancel
Save