golang版的高效日志组件

前端之家收集整理的这篇文章主要介绍了golang版的高效日志组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package log import ( "sync" "time" "os" "fmt" "strconv" ) type Tlog struct{ Qidx int Q1 []string Q2 []string Qlen int Q1len int Q2len int Locker sync.Mutex SavePath string FilePrefix string WriteFileInterval time.Duration StopMark chan int TimeoutChan chan int } func NewTlog(queue_len int,write_file_interval time.Duration,save_path,file_prefix string) *Tlog{ ret := new(Tlog) ret.Q1 = make([]string,queue_len,queue_len) ret.Q2 = make([]string,queue_len) ret.Qidx = 1 ret.Qlen = queue_len ret.Q1len = 0 ret.Q2len = 0 ret.SavePath = IncludeTrailingPathDelimiter(save_path) ret.FilePrefix = file_prefix ret.WriteFileInterval = write_file_interval ret.StopMark = make(chan int,1) ret.TimeoutChan = make(chan int,1) return ret } func (log *Tlog) Stop(){ log.StopMark <- 1 } func (log *Tlog) start_timer(){ for{ time.Sleep(log.WriteFileInterval * time.Millisecond) log.TimeoutChan <- 1 //fmt.Println("log timer passed") } } func(log *Tlog) internal_run(){ go log.start_timer() for{ select{ case <- log.StopMark: goto end case <- log.TimeoutChan:{ log.writefile(true) } } } end: log.writefile(true) } func (log *Tlog) Run(){ go log.internal_run() } func (log *Tlog)swap(){ log.writefile(false) if log.Qidx == 1{ log.Qidx = 2 }else{ log.Qidx = 1 } } func (log *Tlog)writefile(with_lock bool){ if with_lock{ log.Locker.Lock() defer log.Locker.Unlock() } if log.Qidx == 1 && log.Q1len == 0{ return } if log.Qidx == 2 && log.Q2len == 0{ return } file := log.SavePath + log.FilePrefix + time.Now().String()[:10] var f *os.File var e error if FileExists(file){ f,e = os.OpenFile(file,os.O_APPEND | os.O_RDWR,0666) }else{ f,os.O_CREATE | os.O_RDWR,0666) } if e!=nil{ fmt.Println("open/create log file error:"+file+","+e.Error()) return } defer f.Close() var buf string if log.Qidx == 1{ for i:=0;i<log.Q1len;i++{ buf = buf + "\r\n" + log.Q1[i] } }else{ for i:=0;i<log.Q2len;i++{ buf = buf + "\r\n" + log.Q2[i] } } _,err := f.Write([]byte(buf)) if err!=nil{ fmt.Println("write log file error:"+file+","+e.Error()) return } if log.Qidx == 1{ log.Q1len = 0 }else{ log.Q2len = 0 } } func (log *Tlog)Log(s string){ if s[:4] == "err:"{ fmt.Println(s) } log.Locker.Lock() defer log.Locker.Unlock() if log.Qidx==1{ if log.Q1len >= log.Qlen{ log.swap() } }else{ if log.Q2len >= log.Qlen{ log.swap() } } //s1 := time.Now().String() slog :=time.Now().String() + " " + s if log.Qidx==1{ log.Q1len++ log.Q1[log.Q1len - 1] = slog }else{ log.Q2len++ log.Q2[log.Q2len - 1] = slog } } func testlog(log *Tlog){ for i:=0;i<10000;i++{ log.Log(strconv.Itoa(i)) if i % 1000 ==0{ time.Sleep(1000 * time.Millisecond) } } } func TestLog(){ log := NewTlog(100,1000,"/usr/fys/","log") log.Run() go testlog(log) go testlog(log) time.Sleep(5000 * time.Millisecond) log.Stop() } 原文链接:https://www.f2er.com/go/190002.html

猜你在找的Go相关文章