Небольшой рефаткоринг

Удалил мусор
Добавил моки
Добавил тест
This commit is contained in:
Rinsvent 2024-09-24 23:24:06 +07:00
parent 1489c64a01
commit e64e6eb2a3
8 changed files with 74 additions and 48 deletions

44
conf.go
View File

@ -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"`
}

4
data/test.log Normal file
View File

@ -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"}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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

41
logger_test.go Normal file
View File

@ -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)
})
}

View File

@ -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)
}
}

View File

@ -1,6 +0,0 @@
//go:build windows
package gol
func redirectStderrToFile(conf *Configuration) {
}