Merge pull request #4 from kilnfi/isma/fix-logger

fix: replace logger
This commit is contained in:
Isma 2023-12-22 15:32:08 +01:00 committed by GitHub
commit 4967a17566
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 80 deletions

View file

@ -1,71 +0,0 @@
package log
import (
"bytes"
"fmt"
"strings"
"unicode/utf8"
)
func PgethFormat(usecolor bool) Format {
return FormatFunc(func(r *Record) []byte {
var color = 0
if usecolor {
switch r.Lvl {
case LvlCrit:
color = 35
case LvlError:
color = 31
case LvlWarn:
color = 33
case LvlInfo:
color = 34
case LvlDebug:
color = 36
case LvlTrace:
color = 34
}
}
b := &bytes.Buffer{}
lvl := r.Lvl.AlignedString()
if lvl == "INFO " {
lvl = "PLUG "
}
if locationEnabled.Load() {
// Log origin printing was requested, format the location path and line number
location := fmt.Sprintf("%+v", r.Call)
for _, prefix := range locationTrims {
location = strings.TrimPrefix(location, prefix)
}
// Maintain the maximum location length for fancyer alignment
align := int(locationLength.Load())
if align < len(location) {
align = len(location)
locationLength.Store(uint32(align))
}
padding := strings.Repeat(" ", align-len(location))
// Assemble and print the log heading
if color > 0 {
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s|%s]%s %s ", color, lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg)
} else {
fmt.Fprintf(b, "%s[%s|%s]%s %s ", lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg)
}
} else {
if color > 0 {
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg)
} else {
fmt.Fprintf(b, "%s[%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg)
}
}
// try to justify the log output for short messages
length := utf8.RuneCountInString(r.Msg)
if len(r.Ctx) > 0 && length < termMsgJust {
b.Write(bytes.Repeat([]byte{' '}, termMsgJust-length))
}
// print the keys logfmt style
logfmt(b, r.Ctx, color, true)
return b.Bytes()
})
}

View file

@ -6,11 +6,11 @@ import (
"os" "os"
"runtime/debug" "runtime/debug"
"github.com/mattn/go-colorable"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
log "github.com/sirupsen/logrus"
"github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/pgeth/toolkit" "github.com/ethereum/go-ethereum/pgeth/toolkit"
pgeth_monitoring "github.com/ethereum/go-ethereum/plugins/pgeth-monitoring" pgeth_monitoring "github.com/ethereum/go-ethereum/plugins/pgeth-monitoring"
@ -35,16 +35,12 @@ type PluginEngineConfig struct {
type PluginEngine struct { type PluginEngine struct {
node *node.Node node *node.Node
backend ethapi.Backend backend ethapi.Backend
logger log.Logger logger *log.Logger
} }
func NewEngine(cfg *PluginEngineConfig) *PluginEngine { func NewEngine(cfg *PluginEngineConfig) *PluginEngine {
logger := log.New() logger := log.New()
var ostream log.Handler
output := colorable.NewColorableStdout()
ostream = log.StreamHandler(output, log.PgethFormat(true))
logger.SetHandler(ostream)
return &PluginEngine{ return &PluginEngine{
node: cfg.Node, node: cfg.Node,
backend: cfg.Backend, backend: cfg.Backend,

View file

@ -1,13 +1,14 @@
package toolkit package toolkit
import ( import (
log "github.com/sirupsen/logrus"
"github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
) )
type PluginToolkit struct { type PluginToolkit struct {
Node *node.Node Node *node.Node
Backend ethapi.Backend Backend ethapi.Backend
Logger log.Logger Logger *log.Logger
} }