From 31f78370768ff2914e0533055b40d0c8d85793f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Fri, 25 Oct 2024 13:53:14 -0400 Subject: [PATCH 1/2] 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 --- recursion/gnark-ffi/go/sp1/logging.go | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 recursion/gnark-ffi/go/sp1/logging.go diff --git a/recursion/gnark-ffi/go/sp1/logging.go b/recursion/gnark-ffi/go/sp1/logging.go new file mode 100644 index 000000000..7238ceb6d --- /dev/null +++ b/recursion/gnark-ffi/go/sp1/logging.go @@ -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}) +} From 27994aaa641f9018b344cd0b7e975945ab72b7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= <4142+huitseeker@users.noreply.github.com> Date: Fri, 25 Oct 2024 16:40:43 -0400 Subject: [PATCH 2/2] Update recursion/gnark-ffi/go/sp1/logging.go Adds an 'off' option Co-authored-by: wwared --- recursion/gnark-ffi/go/sp1/logging.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recursion/gnark-ffi/go/sp1/logging.go b/recursion/gnark-ffi/go/sp1/logging.go index 7238ceb6d..5101868c9 100644 --- a/recursion/gnark-ffi/go/sp1/logging.go +++ b/recursion/gnark-ffi/go/sp1/logging.go @@ -14,7 +14,7 @@ func init() { // Configure the global log level based on the environment variable switch logLevel { - case "disabled", "false", "none": + case "disabled", "false", "none", "off": zerolog.SetGlobalLevel(zerolog.Disabled) case "panic": zerolog.SetGlobalLevel(zerolog.PanicLevel)