fix_: setupLogSettings code duplication (#6142)

This commit is contained in:
Igor Sirotin 2024-12-03 08:56:07 +00:00 committed by GitHub
parent 4aa683f03d
commit 78da423569
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 42 additions and 72 deletions

View File

@ -492,18 +492,8 @@ func (b *GethStatusBackend) ensureWalletDBOpened(account multiaccounts.Account,
} }
func (b *GethStatusBackend) setupLogSettings() error { func (b *GethStatusBackend) setupLogSettings() error {
logSettings := logutils.LogSettings{ logSettings := b.config.LogSettings()
Enabled: b.config.LogEnabled, return logutils.OverrideRootLoggerWithConfig(logSettings)
Level: b.config.LogLevel,
File: b.config.LogFile,
MaxSize: b.config.LogMaxSize,
MaxBackups: b.config.LogMaxBackups,
CompressRotated: b.config.LogCompressRotated,
}
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
return err
}
return nil
} }
// Deprecated: Use StartNodeWithAccount instead. // Deprecated: Use StartNodeWithAccount instead.

View File

@ -25,6 +25,7 @@ import (
"github.com/status-im/status-go/multiaccounts/accounts" "github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/multiaccounts/settings" "github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/cmd/utils"
"github.com/status-im/status-go/logutils" "github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol" "github.com/status-im/status-go/protocol"
@ -116,7 +117,7 @@ func main() {
} }
// set up logging options // set up logging options
setupLogging(config) utils.SetupLogging(logLevel, logWithoutColors, config)
// We want statusd to be distinct from StatusIM client. // We want statusd to be distinct from StatusIM client.
config.Name = serverClientName config.Name = serverClientName
@ -231,25 +232,6 @@ func getDefaultDataDir() string {
return "./statusd-data" return "./statusd-data"
} }
func setupLogging(config *params.NodeConfig) {
if *logLevel != "" {
config.LogLevel = *logLevel
}
logSettings := logutils.LogSettings{
Enabled: config.LogEnabled,
Level: config.LogLevel,
File: config.LogFile,
MaxSize: config.LogMaxSize,
MaxBackups: config.LogMaxBackups,
CompressRotated: config.LogCompressRotated,
Colorized: !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd())),
}
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
stdlog.Fatalf("Error initializing logger: %v", err)
}
}
// printVersion prints verbose output about version and config. // printVersion prints verbose output about version and config.
func printVersion(config *params.NodeConfig) { func printVersion(config *params.NodeConfig) {
fmt.Println(strings.Title(config.Name)) fmt.Println(strings.Title(config.Name))

View File

@ -20,6 +20,7 @@ import (
"github.com/status-im/status-go/account/generator" "github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/api" "github.com/status-im/status-go/api"
"github.com/status-im/status-go/cmd/utils"
"github.com/status-im/status-go/common/dbsetup" "github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
@ -125,7 +126,7 @@ func main() {
} }
// set up logging options // set up logging options
setupLogging(config) utils.SetupLogging(logLevel, logWithoutColors, config)
// We want statusd to be distinct from StatusIM client. // We want statusd to be distinct from StatusIM client.
config.Name = serverClientName config.Name = serverClientName
@ -275,25 +276,6 @@ func getDefaultDataDir() string {
return "./statusd-data" return "./statusd-data"
} }
func setupLogging(config *params.NodeConfig) {
if *logLevel != "" {
config.LogLevel = *logLevel
}
logSettings := logutils.LogSettings{
Enabled: config.LogEnabled,
Level: config.LogLevel,
File: config.LogFile,
MaxSize: config.LogMaxSize,
MaxBackups: config.LogMaxBackups,
CompressRotated: config.LogCompressRotated,
Colorized: !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd())),
}
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
stdlog.Fatalf("Error initializing logger: %v", err)
}
}
// printVersion prints verbose output about version and config. // printVersion prints verbose output about version and config.
func printVersion(config *params.NodeConfig) { func printVersion(config *params.NodeConfig) {
fmt.Println(strings.Title(config.Name)) fmt.Println(strings.Title(config.Name))

View File

@ -24,6 +24,7 @@ import (
"github.com/status-im/status-go/api" "github.com/status-im/status-go/api"
"github.com/status-im/status-go/appdatabase" "github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/cmd/status-backend/server" "github.com/status-im/status-go/cmd/status-backend/server"
"github.com/status-im/status-go/cmd/utils"
"github.com/status-im/status-go/common/dbsetup" "github.com/status-im/status-go/common/dbsetup"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth" gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
@ -163,7 +164,7 @@ func main() {
} }
// set up logging options // set up logging options
setupLogging(config) utils.SetupLogging(logLevel, logWithoutColors, config)
// We want statusd to be distinct from StatusIM client. // We want statusd to be distinct from StatusIM client.
config.Name = serverClientName config.Name = serverClientName
@ -389,25 +390,6 @@ func getDefaultDataDir() string {
return "./statusd-data" return "./statusd-data"
} }
func setupLogging(config *params.NodeConfig) {
if *logLevel != "" {
config.LogLevel = *logLevel
}
logSettings := logutils.LogSettings{
Enabled: config.LogEnabled,
Level: config.LogLevel,
File: config.LogFile,
MaxSize: config.LogMaxSize,
MaxBackups: config.LogMaxBackups,
CompressRotated: config.LogCompressRotated,
Colorized: !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd())),
}
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
stdlog.Fatalf("Error initializing logger: %v", err)
}
}
// loop for notifying systemd about process being alive // loop for notifying systemd about process being alive
func startSystemDWatchdog() { func startSystemDWatchdog() {
for range time.Tick(30 * time.Second) { for range time.Tick(30 * time.Second) {

23
cmd/utils/utils.go Normal file
View File

@ -0,0 +1,23 @@
package utils
import (
stdlog "log"
"os"
"golang.org/x/crypto/ssh/terminal"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/params"
)
func SetupLogging(logLevel *string, logWithoutColors *bool, config *params.NodeConfig) {
if *logLevel != "" {
config.LogLevel = *logLevel
}
logSettings := config.LogSettings()
logSettings.Colorized = !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd()))
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
stdlog.Fatalf("Error initializing logger: %v", err)
}
}

View File

@ -1191,3 +1191,14 @@ func LesTopic(netid int) string {
return "" return ""
} }
} }
func (c *NodeConfig) LogSettings() logutils.LogSettings {
return logutils.LogSettings{
Enabled: c.LogEnabled,
Level: c.LogLevel,
File: c.LogFile,
MaxSize: c.LogMaxSize,
MaxBackups: c.LogMaxBackups,
CompressRotated: c.LogCompressRotated,
}
}