Go Language Code Automatic Update server code sharing

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Logimport ("io" "Log" "OS" "Runtime" "Time") type L struct {logs *log. Loggerlevel Intio. Closer}func Newlog (Httplogpath string, level int) *l {file, err: = OS. OpenFile (Httplogpath, OS. O_create|os. O_RDWR, 0644) if err! = Nil {log. Println ("Error", err) log. Println ("Error", "Log output to standard output.") return Nil}var Log *log. Logger = log. New (OS. Stdout, now (), 0) if file! = Nil {log = log. New (file, now (), 0) go flushlogfile (file)}file. Seek (0, 2) return &l{log, Level, File}}func now () string {return time. Now (). Format ("2006-01-02 15:04:05")}func flushlogfile (File *os. File) {For _ = range time. Newticker (* time. Second). C {if File = nil {return}file.sync ()}}func (self *l) setloglevel (level int) {if level > 4 {return}self.level = Level}f UNC (self *l) Print (v ... interface{}) {Self.logs.Print (v)}func (self *l) Printf (Formate string, v. interface{}) {Self.lo Gs. Printf (formate, v)}func (self *l) Println (v ... interface{}) {self.logs.Println (v)}func (self *l) Printfi (formate string, V ... interface{}) {ifSelf.level > 1 {return}self.logs.printf ("info->" +formate, v ...)} Func (self *l) PRINTFW (Formate string, v. ... interface{}) {if Self.level > 2 {return}self.logs.printf ("warn->" +forma Te, v ...)} Func (self *l) PRINTFE (Formate string, v. ... interface{}) {if Self.level > 3 {return}self.logs.printf ("error->" +form Ate, V ...)} Func (self *l) PRINTFF (Formate string, v. ... interface{}) {if Self.level > 4 {return}self.logs.fatalf ("fatal->" +form Ate, V ...)} Func (self *l) infoprintf (callers int, formate string, v ... interface{}) {_, file, line, OK: = runtime. Caller (callers + 1) if!ok {return}self.logs.printf ("file->%s line->%d\n", File, line) self.logs.Printf (formate, V ...)} <pre name= "code" class= "HTML" >
<pre style= "margin-top:0px; margin-bottom:0px; " ><span style= "color: #0000ff;" >package</span><span style= "color: #f57900;" > </span>main
Import"Centerserver/log"
FuncMain (){
 l: =log. Newlog ("Http.log",0)
 l.infoprintf (0,"%s\n","error here ↑")
 L.printfi ("%s\n","Info")
 L.PRINTFW ("%s\n","Warn")
 L.printfe ("%s\n","Error")
 L.PRINTFF ("%s\n","Fatal")
}

</pre ><pre name= "code" class= "HTML"; 

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.