More log refactoring

This commit is contained in:
Thomas E Lackey 2023-01-20 13:21:43 -06:00
parent 903b855508
commit c2a5b012cf
7 changed files with 53 additions and 22 deletions

View File

@ -17,12 +17,8 @@ package main
import (
"github.com/cerc-io/ipld-eth-server/v4/cmd"
"github.com/sirupsen/logrus"
)
func main() {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
cmd.Execute()
}

View File

@ -17,8 +17,10 @@
package eth_test
import (
"io/ioutil"
"testing"
"github.com/cerc-io/ipld-eth-server/v4/pkg/log"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@ -29,4 +31,5 @@ func TestETHSuite(t *testing.T) {
}
var _ = BeforeSuite(func() {
log.SetOutput(ioutil.Discard)
})

View File

@ -17,8 +17,10 @@
package graphql_test
import (
"io/ioutil"
"testing"
"github.com/cerc-io/ipld-eth-server/v4/pkg/log"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@ -29,4 +31,5 @@ func TestGraphQL(t *testing.T) {
}
var _ = BeforeSuite(func() {
log.SetOutput(ioutil.Discard)
})

View File

@ -3,6 +3,7 @@ package log
import (
"context"
"fmt"
"io"
"os"
"runtime"
"strings"
@ -13,8 +14,9 @@ import (
const (
CtxKeyUniqId = "id"
CtxKeyApiMethod = "method"
CtxKeyReqId = "reqid"
CtxKeyApiMethod = "api_method"
CtxKeyApiParams = "api_params"
CtxKeyApiReqId = "api_reqid"
CtxKeyUserId = "user_id"
CtxKeyConn = "conn"
CtxKeyDuration = "duration"
@ -25,13 +27,14 @@ const (
// TODO: Allow registering arbitrary keys.
var registeredKeys = []string{
CtxKeyApiMethod,
CtxKeyReqId,
CtxKeyUserId,
CtxKeyApiParams,
CtxKeyApiReqId,
CtxKeyBlockHash,
CtxKeyBlockNumber,
CtxKeyConn,
CtxKeyDuration,
CtxKeyUniqId,
CtxKeyBlockNumber,
CtxKeyBlockHash,
CtxKeyUserId,
}
const FatalLevel = logrus.FatalLevel
@ -41,8 +44,9 @@ const DebugLevel = logrus.DebugLevel
const TraceLevel = logrus.TraceLevel
type Entry = logrus.Entry
type Level = logrus.Level
func WithFieldsFromContext(ctx context.Context) *logrus.Entry {
func WithFieldsFromContext(ctx context.Context) *Entry {
entry := logrus.WithContext(ctx)
for _, key := range registeredKeys {
@ -138,6 +142,18 @@ func Tracef(format string, args ...interface{}) {
logrus.Tracef(format, args...)
}
func SetOutput(out io.Writer) {
logrus.SetOutput(out)
}
func SetLevel(lvl Level) {
logrus.SetLevel(lvl)
}
func IsLevelEnabled(lvl Level) bool {
return logrus.IsLevelEnabled(lvl)
}
func WithError(err error) *Entry {
return logrus.WithError(err)
}
@ -154,14 +170,14 @@ func Init() error {
file, err := os.OpenFile(logFile,
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0640)
if err == nil {
logrus.Infof("Directing output to %s", logFile)
logrus.SetOutput(file)
Infof("Directing output to %s", logFile)
SetOutput(file)
} else {
logrus.SetOutput(os.Stdout)
logrus.Info("Failed to logrus.to file, using default stdout")
SetOutput(os.Stdout)
Info("Failed to logrus.to file, using default stdout")
}
} else {
logrus.SetOutput(os.Stdout)
SetOutput(os.Stdout)
}
// Set the level.
@ -170,9 +186,11 @@ func Init() error {
if err != nil {
return err
}
logrus.SetLevel(lvl)
SetLevel(lvl)
formatter := &logrus.TextFormatter{}
formatter := &logrus.TextFormatter{
FullTimestamp: true,
}
// Show file/line number only at Trace level.
if lvl >= TraceLevel {
logrus.SetReportCaller(true)
@ -199,6 +217,6 @@ func Init() error {
}
logrus.SetFormatter(formatter)
logrus.Info("Log level set to ", lvl.String())
Info("Log level set to ", lvl.String())
return nil
}

View File

@ -18,9 +18,9 @@ package net_test
import (
"io/ioutil"
"log"
"testing"
"github.com/cerc-io/ipld-eth-server/v4/pkg/log"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

View File

@ -33,6 +33,7 @@ import (
const (
jsonMethod = "method"
jsonParams = "params"
jsonReqId = "id"
headerUserId = "X-User-Id"
headerOriginalRemoteAddr = "X-Original-Remote-Addr"
@ -66,8 +67,15 @@ func prepareRequest(r *http.Request) (*http.Request, error) {
}
// Pull out the method name, request ID, user ID, and address info.
reqMethod := fmt.Sprintf("%v", result[jsonMethod])
reqId := fmt.Sprintf("%g", result[jsonReqId])
reqMethod := fmt.Sprintf("%v", result[jsonMethod])
reqParams, _ := json.Marshal(result[jsonParams])
// Truncate parameters unless trace logging is enabled.
if !log.IsLevelEnabled(log.TraceLevel) {
if len(reqParams) > 100 {
reqParams = reqParams[:100]
}
}
userId := r.Header.Get(headerUserId)
conn := r.Header.Get(headerOriginalRemoteAddr)
if len(conn) == 0 {
@ -78,7 +86,8 @@ func prepareRequest(r *http.Request) (*http.Request, error) {
ctx := r.Context()
ctx = context.WithValue(ctx, log.CtxKeyUniqId, uniqId.String())
ctx = context.WithValue(ctx, log.CtxKeyApiMethod, reqMethod)
ctx = context.WithValue(ctx, log.CtxKeyReqId, reqId)
ctx = context.WithValue(ctx, log.CtxKeyApiMethod, reqParams)
ctx = context.WithValue(ctx, log.CtxKeyApiReqId, reqId)
ctx = context.WithValue(ctx, log.CtxKeyUserId, userId)
ctx = context.WithValue(ctx, log.CtxKeyConn, conn)

View File

@ -6,6 +6,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
)
func TestIntegration(t *testing.T) {
@ -14,4 +15,5 @@ func TestIntegration(t *testing.T) {
}
var _ = BeforeSuite(func() {
logrus.SetOutput(ioutil.Discard)
})