Efficient log components for version Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. 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 (Time.millisecond)
}

}
}


Func Testlog () {
LOG: = Newtlog (100,1000, "/usr/fys/", "Log")
Log. Run ()
Go Testlog (log)
Go Testlog (log)
Time. Sleep (Time.millisecond)
Log. Stop ()
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.