Skip to content
This repository has been archived by the owner on Jan 31, 2025. It is now read-only.

Commit

Permalink
Better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Dec 20, 2024
1 parent f3dd050 commit f8d14c3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 26 deletions.
15 changes: 15 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package app

import (
"fmt"
"log/slog"
"os"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
"github.com/TrueBlocks/trueblocks-node/v4/config"
)

Expand Down Expand Up @@ -97,3 +99,16 @@ func (a *App) IsOn(feature Feature) bool {
}
return false
}

// State returns "on" or "off" depending if the feature is on or off.
func (a *App) State(feature Feature) string {
if a.IsOn(feature) {
return "on"
}
return "off"
}

func (a *App) Fatal(err error) {
fmt.Printf("Error: %s%s%s\n", colors.Red, err.Error(), colors.Off)
os.Exit(1)
}
33 changes: 21 additions & 12 deletions app/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (a *App) EstablishConfig() error {
var ok bool
var err error
if a.Config.ConfigPath, ok = os.LookupEnv("TB_NODE_DATADIR"); !ok {
return errors.New("TB_NODE_DATADIR is required in the environment")
return errors.New("environment variable `TB_NODE_DATADIR` is required but not found")
} else {
if a.Config.ConfigPath, err = cleanDataPath(a.Config.ConfigPath); err != nil {
return err
}
}
a.Logger.Info("Using data directory", "dataDir", a.Config.ConfigPath)
a.Logger.Info("data directory", "dataDir", a.Config.ConfigPath)

var targets string
chainStr, ok := os.LookupEnv("TB_NODE_CHAINS")
Expand All @@ -49,25 +49,20 @@ func (a *App) EstablishConfig() error {
return err
}
}
a.Logger.Info("cleaned chain string", "chainStr", chainStr, "targets", targets)
a.Logger.Info("configured chains", "chainStr", chainStr, "targets", targets)
a.Config.Targets = strings.Split(targets, ",")

chains := strings.Split(chainStr, ",")
for _, chain := range chains {
key := "TB_NODE_" + strings.ToUpper(chain) + "RPC"
if providerUrl, ok := os.LookupEnv(key); !ok {
msg := fmt.Sprintf("%s is required in the environment (implied by TB_NODE_CHAINS=%s)", key, chainStr)
msg := fmt.Sprintf("environment variable `%s` is required but not found (implied by TB_NODE_CHAINS=%s)", key, chainStr)
return errors.New(msg)
} else {
providerUrl = strings.Trim(providerUrl, "/")
if !isValidURL(providerUrl) {
return fmt.Errorf("invalid URL for %s: %s", key, providerUrl)
}
if err := a.tryConnect(chain, providerUrl, 5); err != nil {
return err
} else {
a.Logger.Info("connected to RPC", "chain", chain, "providerUrl", providerUrl)
}
a.Config.ProviderMap[chain] = providerUrl
}
}
Expand All @@ -83,14 +78,28 @@ func (a *App) EstablishConfig() error {
}

