Skip to content

Commit 5aae9a5

Browse files
Improve json logging (#219)
1 parent eee9ef7 commit 5aae9a5

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

plugin/evm/log.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
package evm
55

66
import (
7+
"encoding/json"
78
"fmt"
89
"io"
10+
"reflect"
11+
"time"
912

1013
"github.com/ethereum/go-ethereum/log"
1114
)
1215

16+
const (
17+
errorKey = "LOG15_ERROR"
18+
timeFormat = "2006-01-02T15:04:05-0700"
19+
)
20+
1321
type SubnetEVMLogger struct {
1422
log.Handler
1523
}
@@ -56,12 +64,56 @@ func SubnetEVMTermFormat(alias string) log.Format {
5664
func SubnetEVMJSONFormat(alias string) log.Format {
5765
prefix := fmt.Sprintf("%s Chain", alias)
5866
return log.FormatFunc(func(r *log.Record) []byte {
59-
location := fmt.Sprintf("%+v", r.Call)
60-
r.KeyNames.Lvl = "level"
61-
r.KeyNames.Time = "timestamp"
62-
r.Ctx = append(r.Ctx, "logger", prefix)
63-
r.Ctx = append(r.Ctx, "caller", location)
67+
props := make(map[string]interface{}, 5+len(r.Ctx)/2)
68+
props["timestamp"] = r.Time
69+
props["level"] = r.Lvl.String()
70+
props[r.KeyNames.Msg] = r.Msg
71+
props["logger"] = prefix
72+
props["caller"] = fmt.Sprintf("%+v", r.Call)
73+
for i := 0; i < len(r.Ctx); i += 2 {
74+
k, ok := r.Ctx[i].(string)
75+
if !ok {
76+
props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i])
77+
} else {
78+
props[k] = formatJSONValue(r.Ctx[i+1])
79+
}
80+
}
6481

65-
return log.JSONFormat().Format(r)
82+
b, err := json.Marshal(props)
83+
if err != nil {
84+
b, _ = json.Marshal(map[string]string{
85+
errorKey: err.Error(),
86+
})
87+
return b
88+
}
89+
90+
b = append(b, '\n')
91+
return b
6692
})
6793
}
94+
95+
func formatJSONValue(value interface{}) (result interface{}) {
96+
defer func() {
97+
if err := recover(); err != nil {
98+
if v := reflect.ValueOf(value); v.Kind() == reflect.Ptr && v.IsNil() {
99+
result = "nil"
100+
} else {
101+
panic(err)
102+
}
103+
}
104+
}()
105+
106+
switch v := value.(type) {
107+
case time.Time:
108+
return v.Format(timeFormat)
109+
110+
case error:
111+
return v.Error()
112+
113+
case fmt.Stringer:
114+
return v.String()
115+
116+
default:
117+
return v
118+
}
119+
}

plugin/evm/vm.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,7 @@ func (vm *VM) Initialize(
224224
}
225225
vm.logger = subnetEVMLogger
226226

227-
if b, err := json.Marshal(vm.config); err == nil {
228-
log.Info("Initializing Subnet EVM VM", "Version", Version, "Config", string(b))
229-
} else {
230-
// Log a warning message since we have already successfully unmarshalled into the struct
231-
log.Warn("Problem initializing Subnet EVM VM", "Version", Version, "Config", string(b), "err", err)
232-
}
227+
log.Info("Initializing Subnet EVM VM", "Version", Version, "Config", vm.config)
233228

234229
if len(fxs) > 0 {
235230
return errUnsupportedFXs

0 commit comments

Comments
 (0)