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 ( import (
"github.com/cerc-io/ipld-eth-server/v4/cmd" "github.com/cerc-io/ipld-eth-server/v4/cmd"
"github.com/sirupsen/logrus"
) )
func main() { func main() {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
cmd.Execute() cmd.Execute()
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -33,6 +33,7 @@ import (
const ( const (
jsonMethod = "method" jsonMethod = "method"
jsonParams = "params"
jsonReqId = "id" jsonReqId = "id"
headerUserId = "X-User-Id" headerUserId = "X-User-Id"
headerOriginalRemoteAddr = "X-Original-Remote-Addr" 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. // Pull out the method name, request ID, user ID, and address info.
reqMethod := fmt.Sprintf("%v", result[jsonMethod])
reqId := fmt.Sprintf("%g", result[jsonReqId]) 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) userId := r.Header.Get(headerUserId)
conn := r.Header.Get(headerOriginalRemoteAddr) conn := r.Header.Get(headerOriginalRemoteAddr)
if len(conn) == 0 { if len(conn) == 0 {
@ -78,7 +86,8 @@ func prepareRequest(r *http.Request) (*http.Request, error) {
ctx := r.Context() ctx := r.Context()
ctx = context.WithValue(ctx, log.CtxKeyUniqId, uniqId.String()) ctx = context.WithValue(ctx, log.CtxKeyUniqId, uniqId.String())
ctx = context.WithValue(ctx, log.CtxKeyApiMethod, reqMethod) 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.CtxKeyUserId, userId)
ctx = context.WithValue(ctx, log.CtxKeyConn, conn) ctx = context.WithValue(ctx, log.CtxKeyConn, conn)

View File

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