From 1003a57ee9cc7ad5c2a6829386fe200931262636 Mon Sep 17 00:00:00 2001 From: Mayank Thakur Date: Wed, 1 Nov 2023 11:43:49 +0530 Subject: [PATCH] feat: added debug flag in the SuperTokenConfig in the init() for logging --- recipe/session/logger_test.go | 136 ++++++++++++++++++++++++++++++++++ supertokens/logger.go | 4 +- supertokens/supertokens.go | 2 +- 3 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 recipe/session/logger_test.go diff --git a/recipe/session/logger_test.go b/recipe/session/logger_test.go new file mode 100644 index 00000000..458f1126 --- /dev/null +++ b/recipe/session/logger_test.go @@ -0,0 +1,136 @@ +package session + +import ( + "bytes" + "log" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/supertokens/supertokens-golang/supertokens" + "github.com/supertokens/supertokens-golang/test/unittesting" +) + +func TestLogDebugMessageWhenDebugTrue(t *testing.T) { + var logMessage = "test log message" + var buf bytes.Buffer + + debug := true + supertokens.Logger = log.New(&buf, "", 0) + + configValue := supertokens.TypeInput{ + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: "http://localhost:8080", + }, + AppInfo: supertokens.AppInfo{ + AppName: "SuperTokens", + APIDomain: "api.supertokens.io", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + Init(nil), + }, + Debug: &debug, + } + BeforeEach() + + unittesting.StartUpST("localhost", "8080") + + defer AfterEach() + + err := supertokens.Init(configValue) + + if err != nil { + t.Error(err.Error()) + } + + supertokensInstance, err := supertokens.GetInstanceOrThrowError() + + if err != nil { + t.Error(err.Error()) + } + + supertokens.LogDebugMessage(logMessage) + assert.Equal(t, &debug, supertokensInstance.Debug) + assert.Contains(t, buf.String(), logMessage, "checking log message in logs") +} + +func TestLogDebugMessageWhenDebugFalse(t *testing.T) { + var logMessage = "test log message" + var buf bytes.Buffer + + debug := false + supertokens.Logger = log.New(&buf, "", 0) + + configValue := supertokens.TypeInput{ + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: "http://localhost:8080", + }, + AppInfo: supertokens.AppInfo{ + AppName: "SuperTokens", + APIDomain: "api.supertokens.io", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + Init(nil), + }, + Debug: &debug, + } + BeforeEach() + + unittesting.StartUpST("localhost", "8080") + + defer AfterEach() + + err := supertokens.Init(configValue) + + if err != nil { + t.Error(err.Error()) + } + + supertokensInstance, err := supertokens.GetInstanceOrThrowError() + + if err != nil { + t.Error(err.Error()) + } + + supertokens.LogDebugMessage(logMessage) + assert.Equal(t, &debug, supertokensInstance.Debug) + assert.NotContains(t, buf.String(), logMessage, "checking log message in logs") +} + +func TestLogDebugMessageWithEnvVar(t *testing.T) { + var logMessage = "test log message" + var buf bytes.Buffer + + supertokens.Logger = log.New(&buf, "", 0) + os.Setenv("SUPERTOKENS_DEBUG", "1") + + configValue := supertokens.TypeInput{ + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: "http://localhost:8080", + }, + AppInfo: supertokens.AppInfo{ + AppName: "SuperTokens", + APIDomain: "api.supertokens.io", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + Init(nil), + }, + } + BeforeEach() + + unittesting.StartUpST("localhost", "8080") + + defer AfterEach() + + err := supertokens.Init(configValue) + + if err != nil { + t.Error(err.Error()) + } + + supertokens.LogDebugMessage(logMessage) + assert.Contains(t, buf.String(), logMessage, "checking log message in logs") +} diff --git a/supertokens/logger.go b/supertokens/logger.go index 210d490a..7731f966 100644 --- a/supertokens/logger.go +++ b/supertokens/logger.go @@ -16,7 +16,7 @@ const supertokens_namespace = "com.supertokens" */ var ( - logger = log.New(os.Stdout, supertokens_namespace, 0) + Logger = log.New(os.Stdout, supertokens_namespace, 0) ) func formatMessage(message string) string { @@ -27,6 +27,6 @@ func formatMessage(message string) string { func LogDebugMessage(message string) { _, exists := os.LookupEnv("SUPERTOKENS_DEBUG") if exists { - logger.Printf(formatMessage(message)) + Logger.Printf(formatMessage(message)) } } diff --git a/supertokens/supertokens.go b/supertokens/supertokens.go index 0789bf84..0196e0e5 100644 --- a/supertokens/supertokens.go +++ b/supertokens/supertokens.go @@ -57,7 +57,7 @@ func supertokensInit(config TypeInput) error { } superTokens.Debug = config.Debug - if superTokens.Debug != nil && *superTokens.Debug { + if superTokens.Debug != nil && *superTokens.Debug == true { err := os.Setenv("SUPERTOKENS_DEBUG", "1") if err != nil { return err