forked from go-clog/clog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclog.go
148 lines (125 loc) · 2.99 KB
/
clog.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
// Copyright 2017 Unknwon
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// Clog is a channel-based logging package for Go.
package clog
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
const (
_VERSION = "1.2.0"
)
// Version returns current version of the package.
func Version() string {
return _VERSION
}
type (
MODE string
LEVEL int
)
const (
CONSOLE MODE = "console"
FILE MODE = "file"
SLACK MODE = "slack"
DISCORD MODE = "discord"
)
const (
TRACE LEVEL = iota
INFO
WARN
ERROR
FATAL
)
var formats = map[LEVEL]string{
TRACE: "[TRACE] ",
INFO: "[ INFO] ",
WARN: "[ WARN] ",
ERROR: "[ERROR] ",
FATAL: "[FATAL] ",
}
// isValidLevel returns true if given level is in the valid range.
func isValidLevel(level LEVEL) bool {
return level >= TRACE && level <= FATAL
}
// Message represents a log message to be processed.
type Message struct {
Level LEVEL
Body string
}
func Write(level LEVEL, skip int, format string, v ...interface{}) {
msg := &Message{
Level: level,
}
// Only error and fatal information needs locate position for debugging.
// But if skip is 0 means caller doesn't care so we can skip.
if msg.Level >= ERROR && skip > 0 {
pc, file, line, ok := runtime.Caller(skip)
if ok {
// Get caller function name.
fn := runtime.FuncForPC(pc)
var fnName string
if fn == nil {
fnName = "?()"
} else {
fnName = strings.TrimLeft(filepath.Ext(fn.Name()), ".") + "()"
}
if len(file) > 20 {
file = "..." + file[len(file)-20:]
}
msg.Body = formats[level] + fmt.Sprintf("[%s:%d %s] ", file, line, fnName) + fmt.Sprintf(format, v...)
}
}
if len(msg.Body) == 0 {
msg.Body = formats[level] + fmt.Sprintf(format, v...)
}
for i := range receivers {
if receivers[i].Level() > level {
continue
}
receivers[i].msgChan <- msg
}
}
func Trace(format string, v ...interface{}) {
Write(TRACE, 0, format, v...)
}
func Info(format string, v ...interface{}) {
Write(INFO, 0, format, v...)
}
func Warn(format string, v ...interface{}) {
Write(WARN, 0, format, v...)
}
func Error(skip int, format string, v ...interface{}) {
Write(ERROR, skip, format, v...)
}
func Fatal(skip int, format string, v ...interface{}) {
Write(FATAL, skip, format, v...)
Shutdown()
os.Exit(1)
}
func Shutdown() {
for i := range receivers {
receivers[i].Destroy()
}
// Shutdown the error handling goroutine.
quitChan <- struct{}{}
for {
if len(errorChan) == 0 {
break
}
fmt.Printf("clog: unable to write message: %v\n", <-errorChan)
}
}