-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create base signer and signer structs to group common data …
…of EVM/Bitcoin signer and observer (#2344) * save local new files to remote * initiated base observer * move base to chains folder * moved logger to base package * added base signer and logger * added changelog entry * added metrics server to base signer/observer * added more descriptions for constants and base structs --------- Co-authored-by: Lucas Bertrand <[email protected]>
- Loading branch information
1 parent
d4a9f7d
commit 2c76429
Showing
22 changed files
with
1,133 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package base | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/zeta-chain/zetacore/zetaclient/config" | ||
) | ||
|
||
const ( | ||
ComplianceLogFile = "compliance.log" | ||
) | ||
|
||
// Logger contains the base loggers | ||
type Logger struct { | ||
Std zerolog.Logger | ||
Compliance zerolog.Logger | ||
} | ||
|
||
// DefaultLoggers creates default base loggers for tests | ||
func DefaultLogger() Logger { | ||
return Logger{ | ||
Std: log.Logger, | ||
Compliance: log.Logger, | ||
} | ||
} | ||
|
||
// ObserverLogger contains the loggers for chain observers | ||
type ObserverLogger struct { | ||
// the parent logger for the chain observer | ||
Chain zerolog.Logger | ||
|
||
// the logger for inbound transactions | ||
Inbound zerolog.Logger | ||
|
||
// the logger for outbound transactions | ||
Outbound zerolog.Logger | ||
|
||
// the logger for the chain's gas price | ||
GasPrice zerolog.Logger | ||
|
||
// the logger for block headers | ||
Headers zerolog.Logger | ||
|
||
// the logger for the compliance check | ||
Compliance zerolog.Logger | ||
} | ||
|
||
// InitLogger initializes the base loggers | ||
func InitLogger(cfg config.Config) (Logger, error) { | ||
// open compliance log file | ||
file, err := openComplianceLogFile(cfg) | ||
if err != nil { | ||
return DefaultLogger(), err | ||
} | ||
|
||
// create loggers based on configured level and format | ||
var std zerolog.Logger | ||
var compliance zerolog.Logger | ||
switch cfg.LogFormat { | ||
case "json": | ||
std = zerolog.New(os.Stdout).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() | ||
compliance = zerolog.New(file).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() | ||
case "text": | ||
std = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}). | ||
Level(zerolog.Level(cfg.LogLevel)). | ||
With(). | ||
Timestamp(). | ||
Logger() | ||
compliance = zerolog.New(file).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() | ||
default: | ||
std = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}) | ||
compliance = zerolog.New(file).With().Timestamp().Logger() | ||
} | ||
|
||
if cfg.LogSampler { | ||
std = std.Sample(&zerolog.BasicSampler{N: 5}) | ||
} | ||
log.Logger = std // set global logger | ||
|
||
return Logger{ | ||
Std: std, | ||
Compliance: compliance, | ||
}, nil | ||
} | ||
|
||
// openComplianceLogFile opens the compliance log file | ||
func openComplianceLogFile(cfg config.Config) (*os.File, error) { | ||
// use zetacore home as default | ||
logPath := cfg.ZetaCoreHome | ||
if cfg.ComplianceConfig.LogPath != "" { | ||
logPath = cfg.ComplianceConfig.LogPath | ||
} | ||
|
||
// clean file name | ||
name := filepath.Join(logPath, ComplianceLogFile) | ||
name, err := filepath.Abs(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
name = filepath.Clean(name) | ||
|
||
// open (or create) compliance log file | ||
return os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | ||
} |
Oops, something went wrong.