[Awesome Go] Use logrus_mate package to initiate Logrus logger

logrus_mate

Logrus mate is a tool for Logrus, it will help you to initial logger by config, including Formatter, HookLevel and Output.

Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.

Installation

Standard go get:

1
$ go get -u github.com/gogap/logrus_mate

Example

Example 1:

Hijack logrus.StandardLogger()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// main.go

package main

import (
"github.com/sirupsen/logrus"
"github.com/gogap/logrus_mate"
)

func main() {
logrus_mate.Hijack(
logrus.StandardLogger(),
logrus_mate.ConfigString(
`{formatter.name = "json"}`,
),
)

logrus.WithField("Field", "A").Debugln("Hello JSON")
}

Example 2:

Create new logger from mate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// main.go

package main

import (
"github.com/gogap/logrus_mate"
)

func main() {
mate, _ := logrus_mate.NewLogrusMate(
logrus_mate.ConfigString(
`{ mike {formatter.name = "json"} }`,
),
)

mikeLoger := mate.Logger("mike")
mikeLoger.Errorln("Hello Error Level from Mike and my formatter is json")
}

Example 3:

Hi jack logger by mate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// main.go

package main

import (
"github.com/sirupsen/logrus"
"github.com/gogap/logrus_mate"
)

func main() {
mate, _ := logrus_mate.NewLogrusMate(
logrus_mate.ConfigString(
`{ mike {formatter.name = "json"} }`,
),
)

mate.Hijack(
logrus.StandardLogger(),
"mike",
)

logrus.Println("hello std logger is hijack by mike")
}

Example 4:

Fallback the ConfigString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// main.go

package main

import (
"github.com/sirupsen/logrus"
"github.com/gogap/logrus_mate"
)

func main() {
mate, _ := logrus_mate.NewLogrusMate(
logrus_mate.ConfigString(
`{ mike {formatter.name = "json"} }`,
),
logrus_mate.ConfigFile(
"mate.conf", // { mike {formatter.name = "text"} }
),
)

mate.Hijack(
logrus.StandardLogger(),
"mike",
)

logrus.Println("hello std logger is hijack by mike")
}

the json formatter is used

Example 5:

Fallback config while hijack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// main.go

package main

import (
"github.com/sirupsen/logrus"
"github.com/gogap/logrus_mate"
)

func main() {
mate, _ := logrus_mate.NewLogrusMate(
logrus_mate.ConfigFile(
"mate.conf", // { mike {formatter.name = "text"} }
),
)

mate.Hijack(logrus.StandardLogger(),
"mike",
logrus_mate.ConfigString(
`{formatter.name = "json"}`,
),
)

logrus.Errorln("hello std logger is hijack by mike")
}

Hooks

Hook Options
File filename max-lines max-size daily max-days rotate level

When we need use above hooks, we need import these package as follow:

1
import _ "github.com/gogap/logrus_mate/hooks/file"

If you want write your own hook, you just need todo as follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package myhook

import (
"github.com/gogap/logrus_mate"
)

type MyHookConfig struct {
Address string
}

func init() {
logrus_mate.RegisterHook("myhook", NewMyHook)
}

func NewMyHook(config logrus_mate.Configuration) (hook logrus.Hook, err error) {
conf := MyHookConfig{}
if config!=nil {
conf.Address = config.GetString("address")
}

// write your hook logic code here

return
}

See Hooks - https://github.com/gogap/logrus_mate#hooks to learn more Hooks.

Formatters

Internal formatters:

Formatter Options Output Example
null
text force-colors disable-colors disable-timestamp full-timestamp timestamp-format disable-sorting DEBU[0000] Hello Default Logrus Mate
json timestamp-format {“level”:“info”,“msg”:“Hello, I am A Logger from jack”,“time”:“2015-10-18T21:24:19+08:00”}

If you want write your own formatter, you just need todo as follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package myformatter

import (
"github.com/gogap/logrus_mate"
)

type MyFormatterConfig struct {
Address string `json:"address"`
}

func init() {
logrus_mate.RegisterFormatter("myformatter", NewMyFormatter)
}

func NewMyFormatter(config logrus_mate.Configuration) (formatter logrus.Formatter, err error) {
conf := MyFormatterConfig{}
if config!=nil {
conf.Address=config.GetString("address")
}

// write your formatter logic code here

return
}

See Formatters - https://github.com/gogap/logrus_mate#formatters to learn more Formatters.

Writer

internal writers (output):

  • stdout

  • stderr

  • null

If you want write your own writer, you just need todo as follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package mywriter

import (
"io"

"github.com/gogap/logrus_mate"
)

type MyWriterConfig struct {
Address string `json:"address"`
}

func init() {
logrus_mate.RegisterWriter("mywriter", NewMyWriter)
}

func NewMyWriter(config logrus_mate.Configuration) (writer io.Writer, err error) {
conf := MyWriterConfig{}
if config!=nil {
conf.Address=config.GetString("address")
}

// write your writer logic code here

return
}

See Writer - https://github.com/gogap/logrus_mate#writer to learn more Writer.

Config Provider

The default config provider is HOCON, you could use your own config provider, just implement the following interface{}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
type ConfigurationProvider interface {
LoadConfig(filename string) Configuration
ParseString(cfgStr string) Configuration
}

type Configuration interface {
GetBoolean(path string, defaultVal ...bool) bool
GetByteSize(path string) *big.Int
GetInt32(path string, defaultVal ...int32) int32
GetInt64(path string, defaultVal ...int64) int64
GetString(path string, defaultVal ...string) string
GetFloat32(path string, defaultVal ...float32) float32
GetFloat64(path string, defaultVal ...float64) float64
GetTimeDuration(path string, defaultVal ...time.Duration) time.Duration
GetTimeDurationInfiniteNotAllowed(path string, defaultVal ...time.Duration) time.Duration
GetBooleanList(path string) []bool
GetFloat32List(path string) []float32
GetFloat64List(path string) []float64
GetInt32List(path string) []int32
GetInt64List(path string) []int64
GetByteList(path string) []byte
GetStringList(path string) []string
GetConfig(path string) Configuration
WithFallback(fallback Configuration)
HasPath(path string) bool
Keys() []string
}

set your own config provider

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// main.go

package main

import (
"github.com/gogap/config"
"github.com/gogap/logrus_mate"
"github.com/sirupsen/logrus"
)

func main() {
mate, _ := logrus_mate.NewLogrusMate(
logrus_mate.ConfigString(
`{ mike {formatter.name = "json"} }`,
),
logrus_mate.ConfigFile(
"mate.conf", // { mike {formatter.name = "text"} }
),
logrus_mate.ConfigProvider(
&config.HOCONConfigProvider{}, // this is defualt provider if you did not configurate
),
)

mate.Hijack(
logrus.StandardLogger(),
"mike",
)

logrus.Println("hello std logger is hijack by mike")
}

References

[1] gogap/logrus_mate: tool for logrus, let it easy to use - https://github.com/gogap/logrus_mate

[2] sirupsen/logrus: Structured, pluggable logging for Go. - https://github.com/sirupsen/logrus

[3] go-akka/configuration: typesafe HOCON - https://github.com/go-akka/configuration

[4] config/HOCON.md at master · lightbend/config · GitHub - https://github.com/lightbend/config/blob/master/HOCON.md