for _, env := range os.Environ() {
if strings.HasPrefix(env, "TB_") || strings.HasPrefix(env, "XDG_") {
a.Logger.Info("environment", "value", env)
if (strings.HasPrefix(env, "TB_") || strings.HasPrefix(env, "XDG_")) && strings.Contains(env, "=") {
parts := strings.Split(env, "=")
if len(parts) > 1 {
a.Logger.Info("environment", parts[0], parts[1])
} else {
a.Logger.Info("environment", parts[0], "<empty>")
}
}
}

for _, chain := range chains {
providerUrl := a.Config.ProviderMap[chain]
if err := a.tryConnect(chain, providerUrl, 5); err != nil {
return err
} else {
a.Logger.Info("test connection", "result", "okay", "chain", chain, "providerUrl", providerUrl)
}
}

configFn := filepath.Join(a.Config.ConfigPath, "trueBlocks.toml")
if file.FileExists(configFn) {
a.Logger.Info("Using existing config", "configFile", configFn, "nChains", len(a.Config.ProviderMap))
a.Logger.Info("config loaded", "configFile", configFn, "nChains", len(a.Config.ProviderMap))
// check to make sure the config file has all the chains
contents := file.AsciiFileToString(configFn)
for chain := range a.Config.ProviderMap {
Expand Down
2 changes: 1 addition & 1 deletion app/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ You MUST export the following values to the environment:
You MAY also export these environment variables:
TB_NODE_CHAINS: A comma-separated list of chains to index ("mainnet" is added if not present)
TB_NODE_CHAINS: A comma-separated list of chains to index (default: "mainnet")
TB_NODE_<CHAIN>RPC: For each CHAIN in the TB_NODE_CHAINS list, a valid RPC endpoint
(example: TB_NODE_SEPOLIARPC=http://localhost:8548)
Expand Down
6 changes: 5 additions & 1 deletion app/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func (h *CustomHandler) Handle(ctx context.Context, r slog.Record) error {
}
lev := r.Level.String()[:4]
timeFormat := r.Time.Format("02-01|15:04:05.000")
logMsg := fmt.Sprintf("%4.4s[%s] %s", levels[lev], timeFormat, r.Message)

// Format the message to be exactly n characters wide
formattedMessage := fmt.Sprintf("%-18.18s", r.Message)

logMsg := fmt.Sprintf("%4.4s[%s] %s ", levels[lev], timeFormat, formattedMessage)
r.Attrs(func(attr slog.Attr) bool {
logMsg += fmt.Sprintf(" %s=%v", colors.Green+attr.Key+colors.Off, attr.Value)
return true
Expand Down
6 changes: 3 additions & 3 deletions app/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (a *App) RunScraper(wg *sync.WaitGroup) {
for _, chain := range a.Config.Targets {
if rep, err := a.initOneChain(chain); err != nil {
if !strings.HasPrefix(err.Error(), "no record found in the Unchained Index") {
a.Logger.Error("Error", "err", err)
a.Logger.Warn("Warning", "msg", err)
} else {
a.Logger.Warn("No record found in the Unchained Index for chain", "chain", chain)
}
Expand All @@ -50,7 +50,7 @@ func (a *App) RunScraper(wg *sync.WaitGroup) {
msg := []any{"sleep", a.Sleep}
for _, chain := range a.Config.Targets {
if report, err := a.scrapeOneChain(chain); err != nil {
a.Logger.Error("ScrapeRunOnce failed", "chain", chain, "error", err)
a.Logger.Warn("Warning", "msg", "ScrapeRunOnce skipped", "chain", chain, "error", err)
time.Sleep(1 * time.Second)

} else {
Expand Down Expand Up @@ -118,7 +118,7 @@ func (a *App) ReportOneScrape(report *scraperReport) {
}

func (a *App) initOneChain(chain string) (*scraperReport, error) {
a.Logger.Info("For chain", "chain", chain)
a.Logger.Info("initializing unchained index", "chain", chain)

originalHandler := a.Logger.Handler()
defer func() {
Expand Down
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,37 @@ func main() {

// Establish the configuration file
if err := a.EstablishConfig(); err != nil {
a.Logger.Error(err.Error())
a.Fatal(err)

} else {
a.Logger.Info("Starting trueBlocks-node with...", "api", a.IsOn(app.Api), "init", a.InitMode, "monitor", a.IsOn(app.Monitor))
a.Logger.Info("trueblocks-node", "scrape", a.State(app.Scrape), "api", a.State(app.Api), "monitor", a.State(app.Monitor), "init-mode", a.InitMode)

// Start the API server. It runs in its own goroutine.
var apiUrl string
if a.IsOn(app.Api) {
a.Logger.Info("Starting Api server...")
a.RunServer()
a.Logger.Info("Api server started...")
a.Logger.Info("start api...")
apiUrl, _ = a.RunServer()
}

// Start forever loop to scrape and (optionally) monitor the chain
wg := sync.WaitGroup{}

if a.IsOn(app.Scrape) {
wg.Add(1)
a.Logger.Info("Starting scraper...")
a.Logger.Info("start scraper...")
go a.RunScraper(&wg)
a.Logger.Info("Scraper started...")
a.Logger.Info("scraper started...")
}

if a.IsOn(app.Monitor) {
wg.Add(1)
a.Logger.Info("Starting monitors...")
a.Logger.Info("start monitors...")
go a.RunMonitor(&wg)
a.Logger.Info("Monitors started...")
a.Logger.Info("monitors started...")
}

if len(apiUrl) > 0 {
a.Logger.Info("api is runing", "apiUrl", apiUrl)
}

wg.Wait()
Expand Down

0 comments on commit f8d14c3

Please sign in to comment.