feat recorder: add rotate options
This commit is contained in:
+3
-2
@@ -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 {
|
||||
|
||||
@@ -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 != "" {
|
||||
|
||||
Reference in New Issue
Block a user