Skip to content

Commit 58ed6e5

Browse files
authored
feat_: add pre login log (#6285)
* feat_: add pre login log * chore_: add comments for switchToPreLoginLog
1 parent 873e93a commit 58ed6e5

File tree

7 files changed

+67
-24
lines changed

7 files changed

+67
-24
lines changed

api/geth_backend.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,12 @@ func (b *GethStatusBackend) ensureWalletDBOpened(account multiaccounts.Account,
493493
return nil
494494
}
495495

496-
func (b *GethStatusBackend) setupLogSettings() error {
497-
logSettings := b.config.LogSettings()
496+
func (b *GethStatusBackend) SetupLogSettings() error {
497+
// sync pre_login.log
498+
if err := logutils.ZapLogger().Sync(); err != nil {
499+
return errors.Wrap(err, "failed to sync logger")
500+
}
501+
logSettings := b.config.DefaultLogSettings()
498502
return logutils.OverrideRootLoggerWithConfig(logSettings)
499503
}
500504

@@ -577,7 +581,12 @@ func (b *GethStatusBackend) LoginAccount(request *requests.Login) error {
577581
if b.LocalPairingStateManager.IsPairing() {
578582
return nil
579583
}
580-
return b.LoggedIn(request.KeyUID, err)
584+
err = b.LoggedIn(request.KeyUID, err)
585+
if err != nil {
586+
return errors.Wrap(err, "failed to send LoggedIn signal")
587+
}
588+
589+
return nil
581590
}
582591

583592
// This is a workaround to make user be able to login again, the root cause is where the node config migration
@@ -754,11 +763,6 @@ func (b *GethStatusBackend) loginAccount(request *requests.Login) error {
754763
overrideApiConfig(b.config, request.APIConfig)
755764
}
756765

757-
err = b.setupLogSettings()
758-
if err != nil {
759-
return errors.Wrap(err, "failed to setup log settings")
760-
}
761-
762766
accountsDB, err := accounts.NewDB(b.appDB)
763767
if err != nil {
764768
return errors.Wrap(err, "failed to create accounts db")
@@ -873,11 +877,6 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
873877
return err
874878
}
875879

876-
err = b.setupLogSettings()
877-
if err != nil {
878-
return err
879-
}
880-
881880
accountsDB, err := accounts.NewDB(b.appDB)
882881
if err != nil {
883882
return err
@@ -2681,6 +2680,10 @@ func (b *GethStatusBackend) Logout() error {
26812680
signal.SendNodeStopped()
26822681
}
26832682

2683+
if err = b.switchToPreLoginLog(); err != nil {
2684+
return err
2685+
}
2686+
26842687
// re-initialize the node, at some point we should better manage the lifecycle
26852688
b.initialize()
26862689

@@ -2692,6 +2695,18 @@ func (b *GethStatusBackend) Logout() error {
26922695
return nil
26932696
}
26942697

2698+
// switchToPreLoginLog switches to global pre-login logging settings.
2699+
// This log is profile-independent and should be enabled by default,
2700+
// including in release builds, to help diagnose login issues.
2701+
// related issue: https://github.com/status-im/status-mobile/issues/21501
2702+
func (b *GethStatusBackend) switchToPreLoginLog() error {
2703+
err := logutils.ZapLogger().Sync()
2704+
if err != nil {
2705+
return err
2706+
}
2707+
return logutils.OverrideRootLoggerWithConfig(b.config.PreLoginLogSettings())
2708+
}
2709+
26952710
// cleanupServices stops parts of services that doesn't managed by a node and removes injected data from services.
26962711
func (b *GethStatusBackend) cleanupServices() error {
26972712
b.selectedAccountKeyID = ""

cmd/utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func SetupLogging(logLevel *string, logWithoutColors *bool, config *params.NodeC
1515
config.LogLevel = *logLevel
1616
}
1717

18-
logSettings := config.LogSettings()
18+
logSettings := config.DefaultLogSettings()
1919
logSettings.Colorized = !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd()))
2020
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
2121
stdlog.Fatalf("Error initializing logger: %v", err)

logutils/override.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logutils
22

33
import (
4+
"bufio"
45
"io"
56
"os"
67

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

6468
// FIXME: remove go-libp2p logging altogether

mobile/status.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,14 @@ func initializeLogging(request *requests.InitializeApplication) error {
151151
request.LogDir = request.DataDir
152152
}
153153

154+
if request.LogLevel == "" {
155+
request.LogLevel = params.DefaultPreLoginLogLevel
156+
}
157+
154158
logSettings := logutils.LogSettings{
155-
Enabled: request.LogEnabled,
159+
Enabled: true, // always enable pre-login logging
156160
Level: request.LogLevel,
157-
File: path.Join(request.LogDir, api.DefaultLogFile),
161+
File: path.Join(request.LogDir, params.DefaultPreLoginLogFile),
158162
}
159163

160164
err := os.MkdirAll(request.LogDir, 0700)
@@ -543,7 +547,7 @@ func createAccountAndLogin(requestJSON string) string {
543547
return statusBackend.LoggedIn("", err)
544548
}
545549
logutils.ZapLogger().Debug("started a node, and created account")
546-
return nil
550+
return statusBackend.SetupLogSettings()
547551
})
548552
return makeJSONResponse(nil)
549553
}
@@ -580,7 +584,7 @@ func loginAccount(requestJSON string) string {
580584
return err
581585
}
582586
logutils.ZapLogger().Debug("loginAccount started node")
583-
return nil
587+
return statusBackend.SetupLogSettings()
584588
})
585589
return makeJSONResponse(nil)
586590
}
@@ -615,7 +619,7 @@ func restoreAccountAndLogin(requestJSON string) string {
615619
return statusBackend.LoggedIn("", err)
616620
}
617621
logutils.ZapLogger().Debug("started a node, and restored account")
618-
return nil
622+
return statusBackend.SetupLogSettings()
619623
})
620624

621625
return makeJSONResponse(nil)
@@ -658,7 +662,7 @@ func SaveAccountAndLogin(accountData, password, settingsJSON, configJSON, subacc
658662
return err
659663
}
660664
logutils.ZapLogger().Debug("started a node, and saved account", zap.String("key-uid", account.KeyUID))
661-
return nil
665+
return statusBackend.SetupLogSettings()
662666
})
663667
return makeJSONResponse(nil)
664668
}

params/config.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ func LesTopic(netid int) string {
11571157
}
11581158
}
11591159

1160-
func (c *NodeConfig) LogSettings() logutils.LogSettings {
1160+
func (c *NodeConfig) DefaultLogSettings() logutils.LogSettings {
11611161
return logutils.LogSettings{
11621162
Enabled: c.LogEnabled,
11631163
Level: c.LogLevel,
@@ -1168,3 +1168,19 @@ func (c *NodeConfig) LogSettings() logutils.LogSettings {
11681168
CompressRotated: c.LogCompressRotated,
11691169
}
11701170
}
1171+
1172+
func (c *NodeConfig) PreLoginLogSettings() logutils.LogSettings {
1173+
logFile := filepath.Join(c.LogDir, DefaultPreLoginLogFile)
1174+
if c.LogLevel == "" {
1175+
c.LogLevel = DefaultPreLoginLogLevel
1176+
}
1177+
return logutils.LogSettings{
1178+
Enabled: true,
1179+
Level: c.LogLevel,
1180+
Namespaces: c.LogNamespaces,
1181+
File: logFile,
1182+
MaxSize: c.LogMaxSize,
1183+
MaxBackups: c.LogMaxBackups,
1184+
CompressRotated: c.LogCompressRotated,
1185+
}
1186+
}

params/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ const (
8383

8484
// IpfsGatewayURL is the Gateway URL to use for IPFS
8585
IpfsGatewayURL = "https://ipfs.status.im/"
86+
87+
DefaultPreLoginLogFile = "pre_login.log"
88+
DefaultPreLoginLogLevel = "ERROR"
8689
)
8790

8891
var (

tests-functional/tests/test_init_status_app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ def test_check_logs(log_enabled: bool, api_logging_enabled: bool):
6767
"dataDir": str(data_dir),
6868
"logDir": str(logs_dir),
6969
"logEnabled": log_enabled,
70+
"logLevel": "INFO",
7071
"apiLoggingEnabled": api_logging_enabled,
7172
},
7273
)
7374

74-
local_geth_log = backend.extract_data(os.path.join(logs_dir, "geth.log"))
75+
pre_login_log = backend.extract_data(os.path.join(logs_dir, "pre_login.log"))
7576
local_api_log = backend.extract_data(os.path.join(logs_dir, "api.log"))
7677

77-
assert_file_first_line(path=local_geth_log, pattern="logging initialised", expected=log_enabled)
78+
assert_file_first_line(path=pre_login_log, pattern="logging initialised", expected=True)
7879

7980
assert_file_first_line(
8081
path=local_api_log,

0 commit comments

Comments
 (0)