Skip to content

xuexihuang/new_log15

Repository files navigation

log15

一个go日志库,支持文件和控制台输出,支持打印日志的文件行和文件名称,支持多颜色显示,支持多级日志分级,支持按时间 分割日志,支持按大小分割日志

For more information, you can access the Source Link

Add new features:

  • Filter log level when writing into Log File .
  • Rotating logfile automaticly when logfile size larger than setting or default. (eg: > 100M)
  • You can rotate logfile whenever you want by using func "Rotate()"

Usage:

Importing

import log "github.com/xuexihuang/new_log15"

Examples

1: Basic usage

func main() {

    h,_ := log.FileHandler("./app.log", log.LogfmtFormat())
    log.Root().SetHandler(h)

    Path := "http://mytest.com"
    for i:=1; i < 1000000 ; i++ {
        log.Info("page accessed", "path", Path, "user_id", i)
        time.Sleep(50 * time.Millisecond)
    }
}

Will result in output that looks like this:

t=2017-09-26T01:40:37-0400 lvl=info msg="page accessed" path=http://mytest.com user_id=21
t=2017-09-26T01:40:37-0400 lvl=info msg="page accessed" path=http://mytest.com user_id=22

2: Set LogLevel filter

func main() {
    // set level to Warn
    log.SetOutLevel(log.LvlWarn)

    h,_ := log.FileHandler("./app.log", log.LogfmtFormat())
    log.Root().SetHandler(h)

    Path := "http://mytest.com"
    for i:=1; i < 1000000 ; i++ {

        // Info is large than Warn, output none
        log.Info("page accessed", "path", Path, "user_id", i)
        time.Sleep(50 * time.Millisecond)
    }
}

Will output nothing.

Log level define: (from small to large)
  • LvlCrit
  • LvlError
  • LvlWarn
  • LvlInfo
  • LvlDebug

3: Modify default rotate parameters

func main() {
    // set rotate parameters:
    // size:1m, keep 10days, backup 5 files, uncompress when rotate
    log.SetRotatePara(1,10,5,false)

    h,_ := log.FileHandler("./app.log", log.LogfmtFormat())
    log.Root().SetHandler(h)

    Path := "http://mytest.com"
    for i:=1; i < 1000000 ; i++ {
        log.Info("page accessed", "path", Path, "user_id", i)
    }

Will result in output that looks like this:

log15]$ ls -l
total 8664
-rw-r--r--. 1 work work 1048524 Sep 26 02:43 app-2017-09-26T02-43-12.826.log
-rw-r--r--. 1 work work 1048524 Sep 26 02:43 app-2017-09-26T02-43-12.890.log
-rw-r--r--. 1 work work 1048524 Sep 26 02:43 app-2017-09-26T02-43-12.941.log
-rw-r--r--. 1 work work 1048524 Sep 26 02:43 app-2017-09-26T02-43-13.006.log
-rw-r--r--. 1 work work 1048524 Sep 26 02:43 app-2017-09-26T02-43-13.068.log
-rw-r--r--. 1 work work  667368 Sep 26 02:43 app.log

func "SetRotatePara()" define:

SetRotatePara(maxsize, maxage, maxbackup int, compress bool)

The default rotate parameters:

  • maxsize : 100 // 100M
  • maxage: 10 // days
  • maxbackup: 30 // files
  • compress: true

4: Force logfile rotate

func main() {
    h,_ := log.FileHandler("./app.log", log.LogfmtFormat())
    log.Root().SetHandler(h)

    go func(){
      for {
          time.Sleep( 1 * time.Second)
          log.LogRotate()
      }
    }()

    Path := "http://mytest.com"
    for i:=1; i < 1000000 ; i++ {
        log.Info("page accessed", "path", Path, "user_id", i)
        time.Sleep(50 * time.Millisecond)
    }
}

Will result in output that looks like this:

log15]$ ls -l
total 2924
-rw-r--r--. 1 work work     201 Sep 26 03:08 app-2017-09-26T03-08-59.134.log.gz
-rw-r--r--. 1 work work     171 Sep 26 03:09 app-2017-09-26T03-09-00.135.log.gz
-rw-r--r--. 1 work work     174 Sep 26 03:09 app-2017-09-26T03-09-01.136.log.gz
-rw-r--r--. 1 work work     171 Sep 26 03:09 app-2017-09-26T03-09-02.138.log.gz
-rw-r--r--. 1 work work     616 Sep 26 03:09 app.log

every second rotate once.

License

Apache

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages