Commit d0ffd216 authored by suyanlong's avatar suyanlong

Add log package

parent 3098180c
Pipeline #8008 failed with stages
...@@ -5,8 +5,9 @@ import ( ...@@ -5,8 +5,9 @@ import (
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/meshplus/bitxhub-kit/log"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/link33/sidecar/pkg/log"
) )
var logger = log.NewWithModule("cmd") var logger = log.NewWithModule("cmd")
......
...@@ -11,13 +11,13 @@ import ( ...@@ -11,13 +11,13 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/meshplus/bitxhub-kit/log"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/link33/sidecar/internal" "github.com/link33/sidecar/internal"
"github.com/link33/sidecar/internal/app" "github.com/link33/sidecar/internal/app"
"github.com/link33/sidecar/internal/loggers" "github.com/link33/sidecar/internal/loggers"
"github.com/link33/sidecar/internal/repo" "github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/pkg/log"
) )
var startCMD = cli.Command{ var startCMD = cli.Command{
......
...@@ -6,7 +6,8 @@ import ( ...@@ -6,7 +6,8 @@ import (
"github.com/meshplus/bitxhub-kit/crypto" "github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/fileutil" "github.com/meshplus/bitxhub-kit/fileutil"
"github.com/meshplus/bitxhub-kit/log"
"github.com/link33/sidecar/pkg/log"
) )
const ( const (
......
...@@ -9,11 +9,10 @@ import ( ...@@ -9,11 +9,10 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
crypto "github.com/meshplus/bitxhub-kit/crypto"
types "github.com/meshplus/bitxhub-kit/types"
rpcx "github.com/link33/sidecar/hub/client" rpcx "github.com/link33/sidecar/hub/client"
pb "github.com/link33/sidecar/model/pb" pb "github.com/link33/sidecar/model/pb"
crypto "github.com/meshplus/bitxhub-kit/crypto"
types "github.com/meshplus/bitxhub-kit/types"
) )
// MockClient is a mock of Client interface. // MockClient is a mock of Client interface.
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
pb "github.com/link33/sidecar/model/pb" pb "github.com/link33/sidecar/model/pb"
) )
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
pb "github.com/link33/sidecar/model/pb" pb "github.com/link33/sidecar/model/pb"
) )
......
package loggers package loggers
import ( import (
"github.com/meshplus/bitxhub-kit/log"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/link33/sidecar/internal/repo" "github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/pkg/log"
) )
const ( const (
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
peer "github.com/libp2p/go-libp2p-core/peer" peer "github.com/libp2p/go-libp2p-core/peer"
peermgr "github.com/link33/sidecar/internal/peermgr" peermgr "github.com/link33/sidecar/internal/peermgr"
port "github.com/link33/sidecar/internal/port" port "github.com/link33/sidecar/internal/port"
pb "github.com/link33/sidecar/model/pb" pb "github.com/link33/sidecar/model/pb"
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
peer2 "github.com/libp2p/go-libp2p-core/peer" peer2 "github.com/libp2p/go-libp2p-core/peer"
"github.com/meshplus/bitxhub-kit/crypto" "github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/asym" "github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/meshplus/bitxhub-kit/log"
network "github.com/meshplus/go-lightp2p" network "github.com/meshplus/go-lightp2p"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -18,6 +17,7 @@ import ( ...@@ -18,6 +17,7 @@ import (
"github.com/link33/sidecar/internal/repo" "github.com/link33/sidecar/internal/repo"
router2 "github.com/link33/sidecar/internal/router" router2 "github.com/link33/sidecar/internal/router"
"github.com/link33/sidecar/model/pb" "github.com/link33/sidecar/model/pb"
"github.com/link33/sidecar/pkg/log"
) )
var routerTmp router2.Router var routerTmp router2.Router
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
port "github.com/link33/sidecar/internal/port" port "github.com/link33/sidecar/internal/port"
pb "github.com/link33/sidecar/model/pb" pb "github.com/link33/sidecar/model/pb"
) )
......
...@@ -8,10 +8,9 @@ import ( ...@@ -8,10 +8,9 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
appchain_mgr "github.com/meshplus/bitxhub-core/appchain-mgr"
syncer "github.com/link33/sidecar/internal/syncer" syncer "github.com/link33/sidecar/internal/syncer"
pb "github.com/link33/sidecar/model/pb" pb "github.com/link33/sidecar/model/pb"
appchain_mgr "github.com/meshplus/bitxhub-core/appchain-mgr"
) )
// MockSyncer is a mock of Syncer interface. // MockSyncer is a mock of Syncer interface.
......
...@@ -18,7 +18,6 @@ import ( ...@@ -18,7 +18,6 @@ import (
"github.com/meshplus/bitxhub-core/governance" "github.com/meshplus/bitxhub-core/governance"
"github.com/meshplus/bitxhub-kit/crypto" "github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/asym" "github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/meshplus/bitxhub-kit/log"
"github.com/meshplus/bitxhub-kit/storage/leveldb" "github.com/meshplus/bitxhub-kit/storage/leveldb"
"github.com/meshplus/bitxhub-kit/types" "github.com/meshplus/bitxhub-kit/types"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -28,6 +27,7 @@ import ( ...@@ -28,6 +27,7 @@ import (
"github.com/link33/sidecar/internal/repo" "github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/model/constant" "github.com/link33/sidecar/model/constant"
"github.com/link33/sidecar/model/pb" "github.com/link33/sidecar/model/pb"
"github.com/link33/sidecar/pkg/log"
) )
const ( const (
......
package log
import "time"
type config struct {
reportCaller bool
persist bool
filePath string
fileName string
maxSize int64
maxAge time.Duration
rotationTime time.Duration
}
type Option func(*config)
func WithReportCaller(reportCaller bool) Option {
return func(c *config) {
c.reportCaller = reportCaller
}
}
func WithPersist(persist bool) Option {
return func(c *config) {
c.persist = persist
}
}
func WithFilePath(filePath string) Option {
return func(c *config) {
c.filePath = filePath
}
}
func WithFileName(fileName string) Option {
return func(c *config) {
c.fileName = fileName
}
}
func WithMaxSize(maxSize int64) Option {
return func(c *config) {
c.maxSize = maxSize
}
}
func WithMaxAge(maxAge time.Duration) Option {
return func(c *config) {
c.maxAge = maxAge
}
}
func WithRotationTime(rotationTime time.Duration) Option {
return func(c *config) {
c.rotationTime = rotationTime
}
}
func defaultConfig() *config {
return &config{
reportCaller: false,
persist: false,
filePath: "./",
fileName: "log",
maxSize: 2 * 1024 * 1024,
maxAge: 2 * time.Hour,
rotationTime: 24 * time.Hour,
}
}
func generateConfig(opts ...Option) *config {
config := defaultConfig()
for _, opt := range opts {
opt(config)
}
return config
}
package log
import (
"path"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
)
func newRotateHook(logPath string, logFileName string, maxAge time.Duration, rotationTime time.Duration) *lfshook.LfsHook {
baseLogName := path.Join(logPath, logFileName)
writer, err := rotatelogs.New(
baseLogName+"%Y%m%d%H%M%S",
rotatelogs.WithLinkName(baseLogName),
rotatelogs.WithMaxAge(maxAge),
rotatelogs.WithRotationTime(rotationTime),
)
if err != nil {
logrus.Errorf("config local file system logger error. %s", err)
return nil
}
return lfshook.NewHook(lfshook.WriterMap{
logrus.DebugLevel: writer,
logrus.InfoLevel: writer,
logrus.WarnLevel: writer,
logrus.ErrorLevel: writer,
logrus.FatalLevel: writer,
logrus.PanicLevel: writer,
}, getTextFormatter())
}
package log
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/sirupsen/logrus"
)
type loggerContext struct {
loggers map[string]logrus.FieldLogger
config *config
hooks []logrus.Hook
}
var loggerCtx = defaultLoggerContext()
func defaultLoggerContext() *loggerContext {
return &loggerContext{
loggers: make(map[string]logrus.FieldLogger),
config: defaultConfig(),
hooks: make([]logrus.Hook, 0),
}
}
func New() *logrus.Logger {
logger := logrus.New()
formatter := getTextFormatter()
logger.SetFormatter(formatter)
logger.SetReportCaller(loggerCtx.config.reportCaller)
logger.SetOutput(os.Stdout)
for _, hook := range loggerCtx.hooks {
logger.AddHook(hook)
}
return logger
}
func NewWithModule(name string) *logrus.Entry {
logger := New()
l := logger.WithField("module", name)
loggerCtx.loggers[name] = l
return l
}
func ParseLevel(level string) logrus.Level {
lvl, err := logrus.ParseLevel(level)
if err != nil {
lvl = logrus.ErrorLevel
}
return lvl
}
// Initialize initializes a logger instance with given
// level, filepath, filename, maxSize, maxAge and rotationTime.
func Initialize(opts ...Option) error {
config := generateConfig(opts...)
loggerCtx.config = config
if err := os.MkdirAll(config.filePath, os.ModePerm); err != nil {
return fmt.Errorf("create file path: %w", err)
}
if config.persist {
rotation := newRotateHook(config.filePath, config.fileName, config.maxAge, config.rotationTime)
loggerCtx.hooks = append(loggerCtx.hooks, rotation)
}
return nil
}
func getTextFormatter() logrus.Formatter {
return &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02T15:04:05.000",
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
_, filename := filepath.Split(f.File)
return "", fmt.Sprintf("%12s:%-4d", filename, f.Line)
},
}
}
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
pb "github.com/link33/sidecar/model/pb" pb "github.com/link33/sidecar/model/pb"
plugins "github.com/link33/sidecar/pkg/plugins" plugins "github.com/link33/sidecar/pkg/plugins"
) )
......
...@@ -44,7 +44,7 @@ func (p *AppchainGRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Serve ...@@ -44,7 +44,7 @@ func (p *AppchainGRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Serve
func (p *AppchainGRPCPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) { func (p *AppchainGRPCPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{ return &GRPCClient{
client: pb.NewAppchainPluginClient(c), // 创建gRPC客户端的方法是自动生成的 client: pb.NewAppchainPluginClient(c), // 创建gRPC客户端的方法是自动生成的
DoneContext: ctx, doneContext: ctx,
}, nil }, nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment