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";