/
Zloger.go
185 lines (146 loc) · 3.97 KB
/
Zloger.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***
日志类
path:c:/sdfdsf
filename:test.text
wireteString :"sdfsf"
logers := zloger.NewLog(path)
logers.DebugLog(filename,wireteString)
*/
package Zloger
import (
"os"
"fmt"
"io"
"time"
"sync"
)
type Loger struct{
path string
}
// 同步锁
var chInfoLock sync.Mutex
var chCompleteLock sync.Mutex
var chErrorLock sync.Mutex
func NewLog(path string) *Loger {
//创建日志目录
err := os.MkdirAll(path, 0777)
if err != nil {
fmt.Printf("crete fail %s", err)
os.Exit(1)
} else {
// fmt.Print("Create Directory OK!")
}
//返回日志对象
Log := &Loger{
path : path+"/",
}
return Log
}
func (a *Loger) DebugLog(wireteString string){
var f *os.File
var err error
var filename = "debug"+getCurDate()+".log"
if checkFileIsExist(a.path+filename) { //如果文件存在
f, err = os.OpenFile(a.path+filename, os.O_APPEND,0) //打开文件
//fmt.Println("文件存在");
}else {
f, err = os.Create(a.path+filename) //创建文件
//fmt.Println("文件不存在");
}
check(err)
defer f.Close()
wireteString = wireteString+"\r\n"
_,err = io.WriteString(f, time.Now().Format("2006-01-02 15:04:05") +" "+ wireteString) //写入文件(字符串)
check(err)
//格式化用的日期是特定的,123(15)45 -.-lll
//fmt.Printf("写入 %d 个字节", n);
return
}
func (a *Loger) CompleteLog(wireteString string){
chCompleteLock.Lock()
defer chCompleteLock.Unlock()
var f *os.File
var err error
var filename = "complete_"+getCurDate()+".log"
if checkFileIsExist(a.path+filename) { //如果文件存在
f, err = os.OpenFile(a.path+filename, os.O_APPEND|os.O_WRONLY, os.ModeAppend) //打开文件
//fmt.Println("文件存在");
}else {
f, err = os.Create(a.path+filename) //创建文件
//fmt.Println("文件不存在");
}
check(err)
defer f.Close()
wireteString = wireteString+"\r\n"
_,err = io.WriteString(f, wireteString) //写入文件(字符串)
check(err)
//格式化用的日期是特定的,123(15)45 -.-lll
now_time := time.Now().Format("2006-01-02 15:04:05")
io.WriteString(f, now_time+"\r\n") //写入文件(字符串)
//fmt.Printf("写入 %d 个字节", n);
return
}
func (a *Loger) InfoLog(wireteString string,extfilename string){
var f *os.File
var err error
var filename = "info_"+extfilename + getCurDate()+".log"
if checkFileIsExist(a.path+filename) { //如果文件存在
f, err = os.OpenFile(a.path+filename, os.O_APPEND|os.O_WRONLY, os.ModeAppend) //打开文件
//fmt.Println("文件存在");
}else {
f, err = os.Create(a.path+filename) //创建文件
//fmt.Println("文件不存在");
}
check(err)
defer f.Close()
wireteString = wireteString+"\r\n"
_,err = io.WriteString(f, time.Now().Format("2006-01-02 15:04:05") +" "+wireteString) //写入文件(字符串)
check(err)
//格式化用的日期是特定的,123(15)45 -.-lll
//fmt.Printf("写入 %d 个字节", n);
return
}
func (a *Loger) ErrorLog(wireteString string){
chErrorLock.Lock()
defer chErrorLock.Unlock()
var f *os.File
var err error
var filename = "error_"+getCurDate()+".log"
if checkFileIsExist(a.path+filename) { //如果文件存在
f, err = os.OpenFile(a.path+filename, os.O_APPEND|os.O_WRONLY, os.ModeAppend) //打开文件
//fmt.Println("文件存在");
}else {
f, err = os.Create(a.path+filename) //创建文件
//fmt.Println("文件不存在");
}
check(err)
defer f.Close()
wireteString = wireteString+"\r\n"
_,err = io.WriteString(f, time.Now().Format("2006-01-02 15:04:05") +" "+wireteString) //写入文件(字符串)
check(err)
//fmt.Printf("写入 %d 个字节", n);
return
}
func check(e error) {
if e != nil {
panic(e)
}
}
/**
* 判断文件是否存在 存在返回 true 不存在返回false
*/
func checkFileIsExist(path string) (bool) {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
fmt.Println(err)
return false
}
//2006-1-2
func getCurDate() string {
return time.Now().Format("2006-1-2")
}