version 3 relesed

pull/1/head
兔子 3 years ago
parent ecba5e76ff
commit 623cc55dbd

@ -0,0 +1,274 @@
package starlog
import (
"errors"
"os"
"path/filepath"
"time"
"b612.me/staros"
"b612.me/starmap"
)
type Archive interface {
ShouldArchiveNow(string, os.FileInfo) bool
NextLogFilePath(string, os.FileInfo) string
Interval() int64
HookBeforArchive() func(string, os.FileInfo) error
HookAfterArchive() func(string, string, os.FileInfo) error
}
type logfileinfo struct {
fullpath string
pointer *os.File
}
func SetLogFile(path string, logger *StarLogger, appendMode bool) error {
var fileMode int
if appendMode {
fileMode = os.O_APPEND | os.O_CREATE
} else {
fileMode = os.O_CREATE | os.O_WRONLY
}
fullpath, err := filepath.Abs(path)
if err != nil {
return err
}
if !appendMode && staros.Exists(fullpath) {
os.Remove(fullpath)
}
fp, err := os.OpenFile(fullpath, fileMode, 0644)
if err != nil {
return err
}
if starmap.MustGet(logger.logcore.id) != nil {
logger.SetSwitching(true)
err := starmap.MustGet(logger.logcore.id).(logfileinfo).pointer.Close()
if err != nil {
logger.logcore.output = nil
logger.SetSwitching(false)
return err
}
err = starmap.Delete(logger.logcore.id)
if err != nil {
logger.logcore.output = nil
logger.SetSwitching(false)
return err
}
}
err = starmap.Store(logger.logcore.id, logfileinfo{
fullpath: fullpath,
pointer: fp,
})
if err != nil {
fp.Close()
logger.logcore.output = nil
logger.SetSwitching(false)
return err
}
logger.SetSwitching(true)
logger.logcore.output = fp
logger.SetSwitching(false)
return nil
}
func Close(logger *StarLogger) error {
defer logger.SetSwitching(false)
if starmap.MustGet(logger.logcore.id) != nil {
logger.SetSwitching(true)
err := starmap.MustGet(logger.logcore.id).(*os.File).Close()
if err != nil {
logger.logcore.output = nil
return err
}
err = starmap.Delete(logger.logcore.id)
if err != nil {
return err
}
}
return nil
}
func GetLogFileInfo(logger *StarLogger) (os.FileInfo, error) {
if starmap.MustGet(logger.logcore.id) != nil {
return starmap.MustGet(logger.logcore.id).(logfileinfo).pointer.Stat()
}
return nil, errors.New("logger don't have a register logfile")
}
func StartArchive(logger *StarLogger, arch Archive) error {
if starmap.MustGet("arch"+logger.logcore.id) != nil {
return errors.New("already running")
}
stopChan := make(chan int)
starmap.Store("arch"+logger.logcore.id, stopChan)
go func(stopChan chan int, arch Archive, logger *StarLogger) {
for {
select {
case <-stopChan:
return
case <-time.After(time.Second * time.Duration(arch.Interval())):
}
fileinfo, err := GetLogFileInfo(logger)
if err != nil {
logger.Errorf("cannot get log file info,reason is %v\n", err)
continue
}
if starmap.MustGet(logger.logcore.id) == nil {
logger.Errorf("cannot get log core info from the map:no such keys\n")
continue
}
fullpath := starmap.MustGet(logger.logcore.id).(logfileinfo).fullpath
if !arch.ShouldArchiveNow(fullpath, fileinfo) {
continue
}
newLogPath := arch.NextLogFilePath(fullpath, fileinfo)
if arch.HookBeforArchive() != nil {
if err := arch.HookBeforArchive()(fullpath, fileinfo); err != nil {
logger.Errorf("error occur while executing hook before archive,detail is %v\n", err)
continue
}
}
if err := SetLogFile(newLogPath, logger, false); err != nil {
logger.Errorf("error occur while executing coverting new log file,detail is %v\n", err)
continue
} else {
logger.Debugln("Set Log Success")
}
fileinfo, err = GetLogFileInfo(logger)
if err != nil {
logger.Errorf("cannot get new log core info from the map:no such keys\n")
continue
}
if arch.HookAfterArchive() != nil {
if err := arch.HookAfterArchive()(fullpath, newLogPath, fileinfo); err != nil {
logger.Errorf("error occur while executing hook after archive,detail is %v\n", err)
continue
}
}
}
}(stopChan, arch, logger)
return nil
}
func IsArchiveRun(logger *StarLogger) bool {
if starmap.MustGet("arch"+logger.logcore.id) == nil {
return false
}
return true
}
func StopArchive(logger *StarLogger) {
if starmap.MustGet("arch"+logger.logcore.id) == nil {
return
}
starmap.MustGet("arch" + logger.logcore.id).(chan int) <- 1
}
type ArchiveByDate struct {
interval int64
checkInterval int64
newFileNameStyle string
hookBefor func(string, os.FileInfo) error
hookAfter func(string, string, os.FileInfo) error
}
func (abd *ArchiveByDate) ShouldArchiveNow(fullpath string, info os.FileInfo) bool {
if time.Now().Unix()-staros.GetFileCreationTime(info).Unix() > abd.interval {
return true
}
return false
}
func (abd *ArchiveByDate) NextLogFilePath(oldpath string, info os.FileInfo) string {
dir := filepath.Dir(oldpath)
newName := time.Now().Format(abd.newFileNameStyle)
return filepath.Join(dir, newName)
}
func (abd *ArchiveByDate) Interval() int64 {
return abd.checkInterval
}
func (abd *ArchiveByDate) HookBeforArchive() func(string, os.FileInfo) error {
return abd.hookBefor
}
func (abd *ArchiveByDate) HookAfterArchive() func(string, string, os.FileInfo) error {
return abd.hookAfter
}
func (abd *ArchiveByDate) SetHookBeforArchive(f func(string, os.FileInfo) error) {
abd.hookBefor = f
}
func (abd *ArchiveByDate) SetHookAfterArchive(f func(string, string, os.FileInfo) error) {
abd.hookAfter = f
}
func NewArchiveByDate(archInterval int64, checkInterval int64, fileStyle string, hookbefor func(string, os.FileInfo) error, hookafter func(string, string, os.FileInfo) error) *ArchiveByDate {
return &ArchiveByDate{
interval: archInterval,
checkInterval: checkInterval,
newFileNameStyle: fileStyle,
hookBefor: hookbefor,
hookAfter: hookafter,
}
}
type ArchiveBySize struct {
size int64
checkInterval int64
newFileNameStyle string
hookBefor func(string, os.FileInfo) error
hookAfter func(string, string, os.FileInfo) error
}
func (abd *ArchiveBySize) ShouldArchiveNow(fullpath string, info os.FileInfo) bool {
if info.Size() > abd.size {
return true
}
return false
}
func (abd *ArchiveBySize) NextLogFilePath(oldpath string, info os.FileInfo) string {
dir := filepath.Dir(oldpath)
newName := time.Now().Format(abd.newFileNameStyle)
return filepath.Join(dir, newName)
}
func (abd *ArchiveBySize) Interval() int64 {
return abd.checkInterval
}
func (abd *ArchiveBySize) HookBeforArchive() func(string, os.FileInfo) error {
return abd.hookBefor
}
func (abd *ArchiveBySize) HookAfterArchive() func(string, string, os.FileInfo) error {
return abd.hookAfter
}
func (abd *ArchiveBySize) SetHookBeforArchive(f func(string, os.FileInfo) error) {
abd.hookBefor = f
}
func (abd *ArchiveBySize) SetHookAfterArchive(f func(string, string, os.FileInfo) error) {
abd.hookAfter = f
}
func NewArchiveBySize(size int64, checkInterval int64, fileStyle string, hookbefor func(string, os.FileInfo) error, hookafter func(string, string, os.FileInfo) error) *ArchiveBySize {
return &ArchiveBySize{
size: size,
checkInterval: checkInterval,
newFileNameStyle: fileStyle,
hookBefor: hookbefor,
hookAfter: hookafter,
}
}

@ -0,0 +1,249 @@
package starlog
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
func (logger *starlog) build(thread string, isStd bool, handler func([]Attr, string), level int, logDetail string) {
logger.mu.Lock()
defer logger.mu.Unlock()
var skip, line int = 3, 0
var funcname, fileName string
now := time.Now()
if isStd {
skip++
}
if logger.showDeatilFile || logger.showFuncName {
pc, fName, codeln, ok := runtime.Caller(skip)
if !ok {
return
}
line = codeln
funcname = runtime.FuncForPC(pc).Name()
funcname = filepath.Ext(funcname)
funcname = strings.TrimPrefix(funcname, ".")
fileName = filepath.Base(fName)
}
y, m, d := now.Date()
h, i, s := now.Clock()
micro := now.Nanosecond() / 1e3
logStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%06d", y, m, d, h, i, s, micro)
if logger.showDeatilFile {
logStr += " " + fileName + ":" + strconv.Itoa(line)
}
if logger.showFuncName {
logStr += " <" + funcname + ">"
}
if logger.showThread {
logStr += " |" + thread + "|"
}
if logger.showLevel {
logStr += " " + `[` + levels[level] + `]`
}
logStr += " " + logDetail
if logger.showStd {
if !logger.showColor {
fmt.Print(logStr)
} else {
//logcolor := NewColor(logger.colorList[level]...)
logger.colorMe[level].Fprint(stdScreen, logStr)
}
}
if handler != nil {
stacks.Push(logTransfer{
handlerFunc: handler,
colors: logger.colorList[level],
logStr: logStr,
})
}
if !logger.stopWriter {
logger.write(logStr)
}
}
func (logger *starlog) write(logStr string) {
if logger.output == nil || logger.stopWriter {
return
}
var count int = 0
for logger.switching {
time.Sleep(time.Millisecond * 100)
count++
if count > 50 {
return
}
}
if logger.output == nil {
return
}
logger.output.Write([]byte(logStr))
}
func (logger *starlog) print(str ...interface{}) string {
return fmt.Sprint(str...)
}
func (logger *starlog) printf(format string, str ...interface{}) string {
return fmt.Sprintf(format, str...)
}
func (logger *starlog) println(str ...interface{}) string {
return fmt.Sprintln(str...)
}
func (logger *starlog) Debug(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
logger.build(thread, isStd, handler, LvDebug, strs)
}
func (logger *starlog) Debugf(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.build(thread, isStd, handler, LvDebug, strs)
}
func (logger *starlog) Debugln(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.build(thread, isStd, handler, LvDebug, strs)
}
func (logger *starlog) Info(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
logger.build(thread, isStd, handler, LvInfo, strs)
}
func (logger *starlog) Infof(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.build(thread, isStd, handler, LvInfo, strs)
}
func (logger *starlog) Infoln(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.build(thread, isStd, handler, LvInfo, strs)
}
func (logger *starlog) Notice(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
logger.build(thread, isStd, handler, LvNotice, strs)
}
func (logger *starlog) Noticef(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.build(thread, isStd, handler, LvNotice, strs)
}
func (logger *starlog) Noticeln(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.build(thread, isStd, handler, LvNotice, strs)
}
func (logger *starlog) Warning(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
logger.build(thread, isStd, handler, LvWarning, strs)
}
func (logger *starlog) Warningf(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.build(thread, isStd, handler, LvWarning, strs)
}
func (logger *starlog) Warningln(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.build(thread, isStd, handler, LvWarning, strs)
}
func (logger *starlog) Error(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
logger.build(thread, isStd, handler, LvError, strs)
}
func (logger *starlog) Errorf(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.build(thread, isStd, handler, LvError, strs)
}
func (logger *starlog) Errorln(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.build(thread, isStd, handler, LvError, strs)
}
func (logger *starlog) Critical(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
logger.build(thread, isStd, handler, LvCritical, strs)
}
func (logger *starlog) Criticalf(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.build(thread, isStd, handler, LvCritical, strs)
}
func (logger *starlog) Criticalln(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.build(thread, isStd, handler, LvCritical, strs)
}
func (logger *starlog) Fatal(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
logger.build(thread, isStd, handler, LvFatal, strs)
os.Exit(9)
}
func (logger *starlog) Fatalf(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.build(thread, isStd, handler, LvFatal, strs)
os.Exit(9)
}
func (logger *starlog) Fatalln(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.build(thread, isStd, handler, LvFatal, strs)
os.Exit(9)
}
func (logger *starlog) Panic(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
logger.build(thread, isStd, handler, LvPanic, strs)
panic(str)
}
func (logger *starlog) Panicf(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.build(thread, isStd, handler, LvPanic, strs)
panic(fmt.Sprintf(format, str...))
}
func (logger *starlog) Panicln(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.build(thread, isStd, handler, LvPanic, strs)
panic(fmt.Sprintln(str...))
}
func (logger *starlog) Print(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprint(str...)
if logger.showStd {
fmt.Print(strs)
}
logger.write(strs)
}
func (logger *starlog) Printf(thread string, isStd bool, handler func([]Attr, string), format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
if logger.showStd {
fmt.Print(strs)
}
logger.write(strs)
}
func (logger *starlog) Println(thread string, isStd bool, handler func([]Attr, string), str ...interface{}) {
strs := fmt.Sprintln(str...)
if logger.showStd {
fmt.Print(strs)
}
logger.write(strs)
}

