diff --git a/internal/net/gphttp/accesslog/access_logger_test.go b/internal/net/gphttp/accesslog/access_logger_test.go index cb8f468..fabd41a 100644 --- a/internal/net/gphttp/accesslog/access_logger_test.go +++ b/internal/net/gphttp/accesslog/access_logger_test.go @@ -10,6 +10,7 @@ import ( . "github.com/yusing/go-proxy/internal/net/gphttp/accesslog" "github.com/yusing/go-proxy/internal/task" + "github.com/yusing/go-proxy/internal/utils" expect "github.com/yusing/go-proxy/internal/utils/testing" ) @@ -56,7 +57,7 @@ func fmtLog(cfg *Config) (ts string, line string) { t := time.Now() logger := NewMockAccessLogger(testTask, cfg) - MockTimeNow(t) + utils.MockTimeNow(t) buf = logger.AppendLog(buf, req, resp) return t.Format(LogTimeFormat), string(buf) } diff --git a/internal/net/gphttp/accesslog/formatter.go b/internal/net/gphttp/accesslog/formatter.go index 33e5c5b..4d95b91 100644 --- a/internal/net/gphttp/accesslog/formatter.go +++ b/internal/net/gphttp/accesslog/formatter.go @@ -8,6 +8,7 @@ import ( "strconv" "github.com/rs/zerolog" + "github.com/yusing/go-proxy/internal/utils" ) type ( @@ -64,7 +65,7 @@ func (f *CommonFormatter) AppendLog(line []byte, req *http.Request, res *http.Re line = append(line, clientIP(req)...) line = append(line, " - - ["...) - line = TimeNow().AppendFormat(line, LogTimeFormat) + line = utils.TimeNow().AppendFormat(line, LogTimeFormat) line = append(line, `] "`...) line = append(line, req.Method...) @@ -126,7 +127,7 @@ func (f *JSONFormatter) AppendLog(line []byte, req *http.Request, res *http.Resp writer := bytes.NewBuffer(line) logger := zerolog.New(writer).With().Logger() event := logger.Info(). - Str("time", TimeNow().Format(LogTimeFormat)). + Str("time", utils.TimeNow().Format(LogTimeFormat)). Str("ip", clientIP(req)). Str("method", req.Method). Str("scheme", scheme(req)). diff --git a/internal/net/gphttp/accesslog/rotate.go b/internal/net/gphttp/accesslog/rotate.go index 4578aaf..469038f 100644 --- a/internal/net/gphttp/accesslog/rotate.go +++ b/internal/net/gphttp/accesslog/rotate.go @@ -6,6 +6,7 @@ import ( "time" "github.com/rs/zerolog" + "github.com/yusing/go-proxy/internal/utils" "github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/utils/synk" ) @@ -63,13 +64,13 @@ func rotateLogFile(file supportRotate, config *Retention) (result *RotateResult, } var shouldStop func() bool - t := TimeNow() + t := utils.TimeNow() if config.Last > 0 { shouldStop = func() bool { return result.NumLinesKeep-result.NumLinesInvalid == int(config.Last) } // not needed to parse time for last N lines } else if config.Days > 0 { - cutoff := TimeNow().AddDate(0, 0, -int(config.Days)+1) + cutoff := utils.TimeNow().AddDate(0, 0, -int(config.Days)+1) shouldStop = func() bool { return t.Before(cutoff) } } else { return nil, nil // should not happen diff --git a/internal/net/gphttp/accesslog/rotate_test.go b/internal/net/gphttp/accesslog/rotate_test.go index 5b96310..a770331 100644 --- a/internal/net/gphttp/accesslog/rotate_test.go +++ b/internal/net/gphttp/accesslog/rotate_test.go @@ -8,6 +8,7 @@ import ( . "github.com/yusing/go-proxy/internal/net/gphttp/accesslog" "github.com/yusing/go-proxy/internal/task" + "github.com/yusing/go-proxy/internal/utils" "github.com/yusing/go-proxy/internal/utils/strutils" expect "github.com/yusing/go-proxy/internal/utils/testing" ) @@ -55,7 +56,7 @@ func TestRotateKeepLast(t *testing.T) { for _, format := range AvailableFormats { t.Run(string(format)+" keep last", func(t *testing.T) { file := NewMockFile() - MockTimeNow(testTime) + utils.MockTimeNow(testTime) logger := NewAccessLoggerWithIO(task.RootTask("test", false), file, &Config{ Format: format, }) @@ -90,7 +91,7 @@ func TestRotateKeepLast(t *testing.T) { expect.Nil(t, logger.Config().Retention) nLines := 10 for i := range nLines { - MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1)) + utils.MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1)) logger.Log(req, resp) } logger.Flush() @@ -102,7 +103,7 @@ func TestRotateKeepLast(t *testing.T) { expect.Equal(t, retention.KeepSize, 0) logger.Config().Retention = retention - MockTimeNow(testTime) + utils.MockTimeNow(testTime) result, err := logger.Rotate() expect.NoError(t, err) expect.Equal(t, file.NumLines(), int(retention.Days)) @@ -135,7 +136,7 @@ func TestRotateKeepFileSize(t *testing.T) { expect.Nil(t, logger.Config().Retention) nLines := 10 for i := range nLines { - MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1)) + utils.MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1)) logger.Log(req, resp) } logger.Flush() @@ -147,7 +148,7 @@ func TestRotateKeepFileSize(t *testing.T) { expect.Equal(t, retention.Last, 0) logger.Config().Retention = retention - MockTimeNow(testTime) + utils.MockTimeNow(testTime) result, err := logger.Rotate() expect.NoError(t, err) @@ -165,7 +166,7 @@ func TestRotateKeepFileSize(t *testing.T) { expect.Nil(t, logger.Config().Retention) nLines := 100 for i := range nLines { - MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1)) + utils.MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1)) logger.Log(req, resp) } logger.Flush() @@ -177,7 +178,7 @@ func TestRotateKeepFileSize(t *testing.T) { expect.Equal(t, retention.Last, 0) logger.Config().Retention = retention - MockTimeNow(testTime) + utils.MockTimeNow(testTime) result, err := logger.Rotate() expect.NoError(t, err) expect.Equal(t, result.NumBytesKeep, int64(retention.KeepSize)) @@ -197,7 +198,7 @@ func TestRotateSkipInvalidTime(t *testing.T) { expect.Nil(t, logger.Config().Retention) nLines := 10 for i := range nLines { - MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1)) + utils.MockTimeNow(testTime.AddDate(0, 0, -nLines+i+1)) logger.Log(req, resp) logger.Flush() @@ -236,7 +237,7 @@ func BenchmarkRotate(b *testing.B) { Retention: retention, }) for i := range 100 { - MockTimeNow(testTime.AddDate(0, 0, -100+i+1)) + utils.MockTimeNow(testTime.AddDate(0, 0, -100+i+1)) logger.Log(req, resp) } logger.Flush() @@ -267,7 +268,7 @@ func BenchmarkRotateWithInvalidTime(b *testing.B) { Retention: retention, }) for i := range 10000 { - MockTimeNow(testTime.AddDate(0, 0, -10000+i+1)) + utils.MockTimeNow(testTime.AddDate(0, 0, -10000+i+1)) logger.Log(req, resp) if i%10 == 0 { _, _ = file.Write([]byte("invalid time\n")) diff --git a/internal/net/gphttp/accesslog/time_now.go b/internal/utils/time_now.go similarity index 98% rename from internal/net/gphttp/accesslog/time_now.go rename to internal/utils/time_now.go index 554c4c8..8b5e155 100644 --- a/internal/net/gphttp/accesslog/time_now.go +++ b/internal/utils/time_now.go @@ -1,4 +1,4 @@ -package accesslog +package utils import ( "time" diff --git a/internal/net/gphttp/accesslog/time_now_test.go b/internal/utils/time_now_test.go similarity index 99% rename from internal/net/gphttp/accesslog/time_now_test.go rename to internal/utils/time_now_test.go index ccc7eb8..f2ddf54 100644 --- a/internal/net/gphttp/accesslog/time_now_test.go +++ b/internal/utils/time_now_test.go @@ -1,4 +1,4 @@ -package accesslog +package utils import ( "testing"