mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
log: remove unused code, un-exports other code, clean up
This commit is contained in:
parent
dd0d0a2522
commit
7d4613f39f
4 changed files with 21 additions and 51 deletions
|
|
@ -241,11 +241,11 @@ func Setup(ctx *cli.Context) error {
|
||||||
case ctx.Bool(logjsonFlag.Name):
|
case ctx.Bool(logjsonFlag.Name):
|
||||||
// Retain backwards compatibility with `--log.json` flag if `--log.format` not set
|
// Retain backwards compatibility with `--log.json` flag if `--log.format` not set
|
||||||
defer log.Warn("The flag '--log.json' is deprecated, please use '--log.format=json' instead")
|
defer log.Warn("The flag '--log.json' is deprecated, please use '--log.format=json' instead")
|
||||||
handler = log.JSONHandler(output)
|
handler = log.NewJSONHandler(output)
|
||||||
case logFmtFlag == "json":
|
case logFmtFlag == "json":
|
||||||
handler = log.JSONHandler(output)
|
handler = log.NewJSONHandler(output)
|
||||||
case logFmtFlag == "logfmt":
|
case logFmtFlag == "logfmt":
|
||||||
handler = log.LogfmtHandler(output)
|
handler = log.NewLogfmtHandler(output)
|
||||||
case logFmtFlag == "", logFmtFlag == "terminal":
|
case logFmtFlag == "", logFmtFlag == "terminal":
|
||||||
useColor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
useColor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
||||||
if useColor {
|
if useColor {
|
||||||
|
|
|
||||||
|
|
@ -183,7 +183,7 @@ func (h *bufHandler) terminalFormat(r slog.Record) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, attr := range attrs {
|
for _, attr := range attrs {
|
||||||
fmt.Fprintf(buf, " %s=%s", attr.Key, string(log.FormatSlogValue(attr.Value, true, nil)))
|
fmt.Fprintf(buf, " %s=%s", attr.Key, string(log.FormatSlogValue(attr.Value, nil)))
|
||||||
}
|
}
|
||||||
buf.WriteByte('\n')
|
buf.WriteByte('\n')
|
||||||
return buf.String()
|
return buf.String()
|
||||||
|
|
|
||||||
|
|
@ -23,22 +23,6 @@ const (
|
||||||
// 40 spaces
|
// 40 spaces
|
||||||
var spaces = []byte(" ")
|
var spaces = []byte(" ")
|
||||||
|
|
||||||
type Format interface {
|
|
||||||
Format(r slog.Record) []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// FormatFunc returns a new Format object which uses
|
|
||||||
// the given function to perform record formatting.
|
|
||||||
func FormatFunc(f func(slog.Record) []byte) Format {
|
|
||||||
return formatFunc(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
type formatFunc func(slog.Record) []byte
|
|
||||||
|
|
||||||
func (f formatFunc) Format(r slog.Record) []byte {
|
|
||||||
return f(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TerminalStringer is an analogous interface to the stdlib stringer, allowing
|
// TerminalStringer is an analogous interface to the stdlib stringer, allowing
|
||||||
// own types to have custom shortened serialization formats when printed to the
|
// own types to have custom shortened serialization formats when printed to the
|
||||||
// screen.
|
// screen.
|
||||||
|
|
@ -46,7 +30,7 @@ type TerminalStringer interface {
|
||||||
TerminalString() string
|
TerminalString() string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *TerminalHandler) TerminalFormat(buf []byte, r slog.Record, usecolor bool) []byte {
|
func (h *TerminalHandler) format(buf []byte, r slog.Record, usecolor bool) []byte {
|
||||||
msg := escapeMessage(r.Message)
|
msg := escapeMessage(r.Message)
|
||||||
var color = ""
|
var color = ""
|
||||||
if usecolor {
|
if usecolor {
|
||||||
|
|
@ -88,13 +72,13 @@ func (h *TerminalHandler) TerminalFormat(buf []byte, r slog.Record, usecolor boo
|
||||||
if (r.NumAttrs()+len(h.attrs)) > 0 && length < termMsgJust {
|
if (r.NumAttrs()+len(h.attrs)) > 0 && length < termMsgJust {
|
||||||
b.Write(spaces[:termMsgJust-length])
|
b.Write(spaces[:termMsgJust-length])
|
||||||
}
|
}
|
||||||
// print the keys logfmt style
|
// print the attributes
|
||||||
h.logfmt(b, r, color)
|
h.formatAttributes(b, r, color)
|
||||||
|
|
||||||
return b.Bytes()
|
return b.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *TerminalHandler) logfmt(buf *bytes.Buffer, r slog.Record, color string) {
|
func (h *TerminalHandler) formatAttributes(buf *bytes.Buffer, r slog.Record, color string) {
|
||||||
// tmp is a temporary buffer we use, until bytes.Buffer.AvailableBuffer() (1.21)
|
// tmp is a temporary buffer we use, until bytes.Buffer.AvailableBuffer() (1.21)
|
||||||
// can be used.
|
// can be used.
|
||||||
var tmp = make([]byte, 40)
|
var tmp = make([]byte, 40)
|
||||||
|
|
@ -112,7 +96,7 @@ func (h *TerminalHandler) logfmt(buf *bytes.Buffer, r slog.Record, color string)
|
||||||
buf.WriteByte('=')
|
buf.WriteByte('=')
|
||||||
}
|
}
|
||||||
//val := FormatSlogValue(attr.Value, true, buf.AvailableBuffer())
|
//val := FormatSlogValue(attr.Value, true, buf.AvailableBuffer())
|
||||||
val := FormatSlogValue(attr.Value, true, tmp[:0])
|
val := FormatSlogValue(attr.Value, tmp[:0])
|
||||||
|
|
||||||
padding := h.fieldPadding[attr.Key]
|
padding := h.fieldPadding[attr.Key]
|
||||||
|
|
||||||
|
|
@ -140,8 +124,8 @@ func (h *TerminalHandler) logfmt(buf *bytes.Buffer, r slog.Record, color string)
|
||||||
buf.WriteByte('\n')
|
buf.WriteByte('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatSlogValue formats a slog.Value for serialization
|
// FormatSlogValue formats a slog.Value for serialization to terminal.
|
||||||
func FormatSlogValue(v slog.Value, term bool, tmp []byte) (result []byte) {
|
func FormatSlogValue(v slog.Value, tmp []byte) (result []byte) {
|
||||||
var value any
|
var value any
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
|
|
@ -156,11 +140,9 @@ func FormatSlogValue(v slog.Value, term bool, tmp []byte) (result []byte) {
|
||||||
switch v.Kind() {
|
switch v.Kind() {
|
||||||
case slog.KindString:
|
case slog.KindString:
|
||||||
return appendEscapeString(tmp, v.String())
|
return appendEscapeString(tmp, v.String())
|
||||||
case slog.KindAny:
|
case slog.KindInt64: // All int-types (int8, int16 etc) wind up here
|
||||||
value = v.Any()
|
|
||||||
case slog.KindInt64: // All int-types (int8 ,int16 etc) wind up here
|
|
||||||
return appendInt64(tmp, v.Int64())
|
return appendInt64(tmp, v.Int64())
|
||||||
case slog.KindUint64: // All uint-types (int8 ,int16 etc) wind up here
|
case slog.KindUint64: // All uint-types (uint8, uint16 etc) wind up here
|
||||||
return appendUint64(tmp, v.Uint64(), false)
|
return appendUint64(tmp, v.Uint64(), false)
|
||||||
case slog.KindFloat64:
|
case slog.KindFloat64:
|
||||||
return strconv.AppendFloat(tmp, v.Float64(), floatFormat, 3, 64)
|
return strconv.AppendFloat(tmp, v.Float64(), floatFormat, 3, 64)
|
||||||
|
|
@ -180,27 +162,14 @@ func FormatSlogValue(v slog.Value, term bool, tmp []byte) (result []byte) {
|
||||||
return []byte("<nil>")
|
return []byte("<nil>")
|
||||||
}
|
}
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case *big.Int:
|
case *big.Int: // Need to be before fmt.Stringer-clause
|
||||||
// Big ints get consumed by the Stringer clause, so we need to handle
|
|
||||||
// them earlier on.
|
|
||||||
if v == nil {
|
|
||||||
return append(tmp, []byte("<nil>")...)
|
|
||||||
}
|
|
||||||
return appendBigInt(tmp, v)
|
return appendBigInt(tmp, v)
|
||||||
|
case *uint256.Int: // Need to be before fmt.Stringer-clause
|
||||||
case *uint256.Int:
|
|
||||||
// Uint256s get consumed by the Stringer clause, so we need to handle
|
|
||||||
// them earlier on.
|
|
||||||
if v == nil {
|
|
||||||
return append(tmp, []byte("<nil>")...)
|
|
||||||
}
|
|
||||||
return appendU256(tmp, v)
|
return appendU256(tmp, v)
|
||||||
case error:
|
case error:
|
||||||
return appendEscapeString(tmp, v.Error())
|
return appendEscapeString(tmp, v.Error())
|
||||||
case TerminalStringer:
|
case TerminalStringer:
|
||||||
if term {
|
return appendEscapeString(tmp, v.TerminalString())
|
||||||
return appendEscapeString(tmp, v.TerminalString()) // Custom terminal stringer provided, use that
|
|
||||||
}
|
|
||||||
case fmt.Stringer:
|
case fmt.Stringer:
|
||||||
return appendEscapeString(tmp, v.String())
|
return appendEscapeString(tmp, v.String())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -112,7 +112,7 @@ func NewTerminalHandlerWithLevel(wr io.Writer, lvl slog.Level, useColor bool) *T
|
||||||
func (h *TerminalHandler) Handle(_ context.Context, r slog.Record) error {
|
func (h *TerminalHandler) Handle(_ context.Context, r slog.Record) error {
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
buf := h.TerminalFormat(h.buf, r, h.useColor)
|
buf := h.format(h.buf, r, h.useColor)
|
||||||
h.wr.Write(buf)
|
h.wr.Write(buf)
|
||||||
h.buf = buf[:0]
|
h.buf = buf[:0]
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -149,17 +149,18 @@ func (l *leveler) Level() slog.Level {
|
||||||
return l.minLevel
|
return l.minLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
func JSONHandler(wr io.Writer) slog.Handler {
|
// NewJSONHandler returns a handler which prints records in JSON format.
|
||||||
|
func NewJSONHandler(wr io.Writer) slog.Handler {
|
||||||
return slog.NewJSONHandler(wr, &slog.HandlerOptions{
|
return slog.NewJSONHandler(wr, &slog.HandlerOptions{
|
||||||
ReplaceAttr: builtinReplaceJSON,
|
ReplaceAttr: builtinReplaceJSON,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogfmtHandler returns a handler which prints records in logfmt format, an easy machine-parseable but human-readable
|
// NewLogfmtHandler returns a handler which prints records in logfmt format, an easy machine-parseable but human-readable
|
||||||
// format for key/value pairs.
|
// format for key/value pairs.
|
||||||
//
|
//
|
||||||
// For more details see: http://godoc.org/github.com/kr/logfmt
|
// For more details see: http://godoc.org/github.com/kr/logfmt
|
||||||
func LogfmtHandler(wr io.Writer) slog.Handler {
|
func NewLogfmtHandler(wr io.Writer) slog.Handler {
|
||||||
return slog.NewTextHandler(wr, &slog.HandlerOptions{
|
return slog.NewTextHandler(wr, &slog.HandlerOptions{
|
||||||
ReplaceAttr: builtinReplaceLogfmt,
|
ReplaceAttr: builtinReplaceLogfmt,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue