feat recorder: add rotate options

This commit is contained in:
Denis Galeev
2025-02-06 17:34:13 +03:00
committed by ginuerzh
parent 75a106ee84
commit e4b1c3c2b5
3 changed files with 45 additions and 19 deletions
+3 -2
View File
@@ -278,8 +278,9 @@ type RecorderConfig struct {
}
type FileRecorder struct {
Path string `json:"path"`
Sep string `yaml:",omitempty" json:"sep,omitempty"`
Path string `json:"path"`
Sep string `yaml:",omitempty" json:"sep,omitempty"`
Rotation *LogRotationConfig `yaml:",omitempty" json:"rotation,omitempty"`
}
type TCPRecorder struct {
+32 -3
View File
@@ -2,16 +2,26 @@ package recorder
import (
"crypto/tls"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-gost/core/logger"
"github.com/go-gost/core/recorder"
"github.com/go-gost/x/config"
"github.com/go-gost/x/internal/plugin"
xrecorder "github.com/go-gost/x/recorder"
recorder_plugin "github.com/go-gost/x/recorder/plugin"
"gopkg.in/natefinch/lumberjack.v2"
)
type discardCloser struct{}
func (discardCloser) Write(p []byte) (n int, err error) { return len(p), nil }
func (discardCloser) Close() error { return nil }
func ParseRecorder(cfg *config.RecorderConfig) (r recorder.Recorder) {
if cfg == nil {
return nil
@@ -42,9 +52,28 @@ func ParseRecorder(cfg *config.RecorderConfig) (r recorder.Recorder) {
}
if cfg.File != nil && cfg.File.Path != "" {
return xrecorder.FileRecorder(cfg.File.Path,
xrecorder.SepRecorderOption(cfg.File.Sep),
)
var out io.WriteCloser = discardCloser{}
if cfg.File.Rotation != nil {
out = &lumberjack.Logger{
Filename: cfg.File.Path,
MaxSize: cfg.File.Rotation.MaxSize,
MaxAge: cfg.File.Rotation.MaxAge,
MaxBackups: cfg.File.Rotation.MaxBackups,
LocalTime: cfg.File.Rotation.LocalTime,
Compress: cfg.File.Rotation.Compress,
}
} else {
os.MkdirAll(filepath.Dir(cfg.File.Path), 0755)
f, err := os.OpenFile(cfg.File.Path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
logger.Default().Warn(err)
} else {
out = f
}
}
return xrecorder.FileRecorder(out, xrecorder.SepRecorderOption(cfg.File.Sep))
}
if cfg.TCP != nil && cfg.TCP.Addr != "" {
+10 -14
View File
@@ -2,7 +2,7 @@ package recorder
import (
"context"
"os"
"io"
"github.com/go-gost/core/recorder"
)
@@ -20,40 +20,36 @@ func SepRecorderOption(sep string) FileRecorderOption {
}
type fileRecorder struct {
filename string
sep string
out io.WriteCloser
sep string
}
// FileRecorder records data to file.
func FileRecorder(filename string, opts ...FileRecorderOption) recorder.Recorder {
func FileRecorder(out io.WriteCloser, opts ...FileRecorderOption) recorder.Recorder {
var options fileRecorderOptions
for _, opt := range opts {
opt(&options)
}
return &fileRecorder{
filename: filename,
sep: options.sep,
out: out,
sep: options.sep,
}
}
func (r *fileRecorder) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
f, err := os.OpenFile(r.filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
if _, err := r.out.Write(b); err != nil {
return err
}
defer f.Close()
if _, err = f.Write(b); err != nil {
return err
}
if r.sep != "" {
_, err := f.WriteString(r.sep)
_, err := io.WriteString(r.out, r.sep)
return err
}
return nil
}
func (r *fileRecorder) Close() error {
return nil
return r.out.Close()
}