This is a creation in Article, where the information may have evolved or changed.
Go Package log:
- Support archive output, one-hour compressed archive one copy
- Keep logs for up to three days
- Supports log level customization
- If the output file is not specified by default output to the console.
- Support output file name line number, and time, log bounds
such as: info 10:08:40.826836 handler.go:81
Package Loggerimport ("FMT" "Log" "OS" "Os/exec" "Strings" "Time") const (Paniclevel int = Iota Fatallevel ErrorLevel warnlevel infolevel debuglevel) type LogFile struct {level int logtime int64 fileName string filefd *os. File}var logFile logfilefunc Config (LogFolder string, level int) {LogFile. FileName= LogFolder LogFile. level= Level Log. Setoutput(logFile) Log. SetFlags(Log. Lmicroseconds| Log. Lshortfile)}func SetLevel (level int) {LogFile. level= Level}func DEBUGF (format string, args:. Interface{}) {if logFile. level>= DebugLevel {Log. Setprefix("Debug") Log. Output(2Fmt. Sprintf(format, args ...)) }}func infof (format string, args:. Interface{}) {if logFile. level>= Infolevel {Log. Setprefix("Info") Log. Output(2Fmt. Sprintf(format, args ...)) }}func WARNF (format string, args:. Interface{}) {if logFile. level>= Warnlevel {Log. Setprefix("Warn") Log. Output(2Fmt. Sprintf(format, args ...)) }}func Errorf (format string, args:. Interface{}) {if logFile. level>= ErrorLevel {Log. Setprefix("Error") Log. Output(2Fmt. Sprintf(format, args ...)) }}func fatalf (format string, args:. Interface{}) {if logFile. level>= Fatallevel {Log. Setprefix("Fatal") Log. Output(2Fmt. Sprintf(format, args ...)) }}func (Me LogFile) Write (buf []byte) (n int, err error) {if me. FileName==""{FMT. Printf("Consol:%s", buf) return Len (BUF), nil} if LogFile. LogTime+3600< time. now(). Unix() {LogFile. Createlogfile() LogFile. LogTime= Time. now(). Unix()} If LogFile. FILEFD= = Nil {return len (BUF), nil} return LogFile. FILEFD. Write(BUF)} Func (Me *logfile) Createlogfile () {logdir: ="./"If index: = Strings. LastIndex(Me. FileName,"/"); Index! =-1 {LogDir = Me. FileName[0: index] +"/"Os. Mkdirall(Me. FileName[0: index], OS. Modeperm)} Now: = Time. now() FileName: = FMT. Sprintf("%s_%04d%02d%02d_%02d%02d", ME. FileName, now. year(), now. Month(), now. Day(), now. Hour(), now. Minute()) If err: = OS. Rename(Me. FileName, filename)Err = = Nil {Go func () {tarcmd: = exec. Command("Tar","-ZCF", filename+". tar.gz", filename,"--remove-files") Tarcmd. Run() Rmcmd: = Exec. Command("/bin/sh","-C","Find"+logdir+ '-type F-mtime +2-exec RM {} \;`)Rmcmd. Run()} ()} for index: =0; index < index++ {If FD, err: = OS. OpenFile(Me. FileName, OS. O_create|os. O_append|os. O_wronly, OS. Modeexclusive)Nil = = Err {Me. FILEFD. Sync() Me. FILEFD. Close() Me. FILEFD= FD Break} Me. FILEFD= nil}}