golang版的高效日志组件

truexf · · 2297 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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,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,e = os.OpenFile(file,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()
}

有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:truexf

查看原文:golang版的高效日志组件

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2297 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传