Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node-api): logging, base handlers & refactor #1776

Merged
merged 62 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
5d17728
yep
archbear Jul 16, 2024
fcc25a0
pruner and cli
archbear Jul 17, 2024
4468ff7
Merge branch 'main' into block-store
archbear Jul 17, 2024
e484c0d
Merge remote-tracking branch 'origin/main' into block-store
archbear Jul 17, 2024
c41039a
msg to warn noop block service
archbear Jul 17, 2024
814c7a3
wire to storage backend
archbear Jul 17, 2024
7477ce0
compiles and wired but chain broken for some reason
archbear Jul 18, 2024
5c007c2
Merge branch 'main' into block-store
archbear Jul 18, 2024
0655d87
Merge branch 'main' into block-store
nidhi-singh02 Jul 18, 2024
d30b61c
service name and fix prune function
archbear Jul 18, 2024
2fe7be2
Merge branch 'block-store' of github.com:berachain/beacon-kit into bl…
archbear Jul 18, 2024
3688fc1
Merge branch 'block-store' into api
archbear Jul 18, 2024
ebea98a
routed
archbear Jul 18, 2024
ba7fb59
Merge remote-tracking branch 'origin/main' into api
archbear Jul 18, 2024
305eb7c
lint
archbear Jul 18, 2024
72856db
generate
archbear Jul 18, 2024
50ee156
gosec
archbear Jul 18, 2024
5d918aa
tidy
archbear Jul 18, 2024
fc30772
tidy-sync
archbear Jul 18, 2024
8f29e5a
Merge branch 'main' of github.com:berachain/beacon-kit into api
calbera Jul 18, 2024
8ee3e73
undo
calbera Jul 18, 2024
f49ebf5
Merge branch 'main' of github.com:berachain/beacon-kit into api
calbera Jul 18, 2024
ed3adee
Merge branch 'main' into api
calbera Jul 18, 2024
7bdd0ab
working changes
archbear Jul 22, 2024
63f0e1c
Merge branch 'main' into api
archbear Jul 22, 2024
9106132
small refactor
archbear Jul 23, 2024
0e0255d
Merge remote-tracking branch 'origin/main' into api
archbear Jul 23, 2024
9e71fa7
generate
archbear Jul 23, 2024
5982620
oops i did it again
archbear Jul 23, 2024
1b33acc
oh baby baby
archbear Jul 23, 2024
147eadc
clarifying comments
calbera Jul 23, 2024
f4e605e
nilawayyyy
calbera Jul 23, 2024
6badeb3
hide banner
archbear Jul 23, 2024
e09e3c0
Merge branch 'api' of github.com:berachain/beacon-kit into api
archbear Jul 23, 2024
e01cc4d
engines go mod
archbear Jul 24, 2024
43fb520
generate
archbear Jul 24, 2024
b096b1d
merge api
ocnc2 Jul 24, 2024
cf900a4
api logger + refactor deps
ocnc2 Jul 24, 2024
3463dc6
base handler
ocnc2 Jul 24, 2024
3f6498d
byebye echo log adapter
ocnc2 Jul 24, 2024
3da74db
im lazy so heres a decorator
ocnc2 Jul 24, 2024
f2c9aeb
ermst
ocnc2 Jul 24, 2024
2821f18
Merge remote-tracking branch 'origin/main' into api
archbear Jul 24, 2024
59029c1
tidy
archbear Jul 24, 2024
7883910
merge did some funky stuff
archbear Jul 24, 2024
c1bb976
Merge remote-tracking branch 'origin/api' into api-log
ocnc2 Jul 24, 2024
67a70c9
gitgit
ocnc2 Jul 24, 2024
b840ee3
git moment
ocnc2 Jul 24, 2024
e996c0e
lint
ocnc2 Jul 24, 2024
79f265a
chagnes
ocnc2 Jul 25, 2024
ad5060f
config
ocnc2 Jul 25, 2024
476f5b2
lint
ocnc2 Jul 25, 2024
ed2ab26
Merge branch 'main' into api-log
ocnc2 Jul 25, 2024
4c87848
colors colors
ocnc2 Jul 25, 2024
8d22a89
colors
ocnc2 Jul 25, 2024
336de53
format + lint
ocnc2 Jul 25, 2024
07d87ca
merge main
ocnc2 Jul 25, 2024
9ca95a9
merge fixes
ocnc2 Jul 25, 2024
b9c0bf4
comments + lint
ocnc2 Jul 25, 2024
4638418
Merge branch 'main' into api-log
ocnc2 Jul 25, 2024
2482aa8
Merge branch 'main' into api-log
ocnc2 Jul 25, 2024
495f893
changes for arch 🎻
ocnc2 Jul 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions mod/cli/pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
sdklog "cosmossdk.io/log"
cmdlib "github.com/berachain/beacon-kit/mod/cli/pkg/commands"
"github.com/berachain/beacon-kit/mod/cli/pkg/utils/context"
"github.com/berachain/beacon-kit/mod/log"
"github.com/berachain/beacon-kit/mod/node-core/pkg/components"
"github.com/berachain/beacon-kit/mod/node-core/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (cb *CLIBuilder[T, ExecutionPayloadT]) Build() (*cmdlib.Root, error) {
mm *module.Manager
clientCtx client.Context
chainSpec common.ChainSpec
logger log.Logger
logger log.AdvancedLogger[any, sdklog.Logger]
)
// build dependencies for the root command
if err := depinject.Inject(
Expand Down Expand Up @@ -139,7 +140,7 @@ func (cb *CLIBuilder[T, ExecutionPayloadT]) Build() (*cmdlib.Root, error) {

// defaultRunHandler returns the default run handler for the CLIBuilder.
func (cb *CLIBuilder[T, ExecutionPayloadT]) defaultRunHandler(
logger log.Logger,
logger log.AdvancedLogger[any, sdklog.Logger],
) func(cmd *cobra.Command) error {
return func(cmd *cobra.Command) error {
return cb.InterceptConfigsPreRunHandler(
Expand All @@ -156,8 +157,11 @@ func (cb *CLIBuilder[T, ExecutionPayloadT]) defaultRunHandler(
// InterceptConfigsAndCreateContext except it also sets the server context on
// the command and the server logger.
func (cb *CLIBuilder[T, ExecutionPayloadT]) InterceptConfigsPreRunHandler(
cmd *cobra.Command, logger log.Logger, customAppConfigTemplate string,
customAppConfig interface{}, cmtConfig *cmtcfg.Config,
cmd *cobra.Command,
logger log.AdvancedLogger[any, sdklog.Logger],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see if we can do LoggerT for the advanced logger otherwise make the sdk logger a generic type

customAppConfigTemplate string,
customAppConfig interface{},
cmtConfig *cmtcfg.Config,
) error {
serverCtx, err := context.InterceptConfigsAndCreateContext(
cmd, customAppConfigTemplate, customAppConfig, cmtConfig, logger)
Expand Down
8 changes: 5 additions & 3 deletions mod/cli/pkg/components/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"io"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
sdklog "cosmossdk.io/log"
"github.com/berachain/beacon-kit/mod/config"
"github.com/berachain/beacon-kit/mod/log/pkg/phuslu"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
Expand All @@ -41,7 +41,9 @@ type LoggerInput struct {
// It reads the log level and format from the server context.
func ProvideLogger(
in LoggerInput,
) log.Logger {
logger := phuslu.NewLogger[log.Logger](in.Out, in.Cfg.GetLogger())
) *Logger {
logger := phuslu.NewLogger[sdklog.Logger](in.Out, in.Cfg.GetLogger())
logger.AddKeyColor("error", "red")
logger.AddKeyColor("err", "red")
return logger
}
30 changes: 30 additions & 0 deletions mod/cli/pkg/components/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package components

import (
sdklog "cosmossdk.io/log"
"github.com/berachain/beacon-kit/mod/log/pkg/phuslu"
)

type (
Logger = phuslu.Logger[sdklog.Logger]
)
6 changes: 6 additions & 0 deletions mod/cli/pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (
nodeAPIRoot = beaconKitRoot + "node-api."
NodeAPIEnabled = nodeAPIRoot + "enabled"
NodeAPIAddress = nodeAPIRoot + "address"
NodeAPILogging = nodeAPIRoot + "logging"
)

// AddBeaconKitFlags implements servertypes.ModuleInitFlags interface.
Expand Down Expand Up @@ -157,4 +158,9 @@ func AddBeaconKitFlags(startCmd *cobra.Command) {
defaultCfg.NodeAPI.Address,
"node api address",
)
startCmd.Flags().Bool(
NodeAPILogging,
defaultCfg.NodeAPI.Logging,
"node api logging",
)
}
3 changes: 3 additions & 0 deletions mod/config/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,7 @@ enabled = "{{ .BeaconKit.NodeAPI.Enabled }}"

# Address is the address to bind the node API to.
address = "{{ .BeaconKit.NodeAPI.Address }}"

# Logging determines if the node API logging is enabled.
logging = "{{ .BeaconKit.NodeAPI.Logging }}"
`
62 changes: 57 additions & 5 deletions mod/log/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

package log

// Logger is extremely similar to the Cosmos-SDK Logger interface.
// Logger represents a basic logger that is extremely similar to the Cosmos-SDK
// Logger interface.
type Logger[KeyValT any] interface {
// Info takes a message and a set of key/value pairs and logs with level
// INFO.
Expand All @@ -40,11 +41,29 @@ type Logger[KeyValT any] interface {
Debug(msg string, keyVals ...KeyValT)
}

// AdvancedLogger is extremely similar to the Cosmos-SDK Logger interface,
// however we introduce a generic to allow for more flexibility in
// the underlying logger implementation.
type AdvancedLogger[KeyValT, LoggerT any] interface {
// ConfigurableLogger extends the basic logger with the ability to configure
// the logger with a config.
type ConfigurableLogger[
ConfigurableLoggerT, KeyValT any, ConfigT any,
] interface {
Logger[KeyValT]
WithConfig(config ConfigT) ConfigurableLoggerT
}

// ColorLogger extends the basic logger with the ability to configure the
// logger with key and key value colors.
type ColorLogger[KeyValT any] interface {
Logger[KeyValT]
// AddKeyColor sets the log color for a key.
AddKeyColor(key any, color Color)
// AddKeyValColor sets the log color for a key and its value.
AddKeyValColor(key any, val any, color Color)
}

// AdvancedLogger extends the color logger with the ability to wrap the logger
// with additional context and to access the underlying logger implementation.
type AdvancedLogger[KeyValT, LoggerT any] interface {
ColorLogger[KeyValT]
// With returns a new wrapped logger with additional context provided by a
// set.
With(keyVals ...KeyValT) LoggerT
Expand All @@ -53,3 +72,36 @@ type AdvancedLogger[KeyValT, LoggerT any] interface {
// Advanced users can type cast the returned value to the actual logger.
Impl() any
}

// Color is a string that holds the hex color code for the color.
type Color string

// Raw returns the raw color code.
func (c Color) Raw() string {
return string(c)
}

const (
// colours.
Reset Color = "\x1b[0m"
Black Color = "\x1b[30m"
Red Color = "\x1b[31m"
Green Color = "\x1b[32m"
Yellow Color = "\x1b[33m"
Blue Color = "\x1b[34m"
Magenta Color = "\x1b[35m"
Cyan Color = "\x1b[36m"
White Color = "\x1b[37m"

Gray Color = "\x1b[90m"
BrightRed Color = "\x1b[91m"
BrightGreen Color = "\x1b[92m"
BrightYellow Color = "\x1b[93m"
BrightBlue Color = "\x1b[94m"
BrightMagenta Color = "\x1b[95m"
BrightCyan Color = "\x1b[96m"
BrightWhite Color = "\x1b[97m"

BrightBackgroundWhite Color = "\x1b[107m"
BrightBackgroundBlue Color = "\x1b[104m"
)
10 changes: 10 additions & 0 deletions mod/log/pkg/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package noop

import "github.com/berachain/beacon-kit/mod/log"

// Logger is a logger that performs no operations. It can be used in
// environments where logging should be disabled. It implements the Logger
// interface with no-op methods.
Expand Down Expand Up @@ -63,3 +65,11 @@ func (n *Logger[KeyValT, ImplT]) With(...KeyValT) ImplT {
func (n *Logger[KeyValT, ImplT]) Impl() any {
return nil
}

func (n *Logger[KeyValT, ImplT]) AddKeyColor(any, log.Color) {
// No operation
}

func (n *Logger[KeyValT, ImplT]) AddKeyValColor(any, any, log.Color) {
// No operation
}
49 changes: 37 additions & 12 deletions mod/log/pkg/phuslu/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@ import (
)

// Formatter is a custom formatter for log messages.
type Formatter struct{}
type Formatter struct {
// KeyColors applies specific colors to log entries based on their keys.
keyColors map[string]Color
// KeyValColors applies specific colors to log entries based on their keys
// and values.
keyValColors map[string]Color
}

// NewFormatter creates a new Formatter with default settings.
func NewFormatter() *Formatter {
return &Formatter{}
return &Formatter{
keyColors: make(map[string]Color),
keyValColors: make(map[string]Color),
}
}

// Format formats the log message.
Expand All @@ -50,21 +59,21 @@ func (f *Formatter) Format(
var color, label string
switch args.Level {
case "trace":
color, label = traceColor, traceLabel
color, label = traceColor.Raw(), traceLabel
case "debug":
color, label = debugColor, debugLabel
color, label = debugColor.Raw(), debugLabel
case "info":
color, label = infoColor, infoLabel
color, label = infoColor.Raw(), infoLabel
case "warn":
color, label = warnColor, warnLabel
color, label = warnColor.Raw(), warnLabel
case "error":
color, label = errorColor, errorLabel
color, label = errorColor.Raw(), errorLabel
case "fatal":
color, label = fatalColor, fatalLabel
color, label = fatalColor.Raw(), fatalLabel
case "panic":
color, label = panicColor, panicLabel
color, label = panicColor.Raw(), panicLabel
default:
color, label = defaultColor, defaultLabel
color, label = defaultColor.Raw(), defaultLabel
}

f.printWithColor(args, buffer, color, label)
Expand All @@ -80,6 +89,16 @@ func (f *Formatter) Format(
return out.Write(buffer.Bytes)
}

// AddKeyColor adds a key and color to the keyColors map.
func (f *Formatter) AddKeyColor(key string, color Color) {
f.keyColors[key] = color
}

// AddKeyValColor adds a key and color to the keyValColors map.
func (f *Formatter) AddKeyValColor(key string, val string, color Color) {
f.keyValColors[key+val] = color
}

// printWithColor prints the log message with color.
func (f *Formatter) printWithColor(
args *log.FormatterArgs,
Expand All @@ -92,8 +111,14 @@ func (f *Formatter) printWithColor(
b.Bytes = append(b.Bytes, args.Message...)
for _, kv := range args.KeyValues {
b.Bytes = append(b.Bytes, ' ')
if kv.Key == "error" || kv.Key == "err" {
b.Bytes = append(b.Bytes, red...)
// apply the key color if configured
if kColor, ok := f.keyColors[kv.Key]; ok {
b.Bytes = append(b.Bytes, kColor.Raw()...)
}
// apply the key+value color if configured (kv color takes precedence
// over key color)
if kvColor, ok := f.keyValColors[kv.Key+kv.Value]; ok {
b.Bytes = append(b.Bytes, kvColor.Raw()...)
}
b.Bytes = append(b.Bytes, kv.Key...)
b.Bytes = append(b.Bytes, '=')
Expand Down
18 changes: 17 additions & 1 deletion mod/log/pkg/phuslu/phuslu.go → mod/log/pkg/phuslu/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ func (l Logger[ImplT]) With(keyVals ...any) ImplT {
return any(&newLogger).(ImplT)
}

// Writer returns the io.Writer of the logger.
func (l *Logger[ImplT]) Writer() io.Writer {
return l.out
}

// msgWithContext logs a message with keyVals and current context.
func (l *Logger[Impl]) msgWithContext(
msg string, e *log.Entry, keyVals ...any,
Expand All @@ -120,7 +125,7 @@ func (l *Logger[Impl]) msgWithContext(
}

/* -------------------------------------------------------------------------- */
/* configuration */
/* config */
/* -------------------------------------------------------------------------- */

// Temporary workaround to allow dynamic configuration post-logger creation.
Expand All @@ -132,6 +137,17 @@ func (l *Logger[ImplT]) WithConfig(cfg Config) *Logger[ImplT] {
return l
}

// AddKeyColor applies a color to log entries based on their keys.
func (l *Logger[ImplT]) AddKeyColor(key any, color Color) {
l.formatter.AddKeyColor(key.(string), color)
}

// AddKeyValColor applies specific colors to log entries based on their keys and
// values.
func (l *Logger[ImplT]) AddKeyValColor(key any, val any, color Color) {
l.formatter.AddKeyValColor(key.(string), val.(string), color)
}

// sets the style of the logger.
func (l *Logger[Impl]) withStyle(style string) {
if style == StylePretty {
Expand Down
Loading
Loading