48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
|
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)
|
||
|
}
|