-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
43 lines (39 loc) · 762 Bytes
/
log.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
package conch
import (
"fmt"
"os"
"time"
)
type Log struct {
Path string
}
func (log Log)Out(format string, arg ...interface{}) {
result := time.Now().Format("[2006-01-02 15:04:05] ")
result += fmt.Sprintf(format + "\n", arg...)
fmt.Print(result)
if len(log.Path) > 0 {
if !Exists(log.Path) {
file, err := os.Create(log.Path)
defer file.Close()
if err != nil {
fmt.Println("Create log file error.")
}
}
file, err := os.OpenFile(log.Path, os.O_APPEND|os.O_WRONLY, 0666)
defer file.Close()
if err != nil {
fmt.Println("Log error.")
}
_, _ = file.WriteString(result)
}
}
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}