DEV Community

suganoo
suganoo

Posted on

Go Gologger

#go

Now I am developing gologger as logger for golang.
Github repository is here.
https://github.com/suganoo/gologger

Sample code

Here is sample code of how to use gologger.

go get -u github.com/suganoo/gologger

gologgerSample.go

package main

import (
    "github.com/suganoo/gologger"
)

var glog *gologger.Gologger

func hogeFunc() {
    glog.Debug("this is debug hogeFunc")
    glog.Info("call hogeFunc")
}

func main() {
    glog = gologger.NewGologger(gologger.Configuration{Logfile : "./testlog.log"})
    defer glog.CloseFile()

    msg := "hogehoge"
    glog.Debug("this is debug")   // default debug is muted
    glog.Info("this is info")
    glog.Info("msg : " + msg)
    glog.Warning("this is warning")
    glog.Error("this is Error")

    glog.UnmuteDebug()
    hogeFunc()

    glog.Debug("this is debug xxx")
    glog.MuteDebug()
    glog.Debug("this is debug yyy")  // this debug message is muted
}
go run gologgerSample.go

cat testlog.log
2018-02-21T10:07:44.277+09:00   INFO    hoge.sever  3892    gid:1   fuga-user   1.0.0   this is info    main    [gologgerSample.go:18]
2018-02-21T10:07:44.277+09:00   INFO    hoge.sever  3892    gid:1   fuga-user   1.0.0   msg : hogehoge  main    [gologgerSample.go:19]
2018-02-21T10:07:44.277+09:00   WARNING hoge.sever  3892    gid:1   fuga-user   1.0.0   this is warning main    [gologgerSample.go:20]
2018-02-21T10:07:44.277+09:00   ERROR   hoge.sever  3892    gid:1   fuga-user   1.0.0   this is Error   main    [gologgerSample.go:21]
2018-02-21T10:07:44.277+09:00   DEBUG   hoge.sever  3892    gid:1   fuga-user   1.0.0   this is debug hogeFunc  hogeFunc    [gologgerSample.go:8]
2018-02-21T10:07:44.277+09:00   INFO    hoge.sever  3892    gid:1   fuga-user   1.0.0   call hogeFunc   hogeFunc    [gologgerSample.go:9]
2018-02-21T10:07:44.277+09:00   DEBUG   hoge.sever  3892    gid:1   fuga-user   1.0.0   this is debug xxx   main    [gologgerSample.go:26]

Features

  • You can get variety of log items, such as line number of file, goroutine number and so on.
  • You can write any types of log messages, such as int, string, or struct, in gologger function (Info(), Warning()...).

If you have any interest on gologger, it would be great to try it.
You can find more information in gologger repository.

Thank you.

Top comments (0)