Skip to content

Commit

Permalink
feat_: add pre login log
Browse files Browse the repository at this point in the history
  • Loading branch information
qfrank committed Feb 5, 2025
1 parent 712fc66 commit 34fb976
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
37 changes: 24 additions & 13 deletions api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,12 @@ func (b *GethStatusBackend) ensureWalletDBOpened(account multiaccounts.Account,
return nil
}

func (b *GethStatusBackend) setupLogSettings() error {
logSettings := b.config.LogSettings()
func (b *GethStatusBackend) SetupLogSettings() error {
// sync pre_login.log
if err := logutils.ZapLogger().Sync(); err != nil {
return errors.Wrap(err, "failed to sync logger")
}
logSettings := b.config.DefaultLogSettings()
return logutils.OverrideRootLoggerWithConfig(logSettings)
}

Expand Down Expand Up @@ -576,7 +580,12 @@ func (b *GethStatusBackend) LoginAccount(request *requests.Login) error {
if b.LocalPairingStateManager.IsPairing() {
return nil
}
return b.LoggedIn(request.KeyUID, err)
err = b.LoggedIn(request.KeyUID, err)
if err != nil {
return errors.Wrap(err, "failed to send LoggedIn signal")
}

return nil
}

func (b *GethStatusBackend) loginAccount(request *requests.Login) error {
Expand Down Expand Up @@ -646,11 +655,6 @@ func (b *GethStatusBackend) loginAccount(request *requests.Login) error {
overrideApiConfig(b.config, request.APIConfig)
}

err = b.setupLogSettings()
if err != nil {
return errors.Wrap(err, "failed to setup log settings")
}

accountsDB, err := accounts.NewDB(b.appDB)
if err != nil {
return errors.Wrap(err, "failed to create accounts db")
Expand Down Expand Up @@ -765,11 +769,6 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
return err
}

err = b.setupLogSettings()
if err != nil {
return err
}

accountsDB, err := accounts.NewDB(b.appDB)
if err != nil {
return err
Expand Down Expand Up @@ -2573,6 +2572,10 @@ func (b *GethStatusBackend) Logout() error {
signal.SendNodeStopped()
}

if err = b.switchToPreLoginLog(); err != nil {
return err
}

// re-initialize the node, at some point we should better manage the lifecycle
b.initialize()

Expand All @@ -2584,6 +2587,14 @@ func (b *GethStatusBackend) Logout() error {
return nil
}

func (b *GethStatusBackend) switchToPreLoginLog() error {
err := logutils.ZapLogger().Sync()
if err != nil {
return err
}
return logutils.OverrideRootLoggerWithConfig(b.config.PreLoginLogSettings())
}

// cleanupServices stops parts of services that doesn't managed by a node and removes injected data from services.
func (b *GethStatusBackend) cleanupServices() error {
b.selectedAccountKeyID = ""
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func SetupLogging(logLevel *string, logWithoutColors *bool, config *params.NodeC
config.LogLevel = *logLevel
}

logSettings := config.LogSettings()
logSettings := config.DefaultLogSettings()
logSettings.Colorized = !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd()))
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
stdlog.Fatalf("Error initializing logger: %v", err)
Expand Down
6 changes: 5 additions & 1 deletion logutils/override.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logutils

import (
"bufio"
"io"
"os"

Expand Down Expand Up @@ -58,7 +59,10 @@ func overrideCoreWithConfig(filteringCore *namespaceFilteringCore, settings LogS
Compress: settings.CompressRotated,
}))
} else {
core.UpdateSyncer(zapcore.Lock(os.Stderr))
// run TestLoginWithKey will get error: sync /dev/stdout: bad file descriptor
// use bufio.NewWriter to wrap os.Stderr to fix it
writer := bufio.NewWriter(os.Stderr)
core.UpdateSyncer(zapcore.Lock(zapcore.AddSync(writer)))
}

// FIXME: remove go-libp2p logging altogether
Expand Down
16 changes: 10 additions & 6 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,14 @@ func initializeLogging(request *requests.InitializeApplication) error {
request.LogDir = request.DataDir
}

