2024-09-13 15:08:20 +00:00
|
|
|
package requestlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2024-10-24 21:46:23 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2024-09-13 15:08:20 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/logutils"
|
2024-10-24 21:46:23 +00:00
|
|
|
"github.com/status-im/status-go/protocol/zaputil"
|
2024-09-13 15:08:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-10-24 21:46:23 +00:00
|
|
|
requestLogger *zap.Logger
|
2024-09-13 15:08:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsRequestLoggingEnabled returns whether RPC logging is enabled
|
|
|
|
func IsRequestLoggingEnabled() bool {
|
2024-10-24 21:46:23 +00:00
|
|
|
return requestLogger != nil
|
2024-09-13 15:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRequestLogger returns the RPC logger object
|
2024-10-24 21:46:23 +00:00
|
|
|
func GetRequestLogger() *zap.Logger {
|
2024-09-13 15:08:20 +00:00
|
|
|
return requestLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConfigureAndEnableRequestLogging(file string) error {
|
2024-10-24 21:46:23 +00:00
|
|
|
if len(file) == 0 {
|
|
|
|
return errors.New("file is required")
|
2024-09-13 15:08:20 +00:00
|
|
|
}
|
2024-10-24 21:46:23 +00:00
|
|
|
|
|
|
|
if IsRequestLoggingEnabled() {
|
|
|
|
return errors.New("request logging is already enabled")
|
|
|
|
}
|
|
|
|
|
2024-09-13 15:08:20 +00:00
|
|
|
fileOpts := logutils.FileOptions{
|
|
|
|
Filename: file,
|
|
|
|
MaxBackups: 1,
|
|
|
|
}
|
2024-10-24 21:46:23 +00:00
|
|
|
|
|
|
|
core := zapcore.NewCore(
|
|
|
|
zaputil.NewConsoleHexEncoder(zap.NewDevelopmentEncoderConfig()),
|
|
|
|
zapcore.AddSync(logutils.ZapSyncerWithRotation(fileOpts)),
|
|
|
|
zap.DebugLevel,
|
|
|
|
)
|
|
|
|
|
|
|
|
requestLogger = zap.New(core).Named("RequestLogger")
|
|
|
|
|
2024-09-13 15:08:20 +00:00
|
|
|
return nil
|
|
|
|
}
|