new starlog

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

@ -2,280 +2,294 @@ package starlog
import (
"fmt"
"strings"
"github.com/fatih/color"
"os"
"path/filepath"
"runtime"
"strconv"
"time"
"github.com/fatih/color"
)
var Path string
var LogPar *os.File
var CSpace bool = false
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
)
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
}
const (
LvDebug = iota
LvInfo
LvNotice
LvWarning
LvError
LvCritical
LvPanic
LvFatal
)
func WriteError(err error, other string) {
now := time.Now().Format("2006-01-02 15:04:05")
strlog := now + ": " + other + ":" + err.Error() + "\n"
LogPar.Write([]byte(strlog))
}
var (
levels = map[int]string{
LvDebug: "DEBUG",
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) {
now := time.Now().Format("2006-01-02 15:04:05")
strlog := now + ": " + strs + "\n"
LogPar.Write([]byte(strlog))
func write(logs string) {
for switching {
time.Sleep(time.Millisecond * 100)
}
if loghandle == nil {
return
}
loghandle.WriteString(logs)
}
func PrintError(sakura ...interface{}) {
var mycolor, bold string
lens := len(sakura)
if lens < 2 {
func output(level int, showline, showlv, dowrite bool, strlog string) {
var logs string
if level < LogLevel {
return
}
if lens >= 3 {
mycolor, _ = sakura[2].(string)
if lens == 4 {
bold, _ = sakura[3].(string)
_, 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)
}
for _, v := range Colors[level] {
color.Set(v)
}
err, _ := sakura[0].(error)
other, _ := sakura[1].(string)
now := time.Now().Format("2006-01-02 15:04:05")
strlog := now + ": " + other + ":" + err.Error()
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:
fmt.Print(logs)
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)
}
}
if CSpace {
strlog += "\n"
}
fmt.Println(strlog)
}
func StdPrint(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Print(str...)
color.Set(color.Reset)
LogPar.Write([]byte(strlog + "\n"))
}
func Println(sakura ...interface{}) {
var mycolor, bold string
lens := len(sakura)
if lens < 2 {
fmt.Println(sakura)
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:
func StdPrintf(c1, c2 color.Attribute, format string, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Printf(format, str...)
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)
}
}
func StdPrintln(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Println(str...)
color.Set(color.Reset)
}
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)
}
func Print(sakura ...interface{}) {
var mycolor, bold string
lens := len(sakura)
if lens < 2 {
fmt.Print(sakura)
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:
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)
}
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)
}
write(strs)
}
func Println(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
strs := fmt.Sprintln(str...)
fmt.Print(strs)
color.Set(color.Reset)
write(strs)
}
func PrintLog(sakura ...interface{}) {
func Debug(str ...interface{}) {
strs := fmt.Sprint(str...)
output(LvDebug, ShowLine, ShowLevel, DoWrite, strs)
}
var mycolor, bold string
lens := len(sakura)
if lens < 1 {
return
}
if lens >= 2 {
mycolor, _ = sakura[1].(string)
if lens == 3 {
bold, _ = sakura[2].(string)
}
}
strs, _ := sakura[0].(string)
now := time.Now().Format("2006-01-02 15:04:05")
strlog := now + ": " + 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)
LogPar.Write([]byte(strlog + "\n"))
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 End() {
LogPar.Close()
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 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)
}
func ThrowError(err error, other string) {
if err != nil {
PrintError(err, other)
func CloseLog() {
if loghandle != nil {
loghandle.Close()
}
End()
time.Sleep(time.Second * 8)
panic(err)
os.Exit(233)
}

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

Loading…
Cancel
Save