Commit 994aa870 by Joneq

修改文件类型

parent 1f7a8e7d
Showing with 67 additions and 3 deletions
package main
import "fmt"
import (
"kaopu-server/internal/logic"
)
func main() {
fmt.Print(123)
logic.LogEs("123113131")
logic.LogEs("4567889")
logic.LogEs("09876")
}
......
package logic
import (
"encoding/json"
"fmt"
"io"
"os"
"time"
)
/*
{
"msg": msg, 错误信息 字符串
"msgCode": msg_code, 自定义错误码 六位数字字符串 etc "100000"
"ts": ts, 10位整形 时间戳
"dateStr": "", # 日期 2018-06-28 21:24:09 字符串
"app": "", 应用名称 字符串
"serverIp": "", 服务器ip 字符串
"fileName": "", 文件名 字符串
"lineNo": "", 行数 整形数字
"method": "", 函数名 字符串
}
*/
type EsLogJson struct {
Msg string `json:"msg"`
Email string `json:"msgCode"`
Ts int64 `json:"ts"`
DateStr string `json:"dateStr"`
App string `json:"app"`
ServerIp string `json:"serverIp"`
FileName string `json:"fileName"`
LineNo int64 `json:"lineNo"`
Method string `json:"method"`
}
func check(e error) {
if e != nil {
panic(e)
......@@ -55,9 +81,42 @@ func LogWrite(log1 string,log_pre string,is_write int) {
Loginfo(log1,log_pre)
}
func LogEs(content string) {
var esLog EsLogJson
date := time.Now().Format("2006-01-02")
dateTime := time.Now().Format("2006-01-02 15:04:05")
strTotime := time.Now().Unix()
filename := "./logs/" + date + "err.log"
esLog.App = "kaopu-server"
esLog.DateStr = dateTime
esLog.Email = ""
esLog.FileName = filename
esLog.LineNo = 1
esLog.ServerIp = "172.18.137.41"
esLog.Method = "easy"
esLog.Ts = strTotime
esLog.Msg = content
var f *os.File
if !checkFileIsExist(filename) { //如果文件存在
f,_= os.Create(filename) //创建文件
}
f,_= os.OpenFile(filename, os.O_WRONLY|os.O_APPEND, 0666)
writeContent,_ := json.Marshal(esLog)
f.Write([]byte("\n" + string(writeContent)))
defer f.Close()
}
func CheckErr(err error) {
if err != nil {
Loginfo(err.Error(),"error")
LogEs(err.Error())
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment