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

296 lines
6.5 KiB
Go

5 years ago
package starlog
import (
"fmt"
"os"
"path/filepath"
5 years ago
"runtime"
5 years ago
"strconv"
"time"
5 years ago
"github.com/fatih/color"
)
5 years ago
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
GREY = color.FgHiYellow
BOLD = color.Bold
)
5 years ago
5 years ago
const (
LvDebug = iota
LvInfo
LvNotice
LvWarning
LvError
LvCritical
LvPanic
LvFatal
)
5 years ago
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, switching bool = true, true, true, false
loghandle *os.File = nil
)
func write(logs string) {
for switching {
time.Sleep(time.Millisecond * 100)
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
func output(level int, showline, showlv, dowrite bool, strlog string) {
var logs string
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
for _, v := range Colors[level] {
color.Set(v)
5 years ago
}
5 years ago
fmt.Print(logs)
color.Set(color.Reset)
if dowrite {
go write(logs)
5 years ago
}
5 years ago
}
func StdPrint(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Print(str...)
5 years ago
color.Set(color.Reset)
5 years ago
}
5 years ago
5 years ago
func StdPrintf(c1, c2 color.Attribute, format string, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Printf(format, str...)
color.Set(color.Reset)
5 years ago
}
5 years ago
func StdPrintln(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Println(str...)
5 years ago
color.Set(color.Reset)
}
5 years ago
func Print(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
strs := fmt.Sprint(str...)
fmt.Print(strs)
color.Set(color.Reset)
write(strs)
}
5 years ago
5 years ago
func Printf(c1, c2 color.Attribute, format string, str ...interface{}) {
color.Set(c1)
color.Set(c2)
strs := fmt.Sprintf(format, str...)
fmt.Print(strs)
color.Set(color.Reset)
write(strs)
}
func Println(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
strs := fmt.Sprintln(str...)
fmt.Print(strs)
5 years ago
color.Set(color.Reset)
5 years ago
write(strs)
}
func Debug(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvDebug, ShowLine, ShowLevel, DoWrite, strs)
}
func Debugf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvDebug, ShowLine, ShowLevel, DoWrite, strs)
}
func Debugln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvDebug, ShowLine, ShowLevel, DoWrite, strs)
}
func Info(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvInfo, ShowLine, ShowLevel, DoWrite, strs)
}
func Infof(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvInfo, ShowLine, ShowLevel, DoWrite, strs)
}
func Infoln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvInfo, ShowLine, ShowLevel, DoWrite, strs)
}
func Notice(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvNotice, ShowLine, ShowLevel, DoWrite, strs)
}
func Noticef(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvNotice, ShowLine, ShowLevel, DoWrite, strs)
}
func Noticeln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvNotice, ShowLine, ShowLevel, DoWrite, strs)
}
func Warning(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvWarning, ShowLine, ShowLevel, DoWrite, strs)
}
func Warningf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvWarning, ShowLine, ShowLevel, DoWrite, strs)
}
func Warningln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvWarning, ShowLine, ShowLevel, DoWrite, strs)
}
func Error(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvError, ShowLine, ShowLevel, DoWrite, strs)
5 years ago
}
5 years ago
func Errorf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvError, ShowLine, ShowLevel, DoWrite, strs)
}
func Errorln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvError, ShowLine, ShowLevel, DoWrite, strs)
}
func Critical(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvCritical, ShowLine, ShowLevel, DoWrite, strs)
}
func Criticalf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvCritical, ShowLine, ShowLevel, DoWrite, strs)
}
func Criticalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvCritical, ShowLine, ShowLevel, DoWrite, strs)
}
func Fatal(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvFatal, ShowLine, ShowLevel, DoWrite, strs)
CloseLog()
os.Exit(9)
}
func Fatalf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvFatal, ShowLine, ShowLevel, DoWrite, strs)
CloseLog()
os.Exit(9)
}
func Fatalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvFatal, ShowLine, ShowLevel, DoWrite, strs)
CloseLog()
os.Exit(9)
}
func Panic(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvPanic, ShowLine, ShowLevel, DoWrite, strs)
panic(str)
}
func Panicf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvPanic, ShowLine, ShowLevel, DoWrite, strs)
panic(strs)
}
func Panicln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvPanic, ShowLine, ShowLevel, DoWrite, strs)
panic(str)
}
func SetLogFile(path string) error {
var err error
loghandle, err = os.Create(path)
return err
}
func SwitchFile(path string) error {
if loghandle != nil {
loghandle.Close()
}
return SetLogFile(path)
5 years ago
}
5 years ago
func CloseLog() {
if loghandle != nil {
loghandle.Close()
5 years ago
}
}