Commit 2bad25f5 by zhengqiuyun86

初始化

parent 5e325a41
package a
import (
"bytes"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"runtime"
"strconv"
"strings"
"time"
)
const (
DebugL = "Debug"
InfoL = "Info"
ErrorL = "Error"
)
// Colors
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
BlueBold = "\033[34;1m"
MagentaBold = "\033[35;1m"
RedBold = "\033[31;1m"
YellowBold = "\033[33;1m"
)
func Print(tag, msg string, file string, line int) {
str := "%s %s [%d] %s %d %s"
if conf.LogColorful {
if tag == "Error" {
str = "%s %s [%d] %s %d " + Red + "%s" + Reset
} else if tag == "Info" {
str = "%s %s [%d] %s %d " + Yellow + "%s" + Reset
} else {
str = "%s %s [%d] %s %d " + Green + "%s" + Reset
}
}
file = file[strings.LastIndex(file, "/")+1:]
fmt.Println(fmt.Sprintf(str, FormatDateMillTime(time.Now()), tag, getGID(), file, line, msg))
}
func FormatDateMillTime(dataTime time.Time) string {
return dataTime.Format("2006-01-02 15:04:05.000000")
}
func getGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
package log
import (
"bytes"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/log/a"
"runtime"
"strconv"
"strings"
"time"
)
const (
LevelDebug = "Debug"
LevelInfo = "Info"
LevelError = "Error"
)
func DebugDo(f func()) {
......@@ -16,7 +25,7 @@ func DebugDo(f func()) {
func IsDebug() bool {
ok := false
switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(a.DebugL):
case strings.ToLower(LevelDebug):
ok = true
break
}
......@@ -26,47 +35,93 @@ func IsDebug() bool {
func Debug(msg string) {
ok := false
switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(a.DebugL):
case strings.ToLower(LevelDebug):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
a.Print("Debug", msg, file, line)
print("Debug", msg, file, line)
}
}
func Info(msg string) {
ok := false
switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(a.DebugL):
case strings.ToLower(LevelDebug):
ok = true
break
case strings.ToLower(a.InfoL):
case strings.ToLower(LevelInfo):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
a.Print("Info", msg, file, line)
print("Info", msg, file, line)
}
}
func Error(msg string) {
ok := false
switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(a.DebugL):
case strings.ToLower(LevelDebug):
ok = true
break
case strings.ToLower(a.InfoL):
case strings.ToLower(LevelInfo):
ok = true
break
case strings.ToLower(a.ErrorL):
case strings.ToLower(LevelError):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
a.Print("Error", msg, file, line)
print("Error", msg, file, line)
}
}
// Colors
const (
reset = "\033[0m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
magenta = "\033[35m"
cyan = "\033[36m"
white = "\033[37m"
blueBold = "\033[34;1m"
magentaBold = "\033[35;1m"
redBold = "\033[31;1m"
yellowBold = "\033[33;1m"
)
func print(tag, msg string, file string, line int) {
str := "%s %s [%d] %s %d %s"
if conf.LogColorful {
if tag == "Error" {
str = "%s %s [%d] %s %d " + red + "%s" + reset
} else if tag == "Info" {
str = "%s %s [%d] %s %d " + yellow + "%s" + reset
} else {
str = "%s %s [%d] %s %d " + green + "%s" + reset
}
}
file = file[strings.LastIndex(file, "/")+1:]
fmt.Println(fmt.Sprintf(str, formatDateMillTime(time.Now()), tag, getGID(), file, line, msg))
}
func formatDateMillTime(dataTime time.Time) string {
return dataTime.Format("2006-01-02 15:04:05.000000")
}
func getGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment