Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement request logging functionality #5812

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions logutils/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

type LogSettings struct {
Enabled bool
MobileSystem bool
Level string
File string
MaxSize int
MaxBackups int
CompressRotated bool
Enabled bool `json:"Enabled"`
MobileSystem bool `json:"MobileSystem"`
Level string `json:"Level"`
File string `json:"File"`
MaxSize int `json:"MaxSize"`
MaxBackups int `json:"MaxBackups"`
CompressRotated bool `json:"CompressRotated"`
}

// OverrideWithStdLogger overwrites ethereum's root logger with a logger from golang std lib.
Expand Down
59 changes: 59 additions & 0 deletions logutils/requestlog/request_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package requestlog

import (
"errors"
"sync/atomic"

"github.com/ethereum/go-ethereum/log"

"github.com/status-im/status-go/logutils"
)

var (
// requestLogger is the request logger object
requestLogger log.Logger
// isRequestLoggingEnabled controls whether request logging is enabled
isRequestLoggingEnabled atomic.Bool
)

// NewRequestLogger creates a new request logger object
func NewRequestLogger(ctx ...interface{}) log.Logger {
requestLogger = log.New(ctx...)
return requestLogger
}

// EnableRequestLogging enables or disables RPC logging
func EnableRequestLogging(enable bool) {
if enable {
isRequestLoggingEnabled.Store(true)
} else {
isRequestLoggingEnabled.Store(false)
}
}

// IsRequestLoggingEnabled returns whether RPC logging is enabled
func IsRequestLoggingEnabled() bool {
return isRequestLoggingEnabled.Load()
}

// GetRequestLogger returns the RPC logger object
func GetRequestLogger() log.Logger {
return requestLogger
}

func ConfigureAndEnableRequestLogging(file string) error {
log.Info("initialising request logger", "log file", file)
requestLogger := NewRequestLogger()
if file == "" {
return errors.New("log file path is required")
}
fileOpts := logutils.FileOptions{
Filename: file,
MaxBackups: 1,
}
handler := logutils.FileHandlerWithRotation(fileOpts, log.LogfmtFormat())
filteredHandler := log.LvlFilterHandler(log.LvlDebug, handler)
requestLogger.SetHandler(filteredHandler)
EnableRequestLogging(true)
return nil
}
47 changes: 47 additions & 0 deletions mobile/init_logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package statusgo

import (
"fmt"
"os"
"path"
"testing"

"github.com/stretchr/testify/require"

"github.com/status-im/status-go/logutils/requestlog"
"github.com/status-im/status-go/protocol/requests"
)

func TestInitLogging(t *testing.T) {
tempDir := t.TempDir()
t.Logf("temp dir: %s", tempDir)
gethLogFile := path.Join(tempDir, "geth.log")
requestsLogFile := path.Join(tempDir, "requests.log")
logSettings := fmt.Sprintf(`{"LogRequestGo": true, "LogRequestFile": "%s", "File": "%s", "Level": "INFO", "Enabled": true, "MobileSystem": false}`, requestsLogFile, gethLogFile)
response := InitLogging(logSettings)
require.Equal(t, `{"error":""}`, response)
_, err := os.Stat(gethLogFile)
require.NoError(t, err)
require.True(t, requestlog.IsRequestLoggingEnabled())

// requests log file should not be created yet
_, err = os.Stat(requestsLogFile)
require.Error(t, err)
require.True(t, os.IsNotExist(err))

createAccountRequest := &requests.CreateAccount{
DisplayName: "some-display-name",
CustomizationColor: "#ffffff",
Password: "some-password",
RootDataDir: tempDir,
LogFilePath: gethLogFile,
}
_, err = statusBackend.CreateAccountAndLogin(createAccountRequest)
require.NoError(t, err)
result := CallPrivateRPC(`{"jsonrpc":"2.0","method":"settings_getSettings","params":[],"id":1}`)
require.NotContains(t, result, "error")
// Check if request log file exists now
_, err = os.Stat(requestsLogFile)
require.NoError(t, err)
require.FileExists(t, requestsLogFile)
}
Loading