Skip to content

Commit

Permalink
feat: Implement custom logging initialization for SP1 package
Browse files Browse the repository at this point in the history
Configures logging for go through an environment variable.

Example usage:
```
SP1_GO_LOG=false cargo test

./cargo test
SP1_GO_LOG=true ./cargo test
```

This solution works because:

- The init() function in logging.go will run before any other code in the package
- Since gnark uses zerolog, setting the global log level will affect all logging throughout the application, including in dependencies
- The change is minimal and contained to just one file
- It doesn't require any changes to the Rust code or to gnark's source code
- It's configurable through an environment variable
  • Loading branch information
huitseeker committed Oct 25, 2024
1 parent c61f92e commit 31f7837
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions recursion/gnark-ffi/go/sp1/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sp1

import (
"os"
"strings"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func init() {
// Get log level from environment variable
logLevel := strings.ToLower(os.Getenv("SP1_GO_LOG"))

// Configure the global log level based on the environment variable
switch logLevel {
case "disabled", "false", "none":
zerolog.SetGlobalLevel(zerolog.Disabled)
case "panic":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
case "fatal":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "warn", "warning":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "trace":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
default: // Including "info" and empty string
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

// Configure zerolog to use a console writer
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}

0 comments on commit 31f7837

Please sign in to comment.