153 lines
3.4 KiB
Go
153 lines
3.4 KiB
Go
|
package laniakea
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"runtime"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"github.com/fatih/color"
|
||
|
)
|
||
|
|
||
|
type Logger struct {
|
||
|
prefix string
|
||
|
level LogLevel
|
||
|
printTraceback bool
|
||
|
printTime bool
|
||
|
}
|
||
|
|
||
|
type LogLevel struct {
|
||
|
n uint8
|
||
|
t string
|
||
|
c color.Attribute
|
||
|
}
|
||
|
|
||
|
type MethodTraceback struct {
|
||
|
Package string
|
||
|
Method string
|
||
|
fullPath string
|
||
|
signature string
|
||
|
filename string
|
||
|
line int
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
INFO LogLevel = LogLevel{n: 0, t: "info", c: color.FgWhite}
|
||
|
WARN LogLevel = LogLevel{n: 1, t: "warn", c: color.FgHiYellow}
|
||
|
ERROR LogLevel = LogLevel{n: 2, t: "error", c: color.FgHiRed}
|
||
|
FATAL LogLevel = LogLevel{n: 3, t: "fatal", c: color.FgRed}
|
||
|
DEBUG LogLevel = LogLevel{n: 4, t: "debug", c: color.FgGreen}
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
GET LogLevel = LogLevel{n: 0, t: "get", c: color.FgWhite}
|
||
|
POST LogLevel = LogLevel{n: 0, t: "post", c: color.FgBlue}
|
||
|
PUT LogLevel = LogLevel{n: 0, t: "put", c: color.FgYellow}
|
||
|
DELETE LogLevel = LogLevel{n: 0, t: "delete", c: color.FgHiRed}
|
||
|
)
|
||
|
|
||
|
var methodLevelMap = map[string]LogLevel{
|
||
|
"get": GET, "post": POST, "put": PUT, "delete": DELETE,
|
||
|
}
|
||
|
|
||
|
func Create() *Logger {
|
||
|
return &Logger{
|
||
|
prefix: "LOG",
|
||
|
level: FATAL,
|
||
|
printTraceback: false,
|
||
|
printTime: true,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (l *Logger) GetHTTPLogger(next http.Handler) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
level := methodLevelMap[r.Method]
|
||
|
color.New(level.c).Println(l.buildString(methodLevelMap[strings.ToLower(r.Method)], "Hello"))
|
||
|
next.ServeHTTP(w, r)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (l *Logger) SetPrefix(prefix string) {
|
||
|
l.prefix = prefix
|
||
|
}
|
||
|
func (l *Logger) SetLevel(level LogLevel) {
|
||
|
l.level = level
|
||
|
}
|
||
|
func (l *Logger) SetPrintTraceback(b bool) {
|
||
|
l.printTraceback = b
|
||
|
}
|
||
|
|
||
|
func (l *Logger) Info(m interface{}) {
|
||
|
l.print(INFO, m)
|
||
|
}
|
||
|
|
||
|
func (l *Logger) Warn(m interface{}) {
|
||
|
l.print(WARN, m)
|
||
|
}
|
||
|
|
||
|
func (l *Logger) Error(m interface{}) {
|
||
|
l.print(ERROR, m)
|
||
|
}
|
||
|
|
||
|
func (l *Logger) Fatal(m interface{}) {
|
||
|
l.print(FATAL, m)
|
||
|
}
|
||
|
|
||
|
func (l *Logger) Debug(m interface{}) {
|
||
|
l.print(DEBUG, m)
|
||
|
}
|
||
|
|
||
|
func (l *Logger) formatTime(t time.Time) string {
|
||
|
return fmt.Sprintf("%02d.%02d.%02d %02d:%02d:%02d", t.Day(), t.Month(), t.Year(), t.Hour(), t.Minute(), t.Second())
|
||
|
}
|
||
|
|
||
|
func (l *Logger) getTraceback() *MethodTraceback {
|
||
|
caller, _, _, _ := runtime.Caller(4)
|
||
|
details := runtime.FuncForPC(caller)
|
||
|
signature := details.Name()
|
||
|
path, line := details.FileLine(caller)
|
||
|
splitPath := strings.Split(path, "/")
|
||
|
|
||
|
splitSignature := strings.Split(signature, ".")
|
||
|
pkg, method := splitSignature[0], splitSignature[len(splitSignature)-1]
|
||
|
|
||
|
tb := &MethodTraceback{
|
||
|
filename: splitPath[len(splitPath)-1],
|
||
|
fullPath: path,
|
||
|
line: line,
|
||
|
signature: signature,
|
||
|
Package: pkg,
|
||
|
Method: method,
|
||
|
}
|
||
|
|
||
|
return tb
|
||
|
}
|
||
|
func (l *Logger) formatTraceback(mt *MethodTraceback) string {
|
||
|
return fmt.Sprintf("%s:%s:%d", mt.filename, mt.Method, mt.line)
|
||
|
}
|
||
|
|
||
|
func (l *Logger) buildString(level LogLevel, m interface{}) string {
|
||
|
args := []string{
|
||
|
fmt.Sprintf("[%s]", l.prefix),
|
||
|
fmt.Sprintf("[%s]", strings.ToUpper(level.t)),
|
||
|
}
|
||
|
|
||
|
if l.printTraceback {
|
||
|
args = append(args, fmt.Sprintf("[%s]", l.formatTraceback(l.getTraceback())))
|
||
|
}
|
||
|
|
||
|
if l.printTime {
|
||
|
args = append(args, fmt.Sprintf("[%s]", l.formatTime(time.Now())))
|
||
|
}
|
||
|
|
||
|
return fmt.Sprintf("%s %v", strings.Join(args, " "), m)
|
||
|
}
|
||
|
|
||
|
func (l *Logger) print(level LogLevel, m interface{}) {
|
||
|
if l.level.n < level.n {
|
||
|
return
|
||
|
}
|
||
|
color.New(level.c).Print(l.buildString(level, m))
|
||
|
}
|