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"
)
var Path string "github.com/fatih/color"
var LogPar *os.File )
var CSpace bool = false
func CreateLog(logpath, prefix string) string {
logname := strconv.FormatInt(time.Now().Unix(), 10)
logpath, _ = filepath.Abs(logpath)
_, exs := os.Stat(logpath)
if exs != nil && os.IsNotExist(exs) {
os.MkdirAll(logpath, 0755)
}
logname = logpath + `/` + prefix + logname + ".log"
LogPar, _ = os.Create(logname)
strpath, _ := filepath.Abs(logname)
return strpath
}
func WriteError(err error, other string) { const (
now := time.Now().Format("2006-01-02 15:04:05") BLUE = color.FgBlue
strlog := now + ": " + other + ":" + err.Error() + "\n" BLACK = color.FgBlack
LogPar.Write([]byte(strlog)) 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 WriteLog(strs string) { const (
now := time.Now().Format("2006-01-02 15:04:05") LvDebug = iota
strlog := now + ": " + strs + "\n" LvInfo
LogPar.Write([]byte(strlog)) LvNotice
} LvWarning
LvError
LvCritical
LvPanic
LvFatal
)
func PrintError(sakura ...interface{}) { var (
var mycolor, bold string levels = map[int]string{
lens := len(sakura) LvDebug: "DEBUG",
if lens < 2 { LvInfo: "INFO",
return LvNotice: "NOTICE",
} LvWarning: "WARNING",
if lens >= 3 { LvError: "ERROR",
mycolor, _ = sakura[2].(string) LvCritical: "CRITICAL",
if lens == 4 { LvPanic: "PANIC",
bold, _ = sakura[3].(string) LvFatal: "FATAL",
}
} }
err, _ := sakura[0].(error) Colors = map[int][]color.Attribute{
other, _ := sakura[1].(string) LvDebug: []color.Attribute{WHITE},
now := time.Now().Format("2006-01-02 15:04:05") LvInfo: []color.Attribute{GREEN},
strlog := now + ": " + other + ":" + err.Error() LvNotice: []color.Attribute{BLUE},
switch strings.ToLower(mycolor) { LvWarning: []color.Attribute{YELLOW},
case "blue": LvError: []color.Attribute{MAGENTA},
color.Set(color.FgBlue) LvCritical: []color.Attribute{RED, BOLD},
case "black": LvPanic: []color.Attribute{RED, BOLD},
color.Set(color.FgBlack) LvFatal: []color.Attribute{RED},
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) { LogLevel int = 0
switch string([]byte{v}) { ShowLine, ShowLevel, DoWrite, switching bool = true, true, true, false
case "b": loghandle *os.File = nil
color.Set(color.Bold) )
case "l":
color.Set(color.BlinkRapid) func write(logs string) {
case "u": for switching {
color.Set(color.Underline) time.Sleep(time.Millisecond * 100)
}
} }
if CSpace { if loghandle == nil {
strlog += "\n" return
} }
fmt.Println(strlog) loghandle.WriteString(logs)
color.Set(color.Reset)
LogPar.Write([]byte(strlog + "\n"))
} }
func Println(sakura ...interface{}) {
var mycolor, bold string func output(level int, showline, showlv, dowrite bool, strlog string) {
lens := len(sakura) var logs string
if lens < 2 { if level < LogLevel {
fmt.Println(sakura)
return return
} }
if lens >= 2 { _, fname, line, _ := runtime.Caller(2)
mycolor, _ = sakura[lens-2].(string) fname = filepath.Base(fname)
if lens == 3 { date := time.Now().Format("2006-01-02 15:04:05 Mon")
bold, _ = sakura[lens-1].(string) if showline && showlv {
} logs = fmt.Sprintf("%s %s %s %s", date, fname+":"+strconv.Itoa(line), `[`+levels[level]+`]`, strlog)
} } else if showline && !showlv {
switch strings.ToLower(mycolor) { logs = fmt.Sprintf("%s %s %s", date, fname+":"+strconv.Itoa(line), strlog)
case "blue": } else if !showline && showlv {
color.Set(color.FgBlue) logs = fmt.Sprintf("%s %s %s", date, `[`+levels[level]+`]`, strlog)
case "black": } else {
color.Set(color.FgBlack) logs = fmt.Sprintf("%s %s", date, strlog)
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}) {
case "b":
color.Set(color.Bold)
case "l":
color.Set(color.BlinkRapid)
case "u":
color.Set(color.Underline)
}
} }
var hoe []interface{} for _, v := range Colors[level] {
for i := 0; i < lens-2; i++ { color.Set(v)
hoe = append(hoe, sakura[i])
} }
if len(hoe) == 1 { fmt.Print(logs)
fmt.Println(hoe[0]) color.Set(color.Reset)
} else { if dowrite {
fmt.Println(hoe) go write(logs)
} }
}
func StdPrint(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Print(str...)
color.Set(color.Reset) color.Set(color.Reset)
}
func StdPrintf(c1, c2 color.Attribute, format string, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Printf(format, str...)
color.Set(color.Reset)
} }
func Print(sakura ...interface{}) {
var mycolor, bold string func StdPrintln(c1, c2 color.Attribute, str ...interface{}) {
lens := len(sakura) color.Set(c1)
if lens < 2 { color.Set(c2)
fmt.Print(sakura) fmt.Println(str...)
return
}
if lens >= 2 {
mycolor, _ = sakura[lens-2].(string)
if lens == 3 {
bold, _ = sakura[lens-1].(string)
}
}
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}) {
case "b":
color.Set(color.Bold)
case "l":
color.Set(color.BlinkRapid)
case "u":
color.Set(color.Underline)
}
}
var hoe []interface{}
for i := 0; i < lens-2; i++ {
hoe = append(hoe, sakura[i])
}
if len(hoe) == 1 {
fmt.Println(hoe[0])
} else {
fmt.Println(hoe)
}
color.Set(color.Reset) color.Set(color.Reset)
} }
func PrintLog(sakura ...interface{}) { 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)
}
var mycolor, bold string func Printf(c1, c2 color.Attribute, format string, str ...interface{}) {
lens := len(sakura) color.Set(c1)
if lens < 1 { color.Set(c2)
return strs := fmt.Sprintf(format, str...)
} fmt.Print(strs)
if lens >= 2 { color.Set(color.Reset)
mycolor, _ = sakura[1].(string) write(strs)
if lens == 3 { }
bold, _ = sakura[2].(string)
} func Println(c1, c2 color.Attribute, str ...interface{}) {
} color.Set(c1)
strs, _ := sakura[0].(string) color.Set(c2)
now := time.Now().Format("2006-01-02 15:04:05") strs := fmt.Sprintln(str...)
strlog := now + ": " + strs 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)
}
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)
}
}
if CSpace {
strlog += "\n"
}
fmt.Println(strlog)
color.Set(color.Reset) color.Set(color.Reset)
LogPar.Write([]byte(strlog + "\n")) 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)
} }
func End() { func Errorf(format string, str ...interface{}) {
LogPar.Close() 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)
} }
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