Skip to content

Commit 6c79ea8

Browse files
committed
feat: configuration for log rotation
1 parent c6df7c8 commit 6c79ea8

3 files changed

Lines changed: 112 additions & 4 deletions

File tree

cmd/peerswaplnd/config.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type PeerSwapConfig struct {
4444
DataDir string `long:"datadir" description:"peerswap datadir"`
4545
LogLevel LogLevel `long:"loglevel" description:"loglevel (1=Info, 2=Debug)"`
4646

47+
LogRotation LogRotationConfig `group:"Log rotation" namespace:"logrotation"`
48+
4749
LndConfig *LndConfig `group:"Lnd Grpc config" namespace:"lnd"`
4850
ElementsConfig *OnchainConfig `group:"Elements Rpc Config" namespace:"elementsd"`
4951
LWKConfig *lwk.Conf
@@ -75,6 +77,9 @@ func (p *PeerSwapConfig) String() string {
7577
}
7678

7779
func (p *PeerSwapConfig) Validate() error {
80+
if err := p.LogRotation.Validate(); err != nil {
81+
return err
82+
}
7883
if p.ElementsConfig.RpcHost != "" && p.ElementsConfig.LiquidSwaps != false {
7984
err := p.ElementsConfig.Validate()
8085
if err != nil {
@@ -139,6 +144,28 @@ type LndConfig struct {
139144
MacaroonPath string `long:"macaroonpath" description:"path to the macaroon (admin.macaroon or custom baked one)"`
140145
}
141146

147+
type LogRotationConfig struct {
148+
// In megabytes
149+
MaxSize int `long:"maxsize" description:"maximum size in megabytes of the log file before rotation."`
150+
MaxBackups int `long:"maxbackups" description:"maximum number of old log files to retain."`
151+
// In days
152+
MaxAge int `long:"maxage" description:"maximum number of days to retain old log files."`
153+
Compress bool `long:"compress" description:"whether to compress log files using gzip"`
154+
}
155+
156+
func (l LogRotationConfig) Validate() error {
157+
if l.MaxSize <= 0 {
158+
return fmt.Errorf("logrotation.maxsize must be > 0, got %d", l.MaxSize)
159+
}
160+
if l.MaxBackups < 0 {
161+
return fmt.Errorf("logrotation.maxbackups must be >= 0, got %d", l.MaxBackups)
162+
}
163+
if l.MaxAge < 0 {
164+
return fmt.Errorf("logrotation.maxage must be >= 0, got %d", l.MaxAge)
165+
}
166+
return nil
167+
}
168+
142169
func DefaultConfig() *PeerSwapConfig {
143170
return &PeerSwapConfig{
144171
Host: DefaultPeerswapHost,
@@ -154,6 +181,7 @@ func DefaultConfig() *PeerSwapConfig {
154181
BitcoinEnabled: DefaultBitcoinEnabled,
155182
ElementsConfig: defaultLiquidConfig(),
156183
LogLevel: DefaultLogLevel,
184+
LogRotation: defaultLogRotationConfig(),
157185
}
158186
}
159187

@@ -170,6 +198,15 @@ func defaultLiquidConfig() *OnchainConfig {
170198
}
171199
}
172200

201+
func defaultLogRotationConfig() LogRotationConfig {
202+
return LogRotationConfig{
203+
MaxSize: 10,
204+
MaxBackups: 5,
205+
MaxAge: 28,
206+
Compress: true,
207+
}
208+
}
209+
173210
func LWKFromIniFileConfig(filePath string) (*lwk.Conf, error) {
174211
type LWK struct {
175212
SignerName string `long:"signername" description:"name of the signer"`

cmd/peerswaplnd/config_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,77 @@ import (
1111
"github.com/vulpemventures/go-elements/network"
1212
)
1313

14+
func TestLogRotationConfigValidate(t *testing.T) {
15+
t.Parallel()
16+
17+
tests := []struct {
18+
name string
19+
cfg peerswaplnd.LogRotationConfig
20+
wantErr bool
21+
}{
22+
{
23+
name: "valid values",
24+
cfg: peerswaplnd.LogRotationConfig{
25+
MaxSize: 10,
26+
MaxBackups: 5,
27+
MaxAge: 28,
28+
Compress: true,
29+
},
30+
wantErr: false,
31+
},
32+
{
33+
name: "maxsize zero",
34+
cfg: peerswaplnd.LogRotationConfig{
35+
MaxSize: 0,
36+
MaxBackups: 5,
37+
MaxAge: 28,
38+
},
39+
wantErr: true,
40+
},
41+
{
42+
name: "maxsize negative",
43+
cfg: peerswaplnd.LogRotationConfig{
44+
MaxSize: -1,
45+
MaxBackups: 5,
46+
MaxAge: 28,
47+
},
48+
wantErr: true,
49+
},
50+
{
51+
name: "maxbackups negative",
52+
cfg: peerswaplnd.LogRotationConfig{
53+
MaxSize: 10,
54+
MaxBackups: -1,
55+
MaxAge: 28,
56+
},
57+
wantErr: true,
58+
},
59+
{
60+
name: "maxage negative",
61+
cfg: peerswaplnd.LogRotationConfig{
62+
MaxSize: 10,
63+
MaxBackups: 5,
64+
MaxAge: -1,
65+
},
66+
wantErr: true,
67+
},
68+
}
69+
70+
for _, tt := range tests {
71+
tt := tt
72+
t.Run(tt.name, func(t *testing.T) {
73+
t.Parallel()
74+
75+
err := tt.cfg.Validate()
76+
if tt.wantErr {
77+
assert.Error(t, err)
78+
} else {
79+
assert.NoError(t, err)
80+
}
81+
})
82+
}
83+
}
84+
1485
func TestLWKFromIniFileConfig(t *testing.T) {
1586
t.Parallel()
1687
t.Run("valid ini config", func(t *testing.T) {

cmd/peerswaplnd/peerswapd/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,10 @@ func NewLndLogger(cfg *peerswaplnd.PeerSwapConfig) (*LndLogger, error) {
608608

609609
w := io.MultiWriter(os.Stdout, &lumberjack.Logger{
610610
Filename: logFile,
611-
MaxSize: 10, // megabytes
612-
MaxBackups: 5,
613-
MaxAge: 28, // days
614-
Compress: true,
611+
MaxSize: cfg.LogRotation.MaxSize,
612+
MaxBackups: cfg.LogRotation.MaxBackups,
613+
MaxAge: cfg.LogRotation.MaxAge,
614+
Compress: cfg.LogRotation.Compress,
615615
})
616616
core_log.SetOutput(w)
617617

0 commit comments

Comments
 (0)