new starlog

pull/1/head
兔子 5 years ago
parent 6c5ebcf61f
commit 4f2eb0c238

@ -2,280 +2,294 @@ package starlog
import ( import (
"fmt" "fmt"
"strings"
"github.com/fatih/color"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"time" "time"
"github.com/fatih/color"
) )
var Path string const (
var LogPar *os.File BLUE = color.FgBlue
var CSpace bool = false 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
)
func CreateLog(logpath, prefix string) string { const (
logname := strconv.FormatInt(time.Now().Unix(), 10) LvDebug = iota
logpath, _ = filepath.Abs(logpath) LvInfo
_, exs := os.Stat(logpath) LvNotice
if exs != nil && os.IsNotExist(exs) { LvWarning
os.MkdirAll(logpath, 0755) LvError
} LvCritical
logname = logpath + `/` + prefix + logname + ".log" LvPanic
LogPar, _ = os.Create(logname) LvFatal
strpath, _ := filepath.Abs(logname) )
return strpath
}
func WriteError(err error, other string) { var (
now := time.Now().Format("2006-01-02 15:04:05") levels = map[int]string{
strlog := now + ": " + other + ":" + err.Error() + "\n" LvDebug: "DEBUG",
LogPar.Write([]byte(strlog)) LvInfo: "INFO",
} LvNotice: "NOTICE",
LvWarning: "WARNING",
LvError: "ERROR",
LvCritical: "CRITICAL",
LvPanic: "PANIC",
LvFatal: "FATAL",
}
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},
}
LogLevel int = 0
ShowLine, ShowLevel, DoWrite, switching bool = true, true, true, false
loghandle *os.File = nil
)
func WriteLog(strs string) { func write(logs string) {
now := time.Now().Format("2006-01-02 15:04:05") for switching {
strlog := now + ": " + strs + "\n" time.Sleep(time.Millisecond * 100)
LogPar.Write([]byte(strlog)) }
if loghandle == nil {
return
}
loghandle.WriteString(logs)
} }
func PrintError(sakura ...interface{}) { func output(level int, showline, showlv, dowrite bool, strlog string) {
var mycolor, bold string var logs string
lens := len(sakura) if level < LogLevel {
if lens < 2 {
return return
} }
if lens >= 3 { _, fname, line, _ := runtime.Caller(2)
mycolor, _ = sakura[2].(string) fname = filepath.Base(fname)
if lens == 4 { date := time.Now().Format("2006-01-02 15:04:05 Mon")
bold, _ = sakura[3].(string) if showline && showlv {
} logs = fmt.Sprintf("%s %s %s %s", date, fname+":"+strconv.Itoa(line), `[`+levels[level]+`]`, strlog)
} } else if showline && !showlv {
err, _ := sakura[0].(error) logs = fmt.Sprintf("%s %s %s", date, fname+":"+strconv.Itoa(line), strlog)
other, _ := sakura[1].(string) } else if !showline && showlv {
now := time.Now().Format("2006-01-02 15:04:05") logs = fmt.Sprintf("%s %s %s", date, `[`+levels[level]+`]`, strlog)
strlog := now + ": " + other + ":" + err.Error() } else {
switch strings.ToLower(mycolor) { logs = fmt.Sprintf("%s %s", date, strlog)
case "blue": }
color.Set(color.FgBlue) for _, v := range Colors[level] {
case "black": color.Set(v)
color.Set(color.FgBlack) }
case "cyan": fmt.Print(logs)
color.Set(color.FgCyan)
case "green":
color.Set(color.FgGreen)
case "magenta":
color.Set(color.FgMagenta)
case "red":
color.Set(color.FgRed)
case "white":
color.Set(color.FgWhite)
case "yellow":
color.Set(color.FgYellow)
case "grey":
color.Set(color.FgHiYellow)
default:
color.Set(color.Reset) color.Set(color.Reset)
if dowrite {
go write(logs)
} }
for _, v := range []byte(bold) {
switch string([]byte{v}) {
case "b":
color.Set(color.Bold)
case "l":
color.Set(color.BlinkRapid)
case "u":
color.Set(color.Underline)
} }
func StdPrint(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Print(str...)
color.Set(color.Reset)
} }
if CSpace {
strlog += "\n" func StdPrintf(c1, c2 color.Attribute, format string, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Printf(format, str...)
color.Set(color.Reset)
} }
fmt.Println(strlog)
func StdPrintln(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Println(str...)
color.Set(color.Reset) color.Set(color.Reset)
LogPar.Write([]byte(strlog + "\n"))
} }
func Println(sakura ...interface{}) {
var mycolor, bold string func Print(c1, c2 color.Attribute, str ...interface{}) {
lens := len(sakura) color.Set(c1)
if lens < 2 { color.Set(c2)
fmt.Println(sakura) strs := fmt.Sprint(str...)
return fmt.Print(strs)
color.Set(color.Reset)
write(strs)
} }
if lens >= 2 {
mycolor, _ = sakura[lens-2].(string) func Printf(c1, c2 color.Attribute, format string, str ...interface{}) {
if lens == 3 { color.Set(c1)
bold, _ = sakura[lens-1].(string) color.Set(c2)
} strs := fmt.Sprintf(format, str...)
} fmt.Print(strs)
switch strings.ToLower(mycolor) {
case "blue":
color.Set(color.FgBlue)
case "black":
color.Set(color.FgBlack)
case "cyan":
color.Set(color.FgCyan)
case "green":
color.Set(color.FgGreen)
case "magenta":
color.Set(color.FgMagenta)
case "red":
color.Set(color.FgRed)
case "white":
color.Set(color.FgWhite)
case "yellow":
color.Set(color.FgYellow)
case "grey":
color.Set(color.FgHiYellow)
default:
color.Set(color.Reset) color.Set(color.Reset)
write(strs)
} }
for _, v := range []byte(bold) {
switch string([]byte{v}) { func Println(c1, c2 color.Attribute, str ...interface{}) {
case "b": color.Set(c1)
color.Set(color.Bold) color.Set(c2)
case "l": strs := fmt.Sprintln(str...)
color.Set(color.BlinkRapid) fmt.Print(strs)
case "u": color.Set(color.Reset)
color.Set(color.Underline) write(strs)
} }
func Debug(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvDebug, ShowLine, ShowLevel, DoWrite, strs)
} }
var hoe []interface{}
for i := 0; i < lens-2; i++ { func Debugf(format string, str ...interface{}) {
hoe = append(hoe, sakura[i]) strs := fmt.Sprintf(format, str...)
output(LvDebug, ShowLine, ShowLevel, DoWrite, strs)
} }
if len(hoe) == 1 {
fmt.Println(hoe[0]) func Debugln(str ...interface{}) {
} else { strs := fmt.Sprintln(str...)
fmt.Println(hoe) output(LvDebug, ShowLine, ShowLevel, DoWrite, strs)
} }
color.Set(color.Reset)
func Info(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvInfo, ShowLine, ShowLevel, DoWrite, strs)
} }
func Print(sakura ...interface{}) {
var mycolor, bold string func Infof(format string, str ...interface{}) {
lens := len(sakura) strs := fmt.Sprintf(format, str...)
if lens < 2 { output(LvInfo, ShowLine, ShowLevel, DoWrite, strs)
fmt.Print(sakura)
return
} }
if lens >= 2 {
mycolor, _ = sakura[lens-2].(string) func Infoln(str ...interface{}) {
if lens == 3 { strs := fmt.Sprintln(str...)
bold, _ = sakura[lens-1].(string) output(LvInfo, ShowLine, ShowLevel, DoWrite, strs)
}
}
switch strings.ToLower(mycolor) {
case "blue":
color.Set(color.FgBlue)
case "black":
color.Set(color.FgBlack)
case "cyan":
color.Set(color.FgCyan)
case "green":
color.Set(color.FgGreen)
case "magenta":
color.Set(color.FgMagenta)
case "red":
color.Set(color.FgRed)
case "white":
color.Set(color.FgWhite)
case "yellow":
color.Set(color.FgYellow)
case "grey":
color.Set(color.FgHiYellow)
default:
color.Set(color.Reset)
} }
for _, v := range []byte(bold) {
switch string([]byte{v}) { func Notice(str ...interface{}) {
case "b": strs := fmt.Sprint(str...)
color.Set(color.Bold) output(LvNotice, ShowLine, ShowLevel, DoWrite, strs)
case "l":
color.Set(color.BlinkRapid)
case "u":
color.Set(color.Underline)
} }
func Noticef(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvNotice, ShowLine, ShowLevel, DoWrite, strs)
} }
var hoe []interface{}
for i := 0; i < lens-2; i++ { func Noticeln(str ...interface{}) {
hoe = append(hoe, sakura[i]) strs := fmt.Sprintln(str...)
output(LvNotice, ShowLine, ShowLevel, DoWrite, strs)
} }
if len(hoe) == 1 {
fmt.Println(hoe[0]) func Warning(str ...interface{}) {
} else { strs := fmt.Sprint(str...)
fmt.Println(hoe) output(LvWarning, ShowLine, ShowLevel, DoWrite, strs)
} }
color.Set(color.Reset)
func Warningf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
output(LvWarning, ShowLine, ShowLevel, DoWrite, strs)
} }
func PrintLog(sakura ...interface{}) { func Warningln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvWarning, ShowLine, ShowLevel, DoWrite, strs)
}
var mycolor, bold string func Error(str ...interface{}) {
lens := len(sakura) strs := fmt.Sprint(str...)
if lens < 1 { output(LvError, ShowLine, ShowLevel, DoWrite, strs)
return
} }
if lens >= 2 {
mycolor, _ = sakura[1].(string) func Errorf(format string, str ...interface{}) {
if lens == 3 { strs := fmt.Sprintf(format, str...)
bold, _ = sakura[2].(string) output(LvError, ShowLine, ShowLevel, DoWrite, strs)
} }
}
strs, _ := sakura[0].(string) func Errorln(str ...interface{}) {
now := time.Now().Format("2006-01-02 15:04:05") strs := fmt.Sprintln(str...)
strlog := now + ": " + strs output(LvError, ShowLine, ShowLevel, DoWrite, strs)
switch strings.ToLower(mycolor) { }
case "blue":
color.Set(color.FgBlue) func Critical(str ...interface{}) {
case "black": strs := fmt.Sprint(str...)
color.Set(color.FgBlack) output(LvCritical, ShowLine, ShowLevel, DoWrite, strs)
case "cyan":
color.Set(color.FgCyan)
case "green":
color.Set(color.FgGreen)
case "magenta":
color.Set(color.FgMagenta)
case "red":
color.Set(color.FgRed)
case "white":
color.Set(color.FgWhite)
case "yellow":
color.Set(color.FgYellow)
case "grey":
color.Set(color.FgHiYellow)
default:
color.Set(color.Reset)
} }
for _, v := range []byte(bold) {
switch string([]byte{v}) { func Criticalf(format string, str ...interface{}) {
case "b": strs := fmt.Sprintf(format, str...)
color.Set(color.Bold) output(LvCritical, ShowLine, ShowLevel, DoWrite, strs)
case "l":
color.Set(color.BlinkRapid)
case "u":
color.Set(color.Underline)
} }
func Criticalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
output(LvCritical, ShowLine, ShowLevel, DoWrite, strs)
} }
if CSpace {
strlog += "\n" func Fatal(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvFatal, ShowLine, ShowLevel, DoWrite, strs)
CloseLog()
os.Exit(9)
} }
fmt.Println(strlog)
color.Set(color.Reset) func Fatalf(format string, str ...interface{}) {
LogPar.Write([]byte(strlog + "\n")) 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 End() { func Panic(str ...interface{}) {
LogPar.Close() 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)
} }
func ThrowError(err error, other string) { func CloseLog() {
if err != nil { if loghandle != nil {
PrintError(err, other) loghandle.Close()
} }
End()
time.Sleep(time.Second * 8)
panic(err)
os.Exit(233)
} }

@ -1,8 +1,18 @@
package starlog package starlog
import "testing" import (
"errors"
"testing"
)
func Test_StarLog(t *testing.T) { func Test_LOG(t *testing.T) {
CreateLog("./sakura", "suki") SetLogFile("./okk.log")
Println("suki!", "blue", "b") Debugln("这是一个Debug事项")
Infoln("这是一个通知")
err := errors.New("NBM")
Errorln("你牛逼", err)
LogLevel = 1
Debugln("你看不到我了!")
Panicln("我要下线了")
CloseLog()
} }

Loading…
Cancel
Save