-
Notifications
You must be signed in to change notification settings - Fork 36
/
savelog.go
52 lines (45 loc) · 955 Bytes
/
savelog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package savelog
import (
"encoding/json"
"fmt"
"log"
"os"
)
/**
自定义写日志
author Bill
*/
var sufferFix = ".log"
func WriteLog(dir string, logFileName string, logData map[string]interface{}) {
var logFilePath = dir + "/" + logFileName + sufferFix
var Mkdir = dir
if IsExist(Mkdir) == false {
os.Mkdir(Mkdir, 0775)
}
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0775)
if err != nil {
fmt.Println(err.Error())
}
defer logFile.Close()
if err != nil {
fmt.Println(err.Error())
}
Log := log.New(logFile, "", log.LstdFlags)
Msg := fmt.Sprintf(" %s ", _toJsonString(logData))
Log.Println(Msg)
}
/**
JSON (map转json)
@author Bill
*/
func _toJsonString(data map[string]interface{}) string {
jsonData, err := json.Marshal(data)
if err != nil {
return ""
}
return string(jsonData)
}
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}