Skip to content

Commit 3d90b00

Browse files
committed
feat_: add pre login log
1 parent 06d2419 commit 3d90b00

File tree

6 files changed

+60
-22
lines changed

6 files changed

+60
-22
lines changed

api/geth_backend.go

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

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

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

582591
func (b *GethStatusBackend) loginAccount(request *requests.Login) error {
@@ -646,11 +655,6 @@ func (b *GethStatusBackend) loginAccount(request *requests.Login) error {
646655
overrideApiConfig(b.config, request.APIConfig)
647656
}
648657

649-
err = b.setupLogSettings()
650-
if err != nil {
651-
return errors.Wrap(err, "failed to setup log settings")
652-
}
653-
654658
accountsDB, err := accounts.NewDB(b.appDB)
655659
if err != nil {
656660
return errors.Wrap(err, "failed to create accounts db")
@@ -765,11 +769,6 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
765769
return err
766770
}
767771

768-
err = b.setupLogSettings()
769-
if err != nil {
770-
return err
771-
}
772-
773772
accountsDB, err := accounts.NewDB(b.appDB)
774773
if err != nil {
775774
return err
@@ -2573,6 +2572,10 @@ func (b *GethStatusBackend) Logout() error {
25732572
signal.SendNodeStopped()
25742573
}
25752574

2575+
if err = b.switchToPreLoginLog(); err != nil {
2576+
return err
2577+
}
2578+
25762579
// re-initialize the node, at some point we should better manage the lifecycle
25772580
b.initialize()
25782581

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

2590+
func (b *GethStatusBackend) switchToPreLoginLog() error {
2591+
err := logutils.ZapLogger().Sync()
2592+
if err != nil {
2593+
return err
2594+
}
2595+
return logutils.OverrideRootLoggerWithConfig(b.config.PreLoginLogSettings())
2596+
}
2597+
25872598
// cleanupServices stops parts of services that doesn't managed by a node and removes injected data from services.
25882599
func (b *GethStatusBackend) cleanupServices() error {
25892600
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
@@ -1166,7 +1166,7 @@ func LesTopic(netid int) string {
11661166
}
11671167
}
11681168

1169-
func (c *NodeConfig) LogSettings() logutils.LogSettings {
1169+
func (c *NodeConfig) DefaultLogSettings() logutils.LogSettings {
11701170
return logutils.LogSettings{
11711171
Enabled: c.LogEnabled,
11721172
Level: c.LogLevel,
@@ -1177,3 +1177,19 @@ func (c *NodeConfig) LogSettings() logutils.LogSettings {
11771177
CompressRotated: c.LogCompressRotated,
11781178
}
11791179
}
1180+
1181+
func (c *NodeConfig) PreLoginLogSettings() logutils.LogSettings {
1182+
logFile := filepath.Join(c.LogDir, DefaultPreLoginLogFile)
1183+
if c.LogLevel == "" {
1184+
c.LogLevel = DefaultPreLoginLogLevel
1185+
}
1186+
return logutils.LogSettings{
1187+
Enabled: true,
1188+
Level: c.LogLevel,
1189+
Namespaces: c.LogNamespaces,
1190+
File: logFile,
1191+
MaxSize: c.LogMaxSize,
1192+
MaxBackups: c.LogMaxBackups,
1193+
CompressRotated: c.LogCompressRotated,
1194+
}
1195+
}

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 (

0 commit comments

Comments
 (0)