Небольшой рефаткоринг
Удалил мусор Добавил моки Добавил тест
This commit is contained in:
parent
1489c64a01
commit
e64e6eb2a3
44
conf.go
44
conf.go
@ -4,23 +4,29 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Configuration struct {
|
type ConfigurationStdout struct {
|
||||||
Stdout struct {
|
Level string `yaml:"level"`
|
||||||
Level string `yaml:"level"`
|
}
|
||||||
} `yaml:"console"`
|
|
||||||
File struct {
|
type ConfigurationFile struct {
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
Level string `yaml:"level"`
|
Level string `yaml:"level"`
|
||||||
Filename string `yaml:"filename"`
|
Filename string `yaml:"filename"`
|
||||||
MaxSize int `yaml:"maxsize"`
|
MaxSize int `yaml:"maxsize"`
|
||||||
MaxAge int `yaml:"maxage"`
|
MaxAge int `yaml:"maxage"`
|
||||||
MaxBackups int `yaml:"maxbackups"`
|
MaxBackups int `yaml:"maxbackups"`
|
||||||
LocalTime bool `yaml:"localtime"`
|
LocalTime bool `yaml:"localtime"`
|
||||||
Compress bool `yaml:"compress"`
|
Compress bool `yaml:"compress"`
|
||||||
}
|
}
|
||||||
Sentry struct {
|
|
||||||
Level string `yaml:"level"`
|
type ConfigurationSentry struct {
|
||||||
DSN string `yaml:"dsn"`
|
Level string `yaml:"level"`
|
||||||
Timeout time.Duration `yaml:"timeout"`
|
DSN string `yaml:"dsn"`
|
||||||
} `yaml:"sentry"`
|
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
4
data/test.log
Normal 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
1
go.mod
@ -8,6 +8,7 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
bou.ke/monkey v1.0.2 // indirect
|
||||||
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
|
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/getsentry/raven-go v0.2.0 // indirect
|
github.com/getsentry/raven-go v0.2.0 // indirect
|
||||||
|
2
go.sum
2
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 h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s=
|
||||||
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
|
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=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
"gopkg.in/natefinch/lumberjack.v2"
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var zapLogger *zap.Logger
|
var zapLogger = zap.New(nil)
|
||||||
|
|
||||||
func Named(name string) *zap.Logger {
|
func Named(name string) *zap.Logger {
|
||||||
return zapLogger.Named(name)
|
return zapLogger.Named(name)
|
||||||
@ -89,8 +89,6 @@ func Init(conf *Configuration) {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
cores = append(cores, zapcore.NewCore(zapcore.NewJSONEncoder(cfg), writer, fileLevelEnabler))
|
cores = append(cores, zapcore.NewCore(zapcore.NewJSONEncoder(cfg), writer, fileLevelEnabler))
|
||||||
|
|
||||||
redirectStderrToFile(conf)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sentry
|
// sentry
|
||||||
|
41
logger_test.go
Normal file
41
logger_test.go
Normal 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)
|
||||||
|
})
|
||||||
|
}
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
//go:build windows
|
|
||||||
|
|
||||||
package gol
|
|
||||||
|
|
||||||
func redirectStderrToFile(conf *Configuration) {
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user