@ -0,0 +1,271 @@
package starlog
import (
"io"
"math/rand"
"sync"
"time"
)
var Std *StarLogger
var stdmu sync.Mutex
func init() {
rand.Seed(time.Now().UnixNano())
stackStopChan = make(chan int)
StartStacks()
Std = NewStarlog(nil)
}
func SetShowColor(val bool) {
Std.SetShowColor(val)
}
func GetShowColor() bool {
return Std.GetShowColor()
}
func SetLevelColor(level int, color []Attr) {
Std.SetLevelColor(level, color)
}
func GetLevelColor(level int) []Attr {
return Std.GetLevelColor(level)
}
func Debug(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Debug(str...)
Std.isStd = false
}
func Debugf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Debugf(format, str...)
Std.isStd = false
}
func Debugln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Debugln(str...)
Std.isStd = false
}
func Info(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Info(str...)
Std.isStd = false
}
func Infof(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Infof(format, str...)
Std.isStd = false
}
func Infoln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Infoln(str...)
Std.isStd = false
}
func Notice(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Notice(str...)
Std.isStd = false
}
func Noticef(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Noticef(format, str...)
Std.isStd = false
}
func Noticeln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Noticeln(str...)
Std.isStd = false
}
func Warning(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Warning(str...)
Std.isStd = false
}
func Warningf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Warningf(format, str...)
Std.isStd = false
}
func Warningln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Warningln(str...)
Std.isStd = false
}
func Error(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Error(str...)
Std.isStd = false
}
func Errorf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Errorf(format, str...)
Std.isStd = false
}
func Errorln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Errorln(str...)
Std.isStd = false
}
func Critical(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Critical(str...)
Std.isStd = false
}
func Criticalf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Criticalf(format, str...)
Std.isStd = false
}
func Criticalln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Criticalln(str...)
Std.isStd = false
}
func Fatal(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Fatal(str...)
Std.isStd = false
}
func Fatalf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Fatalf(format, str...)
Std.isStd = false
}
func Panicln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Fatal(str...)
Std.isStd = false
}
func SetWriter(wr io.Writer) {
Std.SetWriter(wr)
}
func GetWriter() io.Writer {
return Std.GetWriter()
}
func SetHandler(f func([]Attr, string)) {
Std.SetHandler(f)
}
func GetHandler() func([]Attr, string) {
return Std.GetHandler()
}
func SetSwitching(sw bool) {
Std.SetSwitching(sw)
}
func SetShowOriginFile(val bool) {
Std.SetShowOriginFile(val)
}
func GetShowOriginFile() bool {
return Std.GetShowOriginFile()
}
func SetShowFuncName(val bool) {
Std.logcore.showFuncName = val
}
func GetShowFuncName() bool {
return Std.logcore.showFuncName
}
func SetShowLevel(val bool) {
Std.SetShowLevel(val)
}
func GetShowLevel() bool {
return Std.GetShowLevel()
}
func SetShowFlag(val bool) {
Std.SetShowFlag(val)
}
func GetShowFlag() bool {
return Std.GetShowFlag()
}
func SetShowStd(val bool) {
Std.SetShowStd(val)
}
func GetShowStd() bool {
return Std.GetShowStd()
}
func StopWrite() {
Std.StopWrite()
}
func EnbaleWrite() {
Std.EnbaleWrite()
}
func IsWriteStoed() bool {
return Std.IsWriteStoed()
}

