You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
starlog/starlog.go

319 lines
7.3 KiB
Go

5 years ago
package starlog
import (
"fmt"
"os"
"path/filepath"
5 years ago
"runtime"
5 years ago
"strconv"
4 years ago
"sync"
5 years ago
"time"
5 years ago
"github.com/fatih/color"
4 years ago
"github.com/mattn/go-colorable"
5 years ago
)
5 years ago
4 years ago
// Base attributes
5 years ago
const (
BLUE = color.FgBlue
BLACK = color.FgBlack
CYAN = color.FgCyan
GREEN = color.FgGreen
MAGENTA = color.FgMagenta
RED = color.FgRed
WHITE = color.FgWhite
YELLOW = color.FgYellow
4 years ago
GREY = color.FgHiRed
5 years ago
BOLD = color.Bold
)
5 years ago
4 years ago
// Log Level
5 years ago
const (
LvDebug = iota
LvInfo
LvNotice
LvWarning
LvError
LvCritical
LvPanic
LvFatal
)
5 years ago
4 years ago
// Log Init Data
5 years ago
var (
levels = map[int]string{
LvDebug: "DEBUG",
LvInfo: "INFO",
LvNotice: "NOTICE",
LvWarning: "WARNING",
LvError: "ERROR",
LvCritical: "CRITICAL",
LvPanic: "PANIC",
LvFatal: "FATAL",
5 years ago
}
5 years ago
Colors = map[int][]color.Attribute{
LvDebug: []color.Attribute{WHITE},
LvInfo: []color.Attribute{GREEN},
LvNotice: []color.Attribute{BLUE},
LvWarning: []color.Attribute{YELLOW},
LvError: []color.Attribute{MAGENTA},
LvCritical: []color.Attribute{RED, BOLD},
LvPanic: []color.Attribute{RED, BOLD},
LvFatal: []color.Attribute{RED},
5 years ago
}
5 years ago
LogLevel int = 0
ShowLine, ShowLevel, DoWrite, DoShow, switching bool = true, true, true, true, false
loghandle *os.File = nil
HandleFunc func([]color.Attribute, string)
4 years ago
lock sync.WaitGroup
4 years ago
waiting = make(chan int, 1)
5 years ago
)
4 years ago
// Output 使用此进行windows支持
var Output = colorable.NewColorableStdout()
5 years ago
func write(logs string) {
4 years ago
var i int
lock.Add(1)
4 years ago
defer lock.Done()
5 years ago
for switching {
time.Sleep(time.Millisecond * 100)
4 years ago
i++
if i > 20 {
return
}
5 years ago
}
5 years ago
if loghandle == nil {
return
5 years ago
}
5 years ago
loghandle.WriteString(logs)
5 years ago
}
5 years ago
5 years ago
func output(level int, showline, showlv, dowrite, doshow bool, strlog string) {
5 years ago
var logs string
4 years ago
waiting <- 1
defer func() {
<-waiting
}()
5 years ago
if level < LogLevel {
5 years ago
return
}
5 years ago
_, fname, line, _ := runtime.Caller(2)
fname = filepath.Base(fname)
date := time.Now().Format("2006-01-02 15:04:05 Mon")
if showline && showlv {
logs = fmt.Sprintf("%s %s %s %s", date, fname+":"+strconv.Itoa(line), `[`+levels[level]+`]`, strlog)
} else if showline && !showlv {
logs = fmt.Sprintf("%s %s %s", date, fname+":"+strconv.Itoa(line), strlog)
} else if !showline && showlv {
logs = fmt.Sprintf("%s %s %s", date, `[`+levels[level]+`]`, strlog)
} else {
logs = fmt.Sprintf("%s %s", date, strlog)
5 years ago
}
5 years ago
if doshow {
4 years ago
logcolor := color.New(Colors[level]...)
logstr := logcolor.Sprint(logs)
fmt.Fprint(Output, logstr)
5 years ago
}
if HandleFunc != nil {
go HandleFunc(Colors[level], logs)
5 years ago
}
5 years ago
if dowrite {
4 years ago
write(logs)
5 years ago
}
5 years ago
}
4 years ago
// StdPrint 进行stdout标准输出
5 years ago
func StdPrint(c1, c2 color.Attribute, str ...interface{}) {
4 years ago
waiting <- 1
defer func() {
<-waiting
}()
colorstr := color.New(c1, c2)
colorstr.Fprint(Output, str...)
5 years ago
}
5 years ago
5 years ago
func StdPrintf(c1, c2 color.Attribute, format string, str ...interface{}) {
4 years ago
waiting <- 1
defer func() {
<-waiting
}()
colorstr := color.New(c1, c2)
colorstr.Fprintf(Output, format, str...)
5 years ago
}
5 years ago
func StdPrintln(c1, c2 color.Attribute, str ...interface{}) {
4 years ago
waiting <- 1
defer func() {
<-waiting
}()
colorstr := color.New(c1, c2)
colorstr.Fprintln(Output, str...)
5 years ago
}
5 years ago
func Print(c1, c2 color.Attribute, str ...interface{}) {
4 years ago
StdPrint(c1, c2, str...)
write(fmt.Sprint(str...))
5 years ago
}
5 years ago
5 years ago
func Printf(c1, c2 color.Attribute, format string, str ...interface{}) {
4 years ago
StdPrintf(c1, c2, format, str...)
write(fmt.Sprintf(format, str...))
5 years ago
}
func Println(c1, c2 color.Attribute, str ...interface{}) {
4 years ago
StdPrintln(c1, c2, str...)
write(fmt.Sprintln(str...))
5 years ago
}
func Debug(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvDebug, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Debugf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvDebug, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Debugln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvDebug, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Info(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvInfo, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Infof(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvInfo, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Infoln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvInfo, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Notice(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvNotice, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Noticef(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvNotice, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Noticeln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvNotice, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Warning(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvWarning, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Warningf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvWarning, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Warningln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvWarning, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Error(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvError, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
5 years ago
func Errorf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvError, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Errorln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvError, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Critical(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvCritical, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Criticalf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvCritical, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Criticalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvCritical, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Fatal(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvFatal, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
CloseLog()
os.Exit(9)
}
func Fatalf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvFatal, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
CloseLog()
os.Exit(9)
}
func Fatalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvFatal, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
CloseLog()
os.Exit(9)
}
func Panic(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvPanic, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
panic(str)
}
func Panicf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvPanic, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
panic(strs)
}
func Panicln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvPanic, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
panic(str)
}
func SetLogFile(path string) error {
var err error
loghandle, err = os.Create(path)
4 years ago
switching = false
5 years ago
return err
}
func SwitchFile(path string) error {
4 years ago
switching = true
5 years ago
if loghandle != nil {
loghandle.Close()
}
return SetLogFile(path)
5 years ago
}
5 years ago
func CloseLog() {
4 years ago
lock.Wait()
5 years ago
if loghandle != nil {
loghandle.Close()
5 years ago
}
}