Skip to content

Commit 6df53d0

Browse files
change formatter
1 parent 16795c3 commit 6df53d0

File tree

6 files changed

+73
-16
lines changed

6 files changed

+73
-16
lines changed

beacon-chain/light-client/log.go

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### Changed
2+
3+
- Added a log.go file for every important package with a logger variable containing a `package` field set to the package
4+
path.
5+
- Added a CI check to ensure every important package has a log.go file with the correct `package` field.
6+
- Changed the log formatter to use this `package` field instead of the previous `prefix` field.

hack/gen-logs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ package $pkg_name
134134
import "github.com/sirupsen/logrus"
135135
136136
// The prefix for logs from this package will be the text after the last slash in the package path.
137-
// If you wish to change this, you should add your desired name in the X/X/X/log.go file.
137+
// If you wish to change this, you should add your desired name in the runtime/logging/logrus-prefixed-formatter/prefix-replacement.go file.
138138
var log = logrus.WithField("package", "$rel_path")
139139
EOF
140140

runtime/logging/logrus-prefixed-formatter/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ load("@prysm//tools/go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
5-
srcs = ["formatter.go"],
5+
srcs = [
6+
"formatter.go",
7+
"prefix-replacements.go",
8+
],
69
importpath = "github.com/OffchainLabs/prysm/v7/runtime/logging/logrus-prefixed-formatter",
710
visibility = ["//visibility:public"],
811
deps = [

runtime/logging/logrus-prefixed-formatter/formatter.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919

2020
const defaultTimestampFormat = time.RFC3339
2121

22+
var regex = regexp.MustCompile(`^\\[(.*?)\\]`)
23+
2224
var (
2325
baseTimestamp time.Time = time.Now()
2426
defaultColorScheme *ColorScheme = &ColorScheme{
@@ -263,17 +265,11 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
263265
}
264266

265267
level := levelColor(fmt.Sprintf("%5s", levelText))
266-
prefix := ""
267-
message := entry.Message
268268

269-
if prefixValue, ok := entry.Data["prefix"]; ok {
270-
prefix = colorScheme.PrefixColor(" " + prefixValue.(string) + ":")
271-
} else {
272-
prefixValue, trimmedMsg := extractPrefix(entry.Message)
273-
if len(prefixValue) > 0 {
274-
prefix = colorScheme.PrefixColor(" " + prefixValue + ":")
275-
message = trimmedMsg
276-
}
269+
prefix := ""
270+
prefixValue, message := getPrefixAndMessage(entry)
271+
if len(prefixValue) > 0 {
272+
prefix = colorScheme.PrefixColor(" " + prefixValue + ":")
277273
}
278274

279275
messageFormat := "%s"
@@ -293,7 +289,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
293289
_, err = fmt.Fprintf(b, "%s %s%s "+messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, message)
294290
}
295291
for _, k := range keys {
296-
if k != "prefix" {
292+
if k != "package" {
297293
v := entry.Data[k]
298294

299295
format := "%+v"
@@ -333,9 +329,39 @@ func (f *TextFormatter) needsQuoting(text string) bool {
333329
return false
334330
}
335331

332+
// getPrefixAndMessage extracts the prefix and the message from the entry by the following order:
333+
// 1. If the "prefix" field is set in entry.Data, use that as prefix.
334+
// 2. If the "package" field is set in entry.Data, use that to determine the prefix.
335+
// If a replacement is found in prefixReplacements, use that. Otherwise, use the package name.
336+
// 3. Try to extract the prefix from the message itself by looking for a pattern like "[prefix] message".
337+
// 4. If none of the above methods yield a prefix, return an empty prefix and the original message.
338+
func getPrefixAndMessage(entry *logrus.Entry) (string, string) {
339+
prefix := ""
340+
msg := entry.Message
341+
342+
if prefixOldMethod, ok := entry.Data["prefix"]; ok {
343+
return prefixOldMethod.(string), msg
344+
}
345+
346+
if packagePath, ok := entry.Data["package"]; ok {
347+
if prefixReplacement, ok := prefixReplacements[packagePath.(string)]; ok {
348+
return prefixReplacement, msg
349+
}
350+
pathSplit := strings.Split(packagePath.(string), "/")
351+
prefix = pathSplit[len(pathSplit)-1]
352+
return prefix, msg
353+
}
354+
355+
if prefixExtracted, trimmedMsg := extractPrefix(msg); len(prefixExtracted) > 0 {
356+
return prefixExtracted, trimmedMsg
357+
}
358+
359+
return "", msg
360+
}
361+
336362
func extractPrefix(msg string) (string, string) {
337363
prefix := ""
338-
regex := regexp.MustCompile(`^\\[(.*?)\\]`)
364+
339365
if regex.MatchString(msg) {
340366
match := regex.FindString(msg)
341367
prefix, msg = match[1:len(match)-1], strings.TrimSpace(msg[len(match):])
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package prefixed
2+
3+
var prefixReplacements = map[string]string{
4+
"beacon-chain/cache/depositsnapshot": "cache",
5+
"beacon-chain/core/transition": "state",
6+
"beacon-chain/db/kv": "db",
7+
"beacon-chain/db/slasherkv": "slasherdb",
8+
"beacon-chain/db/pruner": "db-pruner",
9+
"beacon-chain/rpc/core": "rpc/core",
10+
"beacon-chain/rpc/eth/beacon": "rpc/beacon",
11+
"beacon-chain/rpc/eth/validator": "beacon-api",
12+
"beacon-chain/rpc/prysm/v1alpha1/beacon": "rpc",
13+
"beacon-chain/rpc/prysm/v1alpha1/validator": "rpc/validator",
14+
"beacon-chain/sync/checkpoint": "checkpoint-sync",
15+
"config/features": "flags",
16+
"proto/prysm/v1alpha1": "protobuf",
17+
"validator/db/kv": "db",
18+
"validator/db/filesystem": "db",
19+
"validator/keymanager/derived": "derived-keymanager",
20+
"validator/keymanager/local": "local-keymanager",
21+
"validator/keymanager/remote-web3signer": "remote-keymanager",
22+
"validator/keymanager/remote-web3signer/internal": "remote-keymanager-internal",
23+
"beacon-chain/forkchoice/doubly-linked-tree": "forkchoice-doublylinkedtree",
24+
}

0 commit comments

Comments
 (0)