init
This commit is contained in:
47
utils/utils.go
Normal file
47
utils/utils.go
Normal file
@ -0,0 +1,47 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// Получения файла лога
|
||||
func GetLogFile(logDir, fileName string) *os.File {
|
||||
if _, err := os.Stat(logDir); os.IsNotExist(err) {
|
||||
err := os.Mkdir(logDir, 0777)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
logFile, err := os.OpenFile(fmt.Sprintf("./%s/%s-%s.log", logDir, fileName, time.Now().Format("02-01-2006")), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("Ошибка открытия файла логов %v", err)
|
||||
}
|
||||
return logFile
|
||||
}
|
||||
|
||||
// Отправка сообщения
|
||||
func SendMessage(uri string, message []byte) error {
|
||||
req := fasthttp.AcquireRequest()
|
||||
res := fasthttp.AcquireResponse()
|
||||
defer fasthttp.ReleaseRequest(req)
|
||||
defer fasthttp.ReleaseResponse(res)
|
||||
req.SetRequestURI(uri)
|
||||
req.Header.SetMethod("POST")
|
||||
req.Header.SetContentType("application/json")
|
||||
req.SetBody(message)
|
||||
return fasthttp.Do(req, res)
|
||||
}
|
||||
|
||||
// Инициализация лога
|
||||
func SetupLog(name string) *log.Logger {
|
||||
s := []color.Attribute{color.FgBlue, color.FgHiMagenta, color.FgHiGreen, color.FgYellow}
|
||||
prefix := color.New(s[rand.Intn(len(s))])
|
||||
return log.New(nil, prefix.Sprintf(" [%s] ", name), log.LstdFlags)
|
||||
}
|
52
utils/utils_test.go
Normal file
52
utils/utils_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type (
|
||||
LogTestStruct struct {
|
||||
System string `json:"system"`
|
||||
Message string `json:"message"`
|
||||
Error TypeError `json:"error"`
|
||||
}
|
||||
TypeError struct {
|
||||
Code int `json:"code"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
)
|
||||
|
||||
func TestGetLogFile(t *testing.T) {
|
||||
s := GetLogFile("test_logs", "test")
|
||||
if s == nil {
|
||||
t.Error("log file is nil")
|
||||
}
|
||||
if err := os.RemoveAll("test_logs"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendMessage(t *testing.T) {
|
||||
m := &LogTestStruct{
|
||||
System: "go test",
|
||||
Message: "CI TEST",
|
||||
Error: TypeError{Code: -112, Description: "Тестирование"},
|
||||
}
|
||||
// Маршим в байтики
|
||||
b, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := SendMessage("https://devtest.galamart.ru/bus/pub?topic=error&channel=error", b); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupLog(t *testing.T) {
|
||||
l := SetupLog("test")
|
||||
if l.Prefix() != " [test] " {
|
||||
t.Error(l.Prefix())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user