Skip to content

Commit 142bbfd

Browse files
huitseekerwwared
andauthored
feat: Implement custom logging initialization for SP1 package (#193)
* feat: Implement custom logging initialization for SP1 package 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 * Update recursion/gnark-ffi/go/sp1/logging.go Adds an 'off' option Co-authored-by: wwared <[email protected]> --------- Co-authored-by: wwared <[email protected]>
1 parent 1650846 commit 142bbfd

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sp1
2+
3+
import (
4+
"os"
5+
"strings"
6+
7+
"github.com/rs/zerolog"
8+
"github.com/rs/zerolog/log"
9+
)
10+
11+
func init() {
12+
// Get log level from environment variable
13+
logLevel := strings.ToLower(os.Getenv("SP1_GO_LOG"))
14+
15+
// Configure the global log level based on the environment variable
16+
switch logLevel {
17+
case "disabled", "false", "none", "off":
18+
zerolog.SetGlobalLevel(zerolog.Disabled)
19+
case "panic":
20+
zerolog.SetGlobalLevel(zerolog.PanicLevel)
21+
case "fatal":
22+
zerolog.SetGlobalLevel(zerolog.FatalLevel)
23+
case "error":
24+
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
25+
case "warn", "warning":
26+
zerolog.SetGlobalLevel(zerolog.WarnLevel)
27+
case "debug":
28+
zerolog.SetGlobalLevel(zerolog.DebugLevel)
29+
case "trace":
30+
zerolog.SetGlobalLevel(zerolog.TraceLevel)
31+
default: // Including "info" and empty string
32+
zerolog.SetGlobalLevel(zerolog.InfoLevel)
33+
}
34+
35+
// Configure zerolog to use a console writer
36+
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
37+
}

0 commit comments

Comments
 (0)