Skip to content

Commit

Permalink
fix_: set log level for mobile release build won't generate log file
Browse files Browse the repository at this point in the history
  • Loading branch information
qfrank committed Dec 12, 2024
1 parent ef177c1 commit 31d9c6f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions nodecfg/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,11 @@ func SetLogLevel(db *sql.DB, logLevel string) error {
return err
}

func SetLogEnabled(db *sql.DB, enabled bool) error {
_, err := db.Exec(`UPDATE log_config SET enabled = ?`, enabled)
return err
}

func SetLogNamespaces(db *sql.DB, logNamespaces string) error {
_, err := db.Exec(`UPDATE log_config SET log_namespaces = ?`, logNamespaces)
return err
Expand Down
27 changes: 27 additions & 0 deletions protocol/messenger_sync_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
gocommon "github.com/status-im/status-go/common"
"github.com/status-im/status-go/multiaccounts/errors"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/nodecfg"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
)
Expand Down Expand Up @@ -166,6 +167,7 @@ func (m *Messenger) startSettingsChangesLoop() {
channel := m.settings.SubscribeToChanges()
go func() {
defer gocommon.LogOnPanic()
logger := m.logger.Named("SettingsChangesLoop")
for {
select {
case s := <-channel:
Expand All @@ -179,10 +181,35 @@ func (m *Messenger) startSettingsChangesLoop() {
case settings.Bio.GetReactName():
m.selfContact.Bio = s.Value.(string)
m.publishSelfContactSubscriptions(&SelfContactChangeEvent{BioChanged: true})
case settings.LogLevel.GetReactName():
level := s.Value.(string)
m.updateLogConfig(level, logger)
}
case <-m.quit:
return
}
}
}()
}

func (m *Messenger) updateLogConfig(level string, logger *zap.Logger) {
m.setLogLevel(level, logger)
m.setLogEnabled(level != "", logger)
}

func (m *Messenger) setLogLevel(level string, logger *zap.Logger) {
if level == "" {
return
}
err := nodecfg.SetLogLevel(m.database, level)
if err != nil {
logger.Error("nodecfg.SetLogLevel", zap.Error(err))
}
}

func (m *Messenger) setLogEnabled(logEnabled bool, logger *zap.Logger) {
err := nodecfg.SetLogEnabled(m.database, logEnabled)
if err != nil {
logger.Error("nodecfg.SetLogEnabled", zap.Error(err))
}
}
29 changes: 29 additions & 0 deletions protocol/messenger_sync_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/nodecfg"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/tt"
Expand Down Expand Up @@ -311,3 +312,31 @@ func (s *MessengerSyncSettingsSuite) TestSyncSettings_PreferredName() {
s.Require().NoError(err)
s.Require().Equal(pf2, opn)
}

func (s *MessengerSyncSettingsSuite) TestUpdateLogConfig() {
// ensure log level is empty and log is disabled before the test
cfg, err := nodecfg.GetNodeConfigFromDB(s.alice.database)
s.Require().NoError(err)
s.Require().Equal(cfg.LogLevel, "")
s.Require().False(cfg.LogEnabled)

// case 1
// WHEN
expectedLogLevel := "debug"
s.alice.updateLogConfig(expectedLogLevel, s.logger)
// THEN
cfg, err = nodecfg.GetNodeConfigFromDB(s.alice.database)
s.Require().NoError(err)
s.Require().Equal(expectedLogLevel, cfg.LogLevel)
s.Require().True(cfg.LogEnabled)

// case 2
// empty log level should disable logging, but keep the log level
// WHEN
s.alice.updateLogConfig("", s.logger)
// THEN
cfg, err = nodecfg.GetNodeConfigFromDB(s.alice.database)
s.Require().NoError(err)
s.Require().Equal(expectedLogLevel, cfg.LogLevel)
s.Require().False(cfg.LogEnabled)
}
15 changes: 15 additions & 0 deletions protocol/node_config_persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ func (s *NodeConfigPersistenceTestSuite) Test_SetLogLevelDebug() {
s.Require().NoError(err)
s.Require().Equal("DEBUG", dbNodeConfig.LogLevel)
}

func (s *NodeConfigPersistenceTestSuite) Test_SetLogEnabled() {
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(s.db)
s.Require().NoError(err)
s.Require().False(dbNodeConfig.LogEnabled)

// WHEN
err = nodecfg.SetLogEnabled(s.db, true)
s.Require().NoError(err)

// THEN
dbNodeConfig, err = nodecfg.GetNodeConfigFromDB(s.db)
s.Require().NoError(err)
s.Require().True(dbNodeConfig.LogEnabled)
}

0 comments on commit 31d9c6f

Please sign in to comment.