From e64e6eb2a3dc2a4dce3ce85f69a4d801491b21cc Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Tue, 24 Sep 2024 23:24:06 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=BE=D0=B9=20=D1=80=D0=B5=D1=84=D0=B0=D1=82=D0=BA=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=BD=D0=B3=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BC=D1=83=D1=81=D0=BE=D1=80=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BC=D0=BE=D0=BA=D0=B8=20=D0=94=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf.go | 44 +++++++++++++++++++++++------------------ data/test.log | 4 ++++ go.mod | 1 + go.sum | 2 ++ logger.go | 4 +--- logger_test.go | 41 ++++++++++++++++++++++++++++++++++++++ stderrtofile.go | 20 ------------------- stderrtofile_windows.go | 6 ------ 8 files changed, 74 insertions(+), 48 deletions(-) create mode 100644 data/test.log create mode 100644 logger_test.go delete mode 100644 stderrtofile.go delete mode 100644 stderrtofile_windows.go diff --git a/conf.go b/conf.go index 7ef1bb6..608caac 100644 --- a/conf.go +++ b/conf.go @@ -4,23 +4,29 @@ import ( "time" ) -type Configuration struct { - Stdout struct { - Level string `yaml:"level"` - } `yaml:"console"` - File struct { - Enabled bool `yaml:"enabled"` - Level string `yaml:"level"` - Filename string `yaml:"filename"` - MaxSize int `yaml:"maxsize"` - MaxAge int `yaml:"maxage"` - MaxBackups int `yaml:"maxbackups"` - LocalTime bool `yaml:"localtime"` - Compress bool `yaml:"compress"` - } - Sentry struct { - Level string `yaml:"level"` - DSN string `yaml:"dsn"` - Timeout time.Duration `yaml:"timeout"` - } `yaml:"sentry"` +type ConfigurationStdout struct { + Level string `yaml:"level"` +} + +type ConfigurationFile struct { + Enabled bool `yaml:"enabled"` + Level string `yaml:"level"` + Filename string `yaml:"filename"` + MaxSize int `yaml:"maxsize"` + MaxAge int `yaml:"maxage"` + MaxBackups int `yaml:"maxbackups"` + LocalTime bool `yaml:"localtime"` + Compress bool `yaml:"compress"` +} + +type ConfigurationSentry struct { + Level string `yaml:"level"` + DSN string `yaml:"dsn"` + Timeout time.Duration `yaml:"timeout"` +} + +type Configuration struct { + Stdout ConfigurationStdout `yaml:"console"` + File ConfigurationFile `yaml:"file"` + Sentry ConfigurationSentry `yaml:"sentry"` } diff --git a/data/test.log b/data/test.log new file mode 100644 index 0000000..7afa400 --- /dev/null +++ b/data/test.log @@ -0,0 +1,4 @@ +{"level":"debug","ts":"1974-05-19T01:02:03.000000004Z","msg":"Debug message"} +{"level":"info","ts":"1974-05-19T01:02:03.000000004Z","msg":"info message"} +{"level":"warn","ts":"1974-05-19T01:02:03.000000004Z","msg":"Warn message"} +{"level":"error","ts":"1974-05-19T01:02:03.000000004Z","msg":"Error message"} diff --git a/go.mod b/go.mod index 60b1267..e24bd49 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + bou.ke/monkey v1.0.2 // indirect github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/getsentry/raven-go v0.2.0 // indirect diff --git a/go.sum b/go.sum index a0f1bb5..69a96e7 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI= +bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s= github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/logger.go b/logger.go index b29506b..337e731 100644 --- a/logger.go +++ b/logger.go @@ -17,7 +17,7 @@ import ( "gopkg.in/natefinch/lumberjack.v2" ) -var zapLogger *zap.Logger +var zapLogger = zap.New(nil) func Named(name string) *zap.Logger { return zapLogger.Named(name) @@ -89,8 +89,6 @@ func Init(conf *Configuration) { }, ) cores = append(cores, zapcore.NewCore(zapcore.NewJSONEncoder(cfg), writer, fileLevelEnabler)) - - redirectStderrToFile(conf) } // sentry diff --git a/logger_test.go b/logger_test.go new file mode 100644 index 0000000..28db7bd --- /dev/null +++ b/logger_test.go @@ -0,0 +1,41 @@ +package gol + +import ( + "bou.ke/monkey" + "github.com/stretchr/testify/assert" + "os" + "testing" + "time" +) + +func TestLog(t *testing.T) { + now := time.Date(1974, time.May, 19, 1, 2, 3, 4, time.UTC) + patch := monkey.Patch(time.Now, func() time.Time { return now }) + defer patch.Unpatch() + + t.Run("TestNilLog", func(t *testing.T) { + Info("info message") + //assert.Equal(t, test.expectedFileName, buildErrorsFilename(test.logFileName), "result") + }) + + t.Run("TestFileLog", func(t *testing.T) { + configPath := "test.log" + Init(&Configuration{ + File: ConfigurationFile{ + Enabled: true, + Level: "debug", + Filename: configPath, + }, + }) + + Debug("Debug message") + Info("info message") + Warn("Warn message") + Error("Error message") + + expectedFile, _ := os.ReadFile("data/test.log") + actualFile, _ := os.ReadFile("test.log") + assert.Equal(t, string(expectedFile), string(actualFile), "result") + os.Remove(configPath) + }) +} diff --git a/stderrtofile.go b/stderrtofile.go deleted file mode 100644 index b9545fc..0000000 --- a/stderrtofile.go +++ /dev/null @@ -1,20 +0,0 @@ -//go:build !windows - -package gol - -import ( - "log" - "os" - "syscall" -) - -func redirectStderrToFile(conf *Configuration) { - fileName := buildErrorsFilename(conf.File.Filename) - f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - log.Fatalf("open %s: %s", fileName, err) - } - if err = syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd())); err != nil { - log.Fatalf("Failed to redirect stderr to file: %s", err) - } -} diff --git a/stderrtofile_windows.go b/stderrtofile_windows.go deleted file mode 100644 index 34f750f..0000000 --- a/stderrtofile_windows.go +++ /dev/null @@ -1,6 +0,0 @@ -//go:build windows - -package gol - -func redirectStderrToFile(conf *Configuration) { -}