-
Notifications
You must be signed in to change notification settings - Fork 100
/
ossutil_log.go
93 lines (79 loc) · 1.67 KB
/
ossutil_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
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
package lib
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
var logName = "ossutil.log"
var logLevel = oss.LogOff
var utilLogger *log.Logger
var logFile *os.File
func openLogFile() (*os.File, error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
dir = "."
}
absLogName := dir + string(os.PathSeparator) + logName
f, err := os.OpenFile(absLogName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0660)
if err != nil {
fmt.Printf("open %s error,info:%s.\n", absLogName, err.Error())
} else {
fmt.Printf("log file is %s\n", absLogName)
}
return f, err
}
func InitLogger(level int, name string) {
logLevel = level
logName = name
f, err := openLogFile()
if err != nil {
return
}
utilLogger = log.New(f, "", log.LstdFlags)
logFile = f
}
func UnInitLogger() {
if logFile == nil {
return
}
logFile.Close()
logFile = nil
utilLogger = nil
}
func writeLog(level int, format string, a ...interface{}) {
if utilLogger == nil {
return
}
var logBuffer bytes.Buffer
logBuffer.WriteString(oss.LogTag[level-1])
logBuffer.WriteString(fmt.Sprintf(format, a...))
utilLogger.Printf("%s", logBuffer.String())
return
}
func LogError(format string, a ...interface{}) {
if logLevel < oss.Error {
return
}
writeLog(oss.Error, format, a...)
}
func LogWarn(format string, a ...interface{}) {
if logLevel < oss.Warn {
return
}
writeLog(oss.Warn, format, a...)
}
func LogInfo(format string, a ...interface{}) {
if logLevel < oss.Info {
return
}
writeLog(oss.Info, format, a...)
}
func LogDebug(format string, a ...interface{}) {
if logLevel < oss.Debug {
return
}
writeLog(oss.Debug, format, a...)
}