概述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 WriteFileI 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() } 总结
以上是内存溢出为你收集整理的golang版的高效日志组件全部内容,希望文章能够帮你解决golang版的高效日志组件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)