feat recorder: add rotate options
This commit is contained in:
+3
-2
@@ -278,8 +278,9 @@ type RecorderConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type FileRecorder struct {
|
type FileRecorder struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Sep string `yaml:",omitempty" json:"sep,omitempty"`
|
Sep string `yaml:",omitempty" json:"sep,omitempty"`
|
||||||
|
Rotation *LogRotationConfig `yaml:",omitempty" json:"rotation,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TCPRecorder struct {
|
type TCPRecorder struct {
|
||||||
|
|||||||
@@ -2,16 +2,26 @@ package recorder
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-gost/core/logger"
|
||||||
"github.com/go-gost/core/recorder"
|
"github.com/go-gost/core/recorder"
|
||||||
"github.com/go-gost/x/config"
|
"github.com/go-gost/x/config"
|
||||||
"github.com/go-gost/x/internal/plugin"
|
"github.com/go-gost/x/internal/plugin"
|
||||||
xrecorder "github.com/go-gost/x/recorder"
|
xrecorder "github.com/go-gost/x/recorder"
|
||||||
recorder_plugin "github.com/go-gost/x/recorder/plugin"
|
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) {
|
func ParseRecorder(cfg *config.RecorderConfig) (r recorder.Recorder) {
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -42,9 +52,28 @@ func ParseRecorder(cfg *config.RecorderConfig) (r recorder.Recorder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.File != nil && cfg.File.Path != "" {
|
if cfg.File != nil && cfg.File.Path != "" {
|
||||||
return xrecorder.FileRecorder(cfg.File.Path,
|
var out io.WriteCloser = discardCloser{}
|
||||||
xrecorder.SepRecorderOption(cfg.File.Sep),
|
|
||||||
)
|
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 != "" {
|
if cfg.TCP != nil && cfg.TCP.Addr != "" {
|
||||||
|
|||||||
+10
-14
@@ -2,7 +2,7 @@ package recorder
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"io"
|
||||||
|
|
||||||
"github.com/go-gost/core/recorder"
|
"github.com/go-gost/core/recorder"
|
||||||
)
|
)
|
||||||
@@ -20,40 +20,36 @@ func SepRecorderOption(sep string) FileRecorderOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type fileRecorder struct {
|
type fileRecorder struct {
|
||||||
filename string
|
out io.WriteCloser
|
||||||
sep string
|
sep string
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileRecorder records data to file.
|
// 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
|
var options fileRecorderOptions
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(&options)
|
opt(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &fileRecorder{
|
return &fileRecorder{
|
||||||
filename: filename,
|
out: out,
|
||||||
sep: options.sep,
|
sep: options.sep,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *fileRecorder) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
|
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 := r.out.Write(b); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
if _, err = f.Write(b); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if r.sep != "" {
|
if r.sep != "" {
|
||||||
_, err := f.WriteString(r.sep)
|
_, err := io.WriteString(r.out, r.sep)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *fileRecorder) Close() error {
|
func (r *fileRecorder) Close() error {
|
||||||
return nil
|
return r.out.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user