From 4384c37f138bcb7b5e0a72b1eb058099e782fc11 Mon Sep 17 00:00:00 2001 From: Mohammed Madi <66013188+mohammed-madi@users.noreply.github.com> Date: Thu, 24 Aug 2023 17:31:01 +0100 Subject: [PATCH] CBG-3324: Add startup cnf logging to DefaultDbConfig (#6381) (cherry picked from commit 5288dbf17a705c668d588bb36dde7fb8fd22a2e9) --- rest/adminapitest/admin_api_test.go | 34 ++++++++++++++++++++++++++++- rest/config_database.go | 15 +++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/rest/adminapitest/admin_api_test.go b/rest/adminapitest/admin_api_test.go index ca927267e2..5a35840849 100644 --- a/rest/adminapitest/admin_api_test.go +++ b/rest/adminapitest/admin_api_test.go @@ -485,7 +485,7 @@ func TestDBOfflinePutDbConfig(t *testing.T) { // Tests that the users returned in the config endpoint have the correct names // Reproduces #2223 -func TestDBGetConfigNames(t *testing.T) { +func TestDBGetConfigNamesAndDefaultLogging(t *testing.T) { p := "password" rt := rest.NewRestTester(t, @@ -507,11 +507,38 @@ func TestDBGetConfigNames(t *testing.T) { require.NoError(t, base.JSONUnmarshal(response.Body.Bytes(), &body)) assert.Equal(t, len(rt.DatabaseConfig.Users), len(body.Users)) + emptyCnf := &rest.DbLoggingConfig{} + assert.Equal(t, body.Logging, emptyCnf) for k, v := range body.Users { assert.Equal(t, k, *v.Name) } +} + +func TestDBGetConfigCustomLogging(t *testing.T) { + logKeys := []string{base.KeyAccess.String(), base.KeyHTTP.String()} + rt := rest.NewRestTester(t, + &rest.RestTesterConfig{ + DatabaseConfig: &rest.DatabaseConfig{DbConfig: rest.DbConfig{ + Logging: &rest.DbLoggingConfig{ + Console: &rest.DbConsoleLoggingConfig{ + LogLevel: base.LogLevelPtr(base.LevelError), + LogKeys: logKeys, + }, + }, + }, + }, + }, + ) + defer rt.Close() + + response := rt.SendAdminRequest("GET", "/db/_config?include_runtime=true", "") + var body rest.DbConfig + require.NoError(t, base.JSONUnmarshal(response.Body.Bytes(), &body)) + + assert.Equal(t, body.Logging.Console.LogLevel, base.LogLevelPtr(base.LevelError)) + assert.Equal(t, body.Logging.Console.LogKeys, logKeys) } // Take DB offline and ensure can post _resync @@ -3349,6 +3376,8 @@ func TestConfigsIncludeDefaults(t *testing.T) { // Start SG with no databases config := rest.BootstrapStartupConfigForTest(t) sc, err := rest.SetupServerContext(ctx, &config, true) + config.Logging.Console.LogKeys = []string{base.KeyDCP.String()} + config.Logging.Console.LogLevel.Set(base.LevelDebug) require.NoError(t, err) defer func() { sc.Close(ctx) @@ -3381,6 +3410,9 @@ func TestConfigsIncludeDefaults(t *testing.T) { assert.Equal(t, false, *dbConfig.StartOffline) assert.Equal(t, db.DefaultCompactInterval, uint32(*dbConfig.CompactIntervalDays)) + assert.Equal(t, dbConfig.Logging.Console.LogLevel.String(), base.LevelDebug.String()) + assert.Equal(t, dbConfig.Logging.Console.LogKeys, []string{base.KeyDCP.String()}) + var runtimeServerConfigResponse rest.RunTimeServerConfigResponse resp = rest.BootstrapAdminRequest(t, http.MethodGet, "/_config?include_runtime=true", "") resp.RequireStatus(http.StatusOK) diff --git a/rest/config_database.go b/rest/config_database.go index 371bb34f79..20194bf27a 100644 --- a/rest/config_database.go +++ b/rest/config_database.go @@ -85,6 +85,20 @@ func GenerateDatabaseConfigVersionID(ctx context.Context, previousRevID string, return hash, nil } +func DefaultPerDBLogging(bootstrapLoggingCnf base.LoggingConfig) *DbLoggingConfig { + if bootstrapLoggingCnf.Console != nil { + if *bootstrapLoggingCnf.Console.Enabled { + return &DbLoggingConfig{ + Console: &DbConsoleLoggingConfig{ + LogLevel: bootstrapLoggingCnf.Console.LogLevel, + LogKeys: bootstrapLoggingCnf.Console.LogKeys, + }, + } + } + } + return &DbLoggingConfig{} +} + // MergeDatabaseConfigWithDefaults merges the passed in config onto a DefaultDbConfig which results in returned value // being populated with defaults when not set func MergeDatabaseConfigWithDefaults(sc *StartupConfig, dbConfig *DbConfig) (*DbConfig, error) { @@ -163,6 +177,7 @@ func DefaultDbConfig(sc *StartupConfig) *DbConfig { ClientPartitionWindowSecs: base.IntPtr(int(base.DefaultClientPartitionWindow.Seconds())), JavascriptTimeoutSecs: base.Uint32Ptr(base.DefaultJavascriptTimeoutSecs), Suspendable: base.BoolPtr(sc.IsServerless()), + Logging: DefaultPerDBLogging(sc.Logging), } revsLimit := db.DefaultRevsLimitNoConflicts