if request.LogLevel == "" {
request.LogLevel = params.DefaultPreLoginLogLevel
}

logSettings := logutils.LogSettings{
Enabled: request.LogEnabled,
Enabled: true, // always enable pre-login logging
Level: request.LogLevel,
File: path.Join(request.LogDir, api.DefaultLogFile),
File: path.Join(request.LogDir, params.DefaultPreLoginLogFile),
}

err := os.MkdirAll(request.LogDir, 0700)
Expand Down Expand Up @@ -543,7 +547,7 @@ func createAccountAndLogin(requestJSON string) string {
return statusBackend.LoggedIn("", err)
}
logutils.ZapLogger().Debug("started a node, and created account")
return nil
return statusBackend.SetupLogSettings()
})
return makeJSONResponse(nil)
}
Expand Down Expand Up @@ -580,7 +584,7 @@ func loginAccount(requestJSON string) string {
return err
}
logutils.ZapLogger().Debug("loginAccount started node")
return nil
return statusBackend.SetupLogSettings()
})
return makeJSONResponse(nil)
}
Expand Down Expand Up @@ -615,7 +619,7 @@ func restoreAccountAndLogin(requestJSON string) string {
return statusBackend.LoggedIn("", err)
}
logutils.ZapLogger().Debug("started a node, and restored account")
return nil
return statusBackend.SetupLogSettings()
})

return makeJSONResponse(nil)
Expand Down Expand Up @@ -658,7 +662,7 @@ func SaveAccountAndLogin(accountData, password, settingsJSON, configJSON, subacc
return err
}
logutils.ZapLogger().Debug("started a node, and saved account", zap.String("key-uid", account.KeyUID))
return nil
return statusBackend.SetupLogSettings()
})
return makeJSONResponse(nil)
}
Expand Down
18 changes: 17 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ func LesTopic(netid int) string {
}
}

func (c *NodeConfig) LogSettings() logutils.LogSettings {
func (c *NodeConfig) DefaultLogSettings() logutils.LogSettings {
return logutils.LogSettings{
Enabled: c.LogEnabled,
Level: c.LogLevel,
Expand All @@ -1162,3 +1162,19 @@ func (c *NodeConfig) LogSettings() logutils.LogSettings {
CompressRotated: c.LogCompressRotated,
}
}

func (c *NodeConfig) PreLoginLogSettings() logutils.LogSettings {
logFile := filepath.Join(c.LogDir, DefaultPreLoginLogFile)
if c.LogLevel == "" {
c.LogLevel = DefaultPreLoginLogLevel
}
return logutils.LogSettings{
Enabled: true,
Level: c.LogLevel,
Namespaces: c.LogNamespaces,
File: logFile,
MaxSize: c.LogMaxSize,
MaxBackups: c.LogMaxBackups,
CompressRotated: c.LogCompressRotated,
}
}
3 changes: 3 additions & 0 deletions params/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ const (

// IpfsGatewayURL is the Gateway URL to use for IPFS
IpfsGatewayURL = "https://ipfs.status.im/"

DefaultPreLoginLogFile = "pre_login.log"
DefaultPreLoginLogLevel = "ERROR"
)

var (
Expand Down
5 changes: 3 additions & 2 deletions tests-functional/tests/test_init_status_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ def test_check_logs(log_enabled: bool, api_logging_enabled: bool):
"dataDir": str(data_dir),
"logDir": str(logs_dir),
"logEnabled": log_enabled,
"logLevel": "INFO",
"apiLoggingEnabled": api_logging_enabled,
},
)

local_geth_log = backend.extract_data(os.path.join(logs_dir, "geth.log"))
pre_login_log = backend.extract_data(os.path.join(logs_dir, "pre_login.log"))
local_api_log = backend.extract_data(os.path.join(logs_dir, "api.log"))

assert_file_first_line(path=local_geth_log, pattern="logging initialised", expected=log_enabled)
assert_file_first_line(path=pre_login_log, pattern="logging initialised", expected=True)

assert_file_first_line(
path=local_api_log,
Expand Down

0 comments on commit 34fb976

Please sign in to comment.