mq/app/utils/util.go

36 lines
953 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"os"
"runtime"
"strconv"
"time"
)
func Log(c *gin.Context, name string, msg ...interface{}) {
_, file, line, _ := runtime.Caller(1)
var datetime = time.Now().Format(time.DateTime)
fmt.Println(name, msg, file, line, datetime)
}
func LogFile(c *gin.Context, name string, msg ...interface{}) {
_, rfile, line, _ := runtime.Caller(1)
var datetime = time.Now().Format(time.DateTime)
file, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
var data, _ = json.Marshal(map[string]interface{}{"data": msg})
// 写入内容到文件末尾
_, err = file.WriteString("file:" + rfile + " line:" + strconv.Itoa(line) + " 时间:" + datetime + "" + name + ":" + string(data) + "!\n")
if err != nil {
fmt.Println("Error writing to file:", err)
return
}
}