Commit 9a7bb3e2 authored by suyanlong's avatar suyanlong

Add log package

parent 62da68a3
...@@ -7,18 +7,19 @@ import ( ...@@ -7,18 +7,19 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/hashicorp/go-hclog"
"github.com/33cn/chain33/common/address" "github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/db" "github.com/33cn/chain33/common/db"
types33 "github.com/33cn/chain33/types" types33 "github.com/33cn/chain33/types"
evmxgotypes "github.com/33cn/plugin/plugin/dapp/evmxgo/types" evmxgotypes "github.com/33cn/plugin/plugin/dapp/evmxgo/types"
"github.com/sirupsen/logrus"
"github.com/spf13/cast" "github.com/spf13/cast"
"gitlab.33.cn/link33/sidecar-client-chain33/client/rpc" "gitlab.33.cn/link33/sidecar-client-chain33/client/rpc"
"gitlab.33.cn/link33/sidecar-client-chain33/config" "gitlab.33.cn/link33/sidecar-client-chain33/config"
"gitlab.33.cn/link33/sidecar-client-chain33/types" "gitlab.33.cn/link33/sidecar-client-chain33/types"
"gitlab.33.cn/link33/sidecar-client-chain33/util" "gitlab.33.cn/link33/sidecar-client-chain33/util"
"gitlab.33.cn/link33/sidecar-client-chain33/util/log"
"gitlab.33.cn/link33/sidecar/model/pb" "gitlab.33.cn/link33/sidecar/model/pb"
"gitlab.33.cn/link33/sidecar/pkg/log"
"gitlab.33.cn/link33/sidecar/pkg/plugins" "gitlab.33.cn/link33/sidecar/pkg/plugins"
"go.uber.org/atomic" "go.uber.org/atomic"
) )
...@@ -37,8 +38,7 @@ var ( ...@@ -37,8 +38,7 @@ var (
PluginName = evmxgotypes.EvmxgoX PluginName = evmxgotypes.EvmxgoX
ID = evmxgotypes.EvmxgoX ID = evmxgotypes.EvmxgoX
execer = evmxgotypes.EvmxgoX execer = evmxgotypes.EvmxgoX
logger *logrus.Entry logger hclog.Logger
// logger = log.NewWithModuleLevel("chain33-client-"+PluginName, "trace")
) )
type Evmxgo struct { type Evmxgo struct {
...@@ -97,7 +97,7 @@ func (e *Evmxgo) Initialize(configPath string, _ string, _ []byte) error { ...@@ -97,7 +97,7 @@ func (e *Evmxgo) Initialize(configPath string, _ string, _ []byte) error {
if err != nil { if err != nil {
return fmt.Errorf("unmarshal config for plugin :%w", err) return fmt.Errorf("unmarshal config for plugin :%w", err)
} }
err = log.Initialize( out, err := log.Initialize(
log.WithReportCaller(chain33Config.Log.ReportCaller == true), log.WithReportCaller(chain33Config.Log.ReportCaller == true),
log.WithPersist(true), log.WithPersist(true),
log.WithFilePath(filepath.Join(filepath.Dir(configPath), "logs")), log.WithFilePath(filepath.Join(filepath.Dir(configPath), "logs")),
...@@ -109,7 +109,11 @@ func (e *Evmxgo) Initialize(configPath string, _ string, _ []byte) error { ...@@ -109,7 +109,11 @@ func (e *Evmxgo) Initialize(configPath string, _ string, _ []byte) error {
if err != nil { if err != nil {
return fmt.Errorf("log initialize: %w", err) return fmt.Errorf("log initialize: %w", err)
} }
logger = log.NewWithModuleLevel("chain33-client-"+PluginName, chain33Config.Log.Level) logger = hclog.New(&hclog.LoggerOptions{
Name: "chain33-client-" + PluginName,
Output: out,
Level: hclog.LevelFromString(chain33Config.Log.Level),
})
// 初始化e.client工作 // 初始化e.client工作
e.eventC = make(chan *pb.IBTP, 10) e.eventC = make(chan *pb.IBTP, 10)
e.done = make(chan bool) e.done = make(chan bool)
......
...@@ -11,10 +11,6 @@ import ( ...@@ -11,10 +11,6 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/sirupsen/logrus"
"gitlab.33.cn/link33/sidecar/pkg/log"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address" "github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/crypto" "github.com/33cn/chain33/common/crypto"
...@@ -24,10 +20,12 @@ import ( ...@@ -24,10 +20,12 @@ import (
util33 "github.com/33cn/chain33/util" util33 "github.com/33cn/chain33/util"
"github.com/33cn/plugin/plugin/dapp/paracross/executor" "github.com/33cn/plugin/plugin/dapp/paracross/executor"
paracorssTypes "github.com/33cn/plugin/plugin/dapp/paracross/types" paracorssTypes "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/hashicorp/go-hclog"
"github.com/spf13/cast" "github.com/spf13/cast"
"gitlab.33.cn/link33/sidecar-client-chain33/config" "gitlab.33.cn/link33/sidecar-client-chain33/config"
"gitlab.33.cn/link33/sidecar-client-chain33/types" "gitlab.33.cn/link33/sidecar-client-chain33/types"
"gitlab.33.cn/link33/sidecar-client-chain33/util" "gitlab.33.cn/link33/sidecar-client-chain33/util"
"gitlab.33.cn/link33/sidecar-client-chain33/util/log"
"gitlab.33.cn/link33/sidecar/model/pb" "gitlab.33.cn/link33/sidecar/model/pb"
"gitlab.33.cn/link33/sidecar/pkg/plugins" "gitlab.33.cn/link33/sidecar/pkg/plugins"
"go.uber.org/atomic" "go.uber.org/atomic"
...@@ -38,7 +36,7 @@ var ( ...@@ -38,7 +36,7 @@ var (
PluginName = paracorssTypes.ParaX PluginName = paracorssTypes.ParaX
ID = paracorssTypes.ParaX ID = paracorssTypes.ParaX
execer = paracorssTypes.ParaX execer = paracorssTypes.ParaX
logger *logrus.Entry logger hclog.Logger
) )
type Paracross struct { type Paracross struct {
...@@ -118,7 +116,7 @@ func (p *Paracross) Initialize(configPath string, ID string, extra []byte) error ...@@ -118,7 +116,7 @@ func (p *Paracross) Initialize(configPath string, ID string, extra []byte) error
if err != nil { if err != nil {
return fmt.Errorf("unmarshal config for plugin :%w", err) return fmt.Errorf("unmarshal config for plugin :%w", err)
} }
err = log.Initialize( out, err := log.Initialize(
log.WithReportCaller(chain33Config.Log.ReportCaller == true), log.WithReportCaller(chain33Config.Log.ReportCaller == true),
log.WithPersist(true), log.WithPersist(true),
log.WithFilePath(filepath.Join(filepath.Dir(configPath), "logs")), log.WithFilePath(filepath.Join(filepath.Dir(configPath), "logs")),
...@@ -130,7 +128,11 @@ func (p *Paracross) Initialize(configPath string, ID string, extra []byte) error ...@@ -130,7 +128,11 @@ func (p *Paracross) Initialize(configPath string, ID string, extra []byte) error
if err != nil { if err != nil {
return fmt.Errorf("log initialize: %w", err) return fmt.Errorf("log initialize: %w", err)
} }
logger = log.NewWithModuleLevel("chain33-client-"+PluginName, chain33Config.Log.Level) logger = hclog.New(&hclog.LoggerOptions{
Name: "chain33-client-" + PluginName,
Output: out,
Level: hclog.LevelFromString(chain33Config.Log.Level),
})
// 初始化e.client工作 // 初始化e.client工作
p.eventC = make(chan *pb.IBTP, 10) p.eventC = make(chan *pb.IBTP, 10)
......
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 (
"fmt"
"io"
"os"
"path"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
)
// Initialize initializes a logger instance with given
// level, filepath, filename, maxSize, maxAge and rotationTime.
func Initialize(opts ...Option) (io.Writer, error) {
config := generateConfig(opts...)
if err := os.MkdirAll(config.filePath, os.ModePerm); err != nil {
return nil, fmt.Errorf("create file path: %w", err)
}
o := &out{
fileOut: nil,
stdout: os.Stdout,
}
if config.persist {
baseLogName := path.Join(config.filePath, config.fileName)
writer, err := rotatelogs.New(
baseLogName+"%Y%m%d%H%M%S",
rotatelogs.WithLinkName(baseLogName),
rotatelogs.WithMaxAge(config.maxAge),
rotatelogs.WithRotationTime(config.rotationTime),
)
if err != nil {
return nil, err
}
o.fileOut = writer
}
return o, nil
}
type out struct {
fileOut io.Writer
stdout io.Writer
}
func (o *out) Write(p []byte) (n int, err error) {
_, _ = o.stdout.Write(p)
return o.fileOut.Write(p)
}
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