Skip to content

Commit

Permalink
feat: added debug flag in the SuperTokenConfig in the init() for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMayankThakur committed Nov 1, 2023
1 parent 470866b commit 1003a57
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
136 changes: 136 additions & 0 deletions recipe/session/logger_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
4 changes: 2 additions & 2 deletions supertokens/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
}
2 changes: 1 addition & 1 deletion supertokens/supertokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1003a57

Please sign in to comment.