forked from xelabs/go-mysqlstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxlog.go
More file actions
174 lines (151 loc) · 3.06 KB
/
Copy pathxlog.go
File metadata and controls
174 lines (151 loc) · 3.06 KB
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
/*
* go-mysqlstack
* xelabs.org
*
* Copyright (c) XeLabs
* GPL License
*
*/
package xlog
import (
"fmt"
"io"
"log"
"log/syslog"
"os"
"strings"
)
var (
defaultlog *Log
)
// LogLevel used for log level.
type LogLevel int
const (
// DEBUG enum.
DEBUG LogLevel = 1 << iota
// INFO enum.
INFO
// WARNING enum.
WARNING
// ERROR enum.
ERROR
// FATAL enum.
FATAL
// PANIC enum.
PANIC
)
// LevelNames represents the string name of all levels.
var LevelNames = [...]string{
DEBUG: "DEBUG",
INFO: "INFO",
WARNING: "WARNING",
ERROR: "ERROR",
FATAL: "FATAL",
PANIC: "PANIC",
}
const (
// D_LOG_FLAGS is the default log flags.
D_LOG_FLAGS int = log.LstdFlags | log.Lmicroseconds | log.Lshortfile
)
// Log struct.
type Log struct {
opts *Options
*log.Logger
}
// NewSysLog creates a new sys log.
func NewSysLog(opts ...Option) *Log {
w, err := syslog.New(syslog.LOG_DEBUG, "")
if err != nil {
panic(err)
}
return NewXLog(w, opts...)
}
// NewStdLog creates a new std log.
func NewStdLog(opts ...Option) *Log {
return NewXLog(os.Stdout, opts...)
}
// NewXLog creates a new xlog.
func NewXLog(w io.Writer, opts ...Option) *Log {
options := newOptions(opts...)
l := &Log{
opts: options,
}
l.Logger = log.New(w, l.opts.Name, D_LOG_FLAGS)
defaultlog = l
return l
}
// NewLog creates the new log.
func NewLog(w io.Writer, prefix string, flag int) *Log {
l := &Log{}
l.Logger = log.New(w, prefix, flag)
return l
}
// GetLog returns Log.
func GetLog() *Log {
if defaultlog == nil {
log := NewStdLog(Level(INFO))
defaultlog = log
}
return defaultlog
}
// SetLevel used to set the log level.
func (t *Log) SetLevel(level string) {
for i, v := range LevelNames {
if level == v {
t.opts.Level = LogLevel(i)
return
}
}
}
// Debug used to log debug msg.
func (t *Log) Debug(format string, v ...interface{}) {
if DEBUG < t.opts.Level {
return
}
t.log("\t [DEBUG] \t%s", fmt.Sprintf(format, v...))
}
// Info used to log info msg.
func (t *Log) Info(format string, v ...interface{}) {
if INFO < t.opts.Level {
return
}
t.log("\t [INFO] \t%s", fmt.Sprintf(format, v...))
}
// Warning used to log warning msg.
func (t *Log) Warning(format string, v ...interface{}) {
if WARNING < t.opts.Level {
return
}
t.log("\t [WARNING] \t%s", fmt.Sprintf(format, v...))
}
// Error used to log error msg.
func (t *Log) Error(format string, v ...interface{}) {
if ERROR < t.opts.Level {
return
}
t.log("\t [ERROR] \t%s", fmt.Sprintf(format, v...))
}
// Fatal used to log faltal msg.
func (t *Log) Fatal(format string, v ...interface{}) {
if FATAL < t.opts.Level {
return
}
t.log("\t [FATAL+EXIT] \t%s", fmt.Sprintf(format, v...))
os.Exit(1)
}
// Panic used to log panic msg.
func (t *Log) Panic(format string, v ...interface{}) {
if PANIC < t.opts.Level {
return
}
msg := fmt.Sprintf("\t [PANIC] \t%s", fmt.Sprintf(format, v...))
t.log(msg)
panic(msg)
}
// Close used to close the log.
func (t *Log) Close() {
// nothing
}
func (t *Log) log(format string, v ...interface{}) {
t.Output(3, strings.Repeat(" ", 3)+fmt.Sprintf(format, v...)+"\n")
}