LoginSignup
3
5

More than 5 years have passed since last update.

golangのloggerを作ってみた

Last updated at Posted at 2017-02-03

go-easylog

golangでlogを吐くにあたって便利そうなパッケージは世の中たくさんあるのですが、個人的に多機能なものより、さくっと使っておきたいときに使える小回りのきくものが欲しいのですが、見つけられませんでした。

  • とりあえず、すぐ使えて、すぐ出力できる。
  • そのために初期設定をほとんど必要としない。
  • ログレベルが設定できる
  • ロガーインスタンスの取り回しを気にしないで良い
  • しっかりスレッドセーフ
  • 運用レベルになってもローテーション回すくらいはできる

el自体が出せる情報は時間とエラーレベルのみですが、"github.com/pkg/errors"の出力に対応しており、el.SetDebug()を呼び出しておけば、error型に対してラップされた情報を出すことができます。

Install

$ go get github.com/go-easylog/el

Example


package main

import "github.com/go-easylog/el"

func main() {

    // Default output level is "WARN"
    el.Warn("Output warning")
    el.Trace("This comment is don't output to log")

    // Set level for log
    el.SetLogLevel(el.TRACE)

    el.Info("This will be outputed")

    if err := el.SetRotateLog("./%Y/%M/%D.log"); err != nil {
        panic(err)
    }
    // %Y <- year  (YYYY)
    // %M <- month (MM)
    // %D <- day   (DD)
    //
    // "./prefix-%Y-%M.log"  <---  It is possible to specify like this
    //                             This pattern in one month's rotation


    el.Trace("this comment outputing to file")


    // no rotate pattern
    if err := el.SetRotateLog("./no-rotate.log"); err != nil {
        panic(err)
    }

    el.Error("this file is no rotate")
    el.Fatal("error exit") // <- Forced kill here

    el.Trace("this text is no outputed") // <- This is not output
}
ターミナル出力
$ go run main.go
2017-02-04 04:08:03 [WARN] Output warning
2017-02-04 04:08:03 [INFO] This will be outputed
2017-02-04 04:08:03 [FATAL] error exit
exit status 255
2017/02/04.log
2017-02-04 04:08:03 [TRACE] this comment outputing to file
no-rotate.log
2017-02-04 04:08:03 [ERROR] this file is no rotate

Example 2

( github.com/pkg/errors 対応 )

main.go
package main

import (
    "fmt"

    "github.com/go-easylog/el"
    "github.com/pkg/errors"
)

func ErrorFunc() error {
    return fmt.Errorf("base error")
}

func main() {

    // set debug mode
    el.SetDebug()
    if err := ErrorFunc(); err != nil {
        el.Error(errors.Wrap(err, "Error by ErrorFunc() <-- (1)"))
    }

    // if when release mode
    // not outputs the error details 
    el.SetRelease()
    if err := ErrorFunc(); err != nil {
        el.Error(errors.Wrap(err, "Error by ErrorFunc() <-- (2)"))
    }
}
ターミナル出力
$ go run main.go
2017-02-04 04:34:49 [ERROR] base error
Error by ErrorFunc() <-- (1)
main.main
        /Users/.../exsample_el/main.go:18
runtime.main
        /usr/local/Cellar/go/1.7.4_2/libexec/src/runtime/proc.go:183
runtime.goexit
        /usr/local/Cellar/go/1.7.4_2/libexec/src/runtime/asm_amd64.s:2086
2017-02-04 04:34:49 [ERROR] Error by ErrorFunc() <-- (2): base error

 今後の予定

とはいえファイル名くらいは出せるようにしたい。

フォーマットも設定変えられるようになると好みも反映しやすくて使い易いのかなと思ってたりするので、今後検討してみます。

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5