@ -1,704 +1,175 @@
package starlog
import (
"errors"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"b612.me/starmap"
"github.com/mattn/go-colorable"
)
const (
LvDebug = iota
LvInfo
LvNotice
LvWarning
LvError
LvCritical
LvPanic
LvFatal
)
var (
levels = map[int]string{
LvDebug: "DEBUG",
LvInfo: "INFO",
LvNotice: "NOTICE",
LvWarning: "WARNING",
LvError: "ERROR",
LvCritical: "CRITICAL",
LvPanic: "PANIC",
LvFatal: "FATAL",
}
Colors = map[int][]Attr{
LvDebug: []Attr{FgWhite},
LvInfo: []Attr{FgGreen},
LvNotice: []Attr{FgBlue},
LvWarning: []Attr{FgYellow},
LvError: []Attr{FgMagenta},
LvCritical: []Attr{FgRed, Bold},
LvPanic: []Attr{FgRed, Bold},
LvFatal: []Attr{FgRed},
}
stdLock sync.Mutex
logmaps sync.Map
)
type StarLogger struct {
mu *sync.Mutex
lock *sync.Mutex
Flag string
out io.Writer
ShowLine bool
ShowLevel bool
ShowFunc bool
ShowThread bool
DoWrite bool
DoShow bool
switching bool
isStd bool
//handleFunc
stopChan chan int
setChan chan int
outshow io.Writer
LogLevel int
handleFunc func([]Attr, string)
StdFuncColor bool
logbuf string
BufSize int
stacks starmap.StarStack
}
type logTran struct {
color []Attr
log string
}
var Std = New(nil)
var StdFile *os.File
func init() {
Std.DoShow = true
Std.Flag = "MAN"
Std.isStd = true
logmaps.Store("std", Std)
}
func Close() error {
if StdFile == nil {
return errors.New("File not Open")
}
if err := Std.Flush(); err != nil {
return err
}
return StdFile.Close()
}
func New(out io.Writer) *StarLogger {
rand.Seed(time.Now().UnixNano())
logger := new(StarLogger)
logger.DoShow = false
logger.mu = new(sync.Mutex)
logger.lock = new(sync.Mutex)
logger.ShowLevel = true
logger.ShowLine = true
logger.DoWrite = true
logger.ShowThread = true
logger.ShowFunc = false
logger.out = out
logger.outshow = colorable.NewColorableStdout()
logger.StdFuncColor = true
logger.BufSize = 0
logger.Flag = "MAN"
logger.stopChan, logger.setChan = make(chan int), make(chan int)
go logger.runTransfer()
logmaps.Store(fmt.Sprint(time.Now().UnixNano()), logger)
return logger
}
func (logger *StarLogger) SetHandleFunc(funcs func([]Attr, string)) {
if logger.handleFunc == nil {
logger.setChan <- 1
}
logger.handleFunc = funcs
}
func (logger *StarLogger) GetHandleFunc() func([]Attr, string) {
return logger.handleFunc
}
func (logger *StarLogger) Flush() error {
logger.mu.Lock()
defer logger.mu.Unlock()
logger.write(logger.logbuf)
logger.logbuf = ""
return nil
}
func (logger *StarLogger) Close() error {
logger.stopChan <- 1
logmaps.Range(func(k, v interface{}) bool {
if v.(*StarLogger) == logger {
logmaps.Delete(k)
return false
}
return true
})
logger = nil
return nil
}
func (logger *StarLogger) runTransfer() {
if logger.handleFunc == nil {
select {
case <-logger.stopChan:
return
case <-logger.setChan:
}
}
for {
select {
case <-logger.stopChan:
return
default:
}
if logger.handleFunc == nil {
time.Sleep(time.Millisecond * 100)
continue
}
poped := logger.stacks.MustPop()
if poped == nil {
time.Sleep(time.Millisecond * 20)
continue
}
val := poped.(logTran)
logger.handleFunc(val.color, val.log)
}
}
func NewFlag() *StarLogger {
return Std.NewFlag()
}
func (logger *StarLogger) NewFlag() *StarLogger {
logger2 := new(StarLogger)
logger2.out = logger.out
logger2.ShowThread = logger.ShowThread
logger2.ShowFunc = logger.ShowFunc
logger2.DoShow = logger.DoShow
logger2.ShowLevel = logger.ShowLevel
logger2.ShowLine = logger.ShowLine
logger2.DoWrite = logger.DoWrite
logger2.outshow = logger.outshow
logger2.mu = logger.mu
logger2.lock = logger.lock
logger2.Flag = string([]byte{uint8(rand.Intn(26) + 65), uint8(rand.Intn(26) + 65), uint8(rand.Intn(26) + 65)})
logger2.stopChan, logger2.setChan = make(chan int), make(chan int)
logger2.handleFunc = logger.handleFunc
go logger2.runTransfer()
if logger2.Flag == "MAN" {
logger2.Flag = "RYZ"
}
logmaps.Store(fmt.Sprint(time.Now().UnixNano()), logger2)
return logger2
}
func (logger *StarLogger) write(logstr string) {
if (!logger.DoWrite) || (logger.out == nil) {
return
}
i := 0
for logger.switching {
time.Sleep(time.Millisecond * 100)
i++
if i > 20 {
return
}
}
if &(logger.out) == nil {
return
}
logger.out.Write([]byte(logstr))
}
func (logger *StarLogger) OutPut(logstr string) {
logger.out.Write([]byte(logstr))
}
func (logger *StarLogger) output(level int, logstr string) {
var logs string
var skip int = 2
logger.mu.Lock()
defer logger.mu.Unlock()
if level < logger.LogLevel {
return
}
if logger.isStd {
skip++
}
logger.isStd = false
pc, fname, line, ok := runtime.Caller(skip)
if !ok {
return
}
funcname := runtime.FuncForPC(pc).Name()
funcname = filepath.Ext(funcname)
funcname = strings.TrimPrefix(funcname, ".")
fname = filepath.Base(fname)
date := time.Now().Format("2006-01-02 15:04:05.000 Mon")
logs = date
if logger.ShowLine {
logs += " " + fname + ":" + strconv.Itoa(line)
}
if logger.ShowFunc {
logs += " <" + funcname + ">"
}
if logger.ShowThread {
logs += " |" + logger.Flag + "|"
}
if logger.ShowLevel {
logs += " " + `[` + levels[level] + `]`
}
logs += " " + logstr
if logger.DoShow {
logcolor := NewColor(Colors[level]...)
logcolor.Fprint(logger.outshow, logs)
}
if logger.handleFunc != nil {
//logger.handleFunc(Colors[level], logs)
logger.stacks.Push(logTran{Colors[level], logs})
}
if logger.DoWrite {
if logger.BufSize == 0 {
logger.write(logs)
} else {
logger.logbuf += logs
if len(logger.logbuf) >= logger.BufSize {
logger.write(logs)
}
}
}
}
func (logger *StarLogger) Debug(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.output(LvDebug, strs)
}
func Debug(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Debug(str...)
func (logger *StarLogger) SetShowColor(val bool) {
logger.logcore.showColor = val
}
func (logger *StarLogger) Debugf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.output(LvDebug, strs)
func (logger *StarLogger) GetShowColor() bool {
return logger.logcore.showColor
}
func Debugf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Debugf(format, str...)
func (logger *StarLogger) SetLevelColor(level int, color []Attr) {
logger.logcore.colorList[level] = color
logger.logcore.colorMe[level] = NewColor(color...)
}
func (logger *StarLogger) Debugln(str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.output(LvDebug, strs)
func (logger *StarLogger) GetLevelColor(level int) []Attr {
return logger.logcore.colorList[level]
}
func Debugln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Debugln(str...)
func (logger *StarLogger) SetWriter(wr io.Writer) {
logger.logcore.output = wr
}
func (logger *StarLogger) Info(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.output(LvInfo, strs)
func (logger *StarLogger) GetWriter() io.Writer {
return logger.logcore.output
}
func Info(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Info(str...)
func (logger *StarLogger) SetHandler(f func([]Attr, string)) {
logger.handlerFunc = f
}
func (logger *StarLogger) Infof(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.output(LvInfo, strs)
func (logger *StarLogger) GetHandler() func([]Attr, string) {
return logger.handlerFunc
}
func Infof(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Infof(format, str...)
func (logger *StarLogger) SetSwitching(sw bool) {
logger.logcore.switching = sw
}
func (logger *StarLogger) Infoln(str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.output(LvInfo, strs)
func (logger *StarLogger) SetShowOriginFile(val bool) {
logger.logcore.showDeatilFile = val
}
func Infoln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Infoln(str...)
func (logger *StarLogger) GetShowOriginFile() bool {
return logger.logcore.showDeatilFile
}
func (logger *StarLogger) Notice(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.output(LvNotice, strs)
func (logger *StarLogger) SetShowFuncName(val bool) {
logger.logcore.showFuncName = val
}
func Notice(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Notice(str...)
func (logger *StarLogger) GetShowFuncName() bool {
return logger.logcore.showFuncName
}
func (logger *StarLogger) Noticef(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.output(LvNotice, strs)
func (logger *StarLogger) SetShowLevel(val bool) {
logger.logcore.showLevel = val
}
func Noticef(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Noticef(format, str...)
func (logger *StarLogger) GetShowLevel() bool {
return logger.logcore.showLevel
}
func (logger *StarLogger) Noticeln(str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.output(LvNotice, strs)
func (logger *StarLogger) SetShowFlag(val bool) {
logger.logcore.showThread = val
}
func Noticeln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Noticeln(str...)
func (logger *StarLogger) GetShowFlag() bool {
return logger.logcore.showThread
}
func (logger *StarLogger) Warning(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.output(LvWarning, strs)
func (logger *StarLogger) SetShowStd(val bool) {
logger.logcore.showStd = val
}
func Warning(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Warning(str...)
func (logger *StarLogger) GetShowStd() bool {
return logger.logcore.showStd
}
func (logger *StarLogger) Warningf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.output(LvWarning, strs)
func (logger *StarLogger) StopWrite() {
logger.logcore.stopWriter = true
}
func Warningf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Warningf(format, str...)
func (logger *StarLogger) EnbaleWrite() {
logger.logcore.stopWriter = false
}
func (logger *StarLogger) Warningln(str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.output(LvWarning, strs)
func (logger *StarLogger) IsWriteStoed() bool {
return logger.logcore.stopWriter
}
func Warningln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Warningln(str...)
}
func (logger *StarLogger) Error(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.output(LvError, strs)
}
func Error(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Error(str...)
}
func (logger *StarLogger) Errorf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.output(LvError, strs)
}
func Errorf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Errorf(format, str...)
}
func (logger *StarLogger) Errorln(str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.output(LvError, strs)
}
func Errorln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Errorln(str...)
}
func (logger *StarLogger) Critical(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.output(LvCritical, strs)
}
func Critical(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Critical(str...)
}
func (logger *StarLogger) Criticalf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.output(LvCritical, strs)
}
func Criticalf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Criticalf(format, str...)
}
func (logger *StarLogger) Criticalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.output(LvCritical, strs)
}
func Criticalln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Criticalln(str...)
}
func (logger *StarLogger) Fatal(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.output(LvFatal, strs)
os.Exit(9)
}
func Fatal(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Fatal(str...)
}
func (logger *StarLogger) Fatalf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.output(LvFatal, strs)
os.Exit(9)
}
func Fatalf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Fatalf(format, str...)
}
func (logger *StarLogger) Fatalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.output(LvFatal, strs)
os.Exit(9)
}
func Fatalln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Fatalln(str...)
}
func (logger *StarLogger) Panic(str ...interface{}) {
strs := fmt.Sprint(str...)
logger.output(LvPanic, strs)
panic(str)
}
func Panic(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Panic(str...)
func (logger *StarLogger) Debug(str ...interface{}) {
logger.logcore.Debug(logger.thread, logger.isStd, logger.handlerFunc, str...)
}
func (logger *StarLogger) Panicf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
logger.output(LvPanic, strs)
panic(fmt.Sprintf(format, str...))
func (logger *StarLogger) Debugf(format string, str ...interface{}) {
logger.logcore.Debugf(logger.thread, logger.isStd, logger.handlerFunc, format, str...)
}
func Panicf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Panicf(format, str...)
func (logger *StarLogger) Debugln(str ...interface{}) {
logger.logcore.Debugln(logger.thread, logger.isStd, logger.handlerFunc, str...)
}
func (logger *StarLogger) Panicln(str ...interface{}) {
strs := fmt.Sprintln(str...)
logger.output(LvPanic, strs)
panic(fmt.Sprintln(str...))
func (logger *StarLogger) Info(str ...interface{}) {
logger.logcore.Info(logger.thread, logger.isStd, logger.handlerFunc, str...)
}
func Panicln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Panicln(str...)
func (logger *StarLogger) Infof(format string, str ...interface{}) {
logger.logcore.Infof(logger.thread, logger.isStd, logger.handlerFunc, format, str...)
}
func (logger *StarLogger) Print(str ...interface{}) {
logger.lock.Lock()
defer logger.lock.Unlock()
strs := fmt.Sprint(str...)
logger.OutPut(strs)
func (logger *StarLogger) Infoln(str ...interface{}) {
logger.logcore.Infoln(logger.thread, logger.isStd, logger.handlerFunc, str...)
}
func Print(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Print(str...)
func (logger *StarLogger) Notice(str ...interface{}) {
logger.logcore.Notice(logger.thread, logger.isStd, logger.handlerFunc, str...)
}
func (logger *StarLogger) Printf(format string, str ...interface{}) {
logger.lock.Lock()
defer logger.lock.Unlock()
strs := fmt.Sprintf(format, str...)
logger.OutPut(strs)
func (logger *StarLogger) Noticef(format string, str ...interface{}) {
logger.logcore.Noticef(logger.thread, logger.isStd, logger.handlerFunc, format, str...)
}
func Printf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true
Std.Printf(format, str...)
func (logger *StarLogger) Noticeln(str ...interface{}) {
logger.logcore.Noticeln(logger.thread, logger.isStd, logger.handlerFunc, str...)
}
func (logger *StarLogger) Println(str ...interface{}) {
logger.lock.Lock()
defer logger.lock.Unlock()
strs := fmt.Sprintln(str...)