Logrus mate is a tool for Logrus, it will help you to initial logger by config, including Formatter, Hook,Level, Output and Environments.
Example 1:
Using internal default logrus mate:
package main
import (
"github.com/gooops/logrus_mate"
)
func main() {
logrus_mate.Logger().Infoln("Using internal defualt logurs mate")
}Example 2:
Create new logger:
package main
import (
"github.com/gooops/logrus_mate"
)
func main() {
loggerConf := logrus_mate.LoggerConfig{
Level: "info",
Formatter: logrus_mate.FormatterConfig{
Name: "json",
},
}
// package level
if jackLogger, err := logrus_mate.NewLogger("jack", loggerConf); err != nil {
return
} else {
jackLogger.Infoln("hello logurs")
}
// so, the jack added into internal logurs mate
logrus_mate.Logger("jack").Debugln("not print")
logrus_mate.Logger("jack").Infoln("Hello, I am A Logger from jack")
}package log
import (
"os"
"github.com/Sirupsen/logrus"
"github.com/gooops/logrus_mate"
_ "github.com/gooops/logrus_mate/hooks/file"
)
func New(configfile string) (*logrus.Logger, error) {
if _, err := os.Stat(configfile); err != nil {
return nil, err
}
if mateConf, err := logrus_mate.LoadLogrusMateConfig(configfile); err != nil {
return nil, err
} else {
if newMate, err := logrus_mate.NewLogrusMate(mateConf); err != nil {
return nil, err
} else {
return newMate.Logger("mike"), nil
}
}
}
Example 3:
Create logurs mate from config file (also you could fill config struct manually):
export RUN_MODE=productionmate.conf
{
"env_keys": {
"run_env": "RUN_MODE"
},
"loggers": [{
"name": "mike",
"config": {
"production": {
"out": {
"name": "stderr",
"options":{}
},
"level": "error",
"formatter": {
"name": "json"
},
"hooks": [{
"name": "syslog",
"options": {
"network": "udp",
"address": "localhost:514",
"priority": "LOG_ERR",
"tag": ""
}
}]
}
}
}]
}mate.yml
---
env_keys:
run_env: RUN_MODE
loggers:
- name: mike
config:
production:
out:
name: stderr
options: {}
level: error
formatter:
name: json
hooks:
- name: syslog
options:
network: udp
address: localhost:514
priority: LOG_ERR
tag: ''file模式完整配置
---
env_keys:
run_env: RUN_MODE
loggers:
- name: mike
config:
production:
out:
name: stdout
options: {}
level: debug
formatter:
name: text
options:
force_colors: true
disable_colors: false
disable_timestamp: false
full_timestamp: true
timestamp_format: '2006-01-02 15:04:05'
disable_sorting: true
hooks:
- name: file
options:
filename: "/tmp/test.log"
# maxlines: 1000
# maxsize: 10240
daily: true
maxdays: 1
rotate: true
level: 3
development:
out:
name: stdout
options: {}
level: debug
formatter:
name: text
options:
force_colors: true
disable_colors: false
disable_timestamp: false
full_timestamp: true
timestamp_format: '2006-01-02 15:04:05'
disable_sorting: false
package main
import (
"github.com/gooops/logrus_mate"
_ "github.com/gooops/logrus_mate/hooks/syslog"
)
func main() {
if mateConf, err := logrus_mate.LoadLogrusMateConfig("mate.conf"); err != nil {
return
} else {
if newMate, err := logrus_mate.NewLogrusMate(mateConf); err != nil {
return
} else {
newMate.Logger("mike").Errorln("I am mike in new logrus mate")
}
}
}In this example, we used the syslog hook, so we should import package of syslog
import _ "github.com/gooops/logrus_mate/hooks/syslog"logrus mate support environments notion, same logger could have different environment config, the above mate.conf only have production config, so if RUN_MODE is production, it will use this section's options, or else, there have no loggers generate.
different environment could have own level, hooks and formatters, logrus mate have Environments config for create the instance, you can see the above config file of mate.conf
type Environments struct {
RunEnv string `json:"run_env" yaml:"run_env"`
}run_env: this filed is the key of run env, it will get actual value from environment by this key.
The json config file will be compile with package of
gogap/env_jsonwhile you use funclogrus_mate.LoadLogrusMateConfig, please forward to the project of env_json to known more details.
| Hook | Options |
|---|---|
| Airbrake | project_id api_key env |
| Syslog | network address priority tag |
| BugSnag | api_key |
| Slackrus | url levels channel emoji username |
| Graylog | address facility extra |
app_name host port from to username password |
When we need use above hooks, we need import these package as follow:
import _ "github.com/gooops/logrus_mate/hooks/syslog"
import _ "github.com/gooops/logrus_mate/hooks/mail"If you want write your own hook, you just need todo as follow:
package myhook
import (
"github.com/gooops/logrus_mate"
)
type MyHookConfig struct {
Address string `json:"address" yaml:"address"`
}
func init() {
logrus_mate.RegisterHook("myhook", NewMyHook)
}
func NewMyHook(options logrus_mate.Options) (hook logrus.Hook, err error) {
conf := MyHookConfig{}
if err = options.ToObject(&conf); err != nil {
return
}
// write your hook logic code here
return
}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"} |
3rd formatters:
| Formatter | Output Example |
|---|---|
| logstash |
When we need use 3rd formatter, we need import these package as follow:
import _ "github.com/gooops/logrus_mate/formatters/logstash"If you want write your own formatter, you just need todo as follow:
package myformatter
import (
"github.com/gooops/logrus_mate"
)
type MyFormatterConfig struct {
Address string `json:"address" yaml:"address"`
}
func init() {
logrus_mate.RegisterFormatter("myformatter", NewMyFormatter)
}
func NewMyFormatter(options logrus_mate.Options) (formatter logrus.Formatter, err error) {
conf := MyFormatterConfig{}
if err = options.ToObject(&conf); err != nil {
return
}
// write your formatter logic code here
return
}internal writers (output):
- stdout
- stderr
- null
3rd writers:
| Writer | Description |
|---|---|
| redisio | just for demo, it will output into redis, the key type is list |
When we need use 3rd writer, we need import these package as follow:
import _ "github.com/gooops/logrus_mate/writers/redisio"If you want write your own writer, you just need todo as follow:
package mywriter
import (
"io"
"github.com/gooops/logrus_mate"
)
type MyWriterConfig struct {
Address string `json:"address" yaml:"address"`
}
func init() {
logrus_mate.RegisterWriter("mywriter", NewMyWriter)
}
func NewMyWriter(options logrus_mate.Options) (writer io.Writer, err error) {
conf := MyWriterConfig{}
if err = options.ToObject(&conf); err != nil {
return
}
// write your writer logic code here
return
}