Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added debug flag in the SuperTokenConfig logging #385

Merged
merged 11 commits into from
Nov 1, 2023
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]


## [0.16.5] - 2023-11-1

- Adds `debug` flag to the `TypeInput`. If set to `true`, debug logs will be printed.

## [0.16.4] - 2023-10-20

- Fixes an issue where sometimes the `Access-Control-Expose-Headers` header value would contain duplicates
Expand Down
166 changes: 166 additions & 0 deletions recipe/session/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
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"
)

rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
// Added the logger tests here because supertokens/logger_test.go causes cyclic import errors due to imports in test/unittesting/testingUtils.go

func resetLogger() {
supertokens.Logger = log.New(os.Stdout, "com.supertokens", 0)
os.Unsetenv("SUPERTOKENS_DEBUG")
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
}

func TestLogDebugMessageWhenDebugTrue(t *testing.T) {
var logMessage = "test log message"
var buf bytes.Buffer

supertokens.Logger = log.New(&buf, "test", 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: true,
}
BeforeEach()

unittesting.StartUpST("localhost", "8080")

defer AfterEach()
defer resetLogger()

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")
}

func TestLogDebugMessageWhenDebugFalse(t *testing.T) {
var logMessage = "test log message"
var buf bytes.Buffer

supertokens.Logger = log.New(&buf, "test", 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: false,
}
BeforeEach()

unittesting.StartUpST("localhost", "8080")

defer AfterEach()
defer resetLogger()

err := supertokens.Init(configValue)

if err != nil {
t.Error(err.Error())
}

supertokens.LogDebugMessage(logMessage)
assert.NotContains(t, buf.String(), logMessage, "checking log message in logs")
}

func TestLogDebugMessageWhenDebugNotSet(t *testing.T) {
var logMessage = "test log message"
var buf bytes.Buffer

supertokens.Logger = log.New(&buf, "test", 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),
},
}
BeforeEach()

unittesting.StartUpST("localhost", "8080")

defer AfterEach()
defer resetLogger()

err := supertokens.Init(configValue)

if err != nil {
t.Error(err.Error())
}

supertokens.LogDebugMessage(logMessage)
assert.NotContains(t, buf.String(), logMessage, "checking log message in logs")
}

func TestLogDebugMessageWithEnvVar(t *testing.T) {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
var logMessage = "test log message"
var buf bytes.Buffer

supertokens.Logger = log.New(&buf, "test", 0)
os.Setenv("SUPERTOKENS_DEBUG", "1")
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved

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()
defer resetLogger()

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")
}
2 changes: 1 addition & 1 deletion supertokens/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

// VERSION current version of the lib
const VERSION = "0.16.4"
const VERSION = "0.16.5"

var (
cdiSupported = []string{"3.0"}
Expand Down
8 changes: 4 additions & 4 deletions supertokens/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const supertokens_namespace = "com.supertokens"
*/

var (
logger = log.New(os.Stdout, supertokens_namespace, 0)
Logger = log.New(os.Stdout, supertokens_namespace, 0)
debugEnabled = false
)

func formatMessage(message string) string {
Expand All @@ -26,8 +27,7 @@ func formatMessage(message string) string {

func LogDebugMessage(message string) {
_, exists := os.LookupEnv("SUPERTOKENS_DEBUG")
if exists {
logger.Printf(formatMessage(message))

if exists || debugEnabled == true {
Logger.Printf(formatMessage(message))
}
}
1 change: 1 addition & 0 deletions supertokens/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type TypeInput struct {
AppInfo AppInfo
RecipeList []Recipe
Telemetry *bool
Debug bool
OnSuperTokensAPIError func(err error, req *http.Request, res http.ResponseWriter)
}

Expand Down
2 changes: 2 additions & 0 deletions supertokens/supertokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func supertokensInit(config TypeInput) error {
superTokens.OnSuperTokensAPIError = config.OnSuperTokensAPIError
}

debugEnabled = config.Debug

LogDebugMessage("Started SuperTokens with debug logging (supertokens.Init called)")

appInfoJsonString, _ := json.Marshal(config.AppInfo)
Expand Down
Loading