logrus_mate

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2017 License: Apache-2.0 Imports: 11 Imported by: 11

README

Logrus Mate

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

Example

Example 1:

Using internal default logrus mate:

package main

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

func main() {
    logrus_mate.Logger().Infoln("Using internal defualt logurs mate")
}

Example 2:

Create new logger:

package main

import (
    "github.com/gogap/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")
}

Example 3:

Create logurs mate from config file (also you could fill config struct manually):

export RUN_MODE=production

mate.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": ""
                    }
                }]
            }
        }
    }]
}
package main

import (    
    "github.com/gogap/logrus_mate"
    _ "github.com/gogap/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/gogap/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.

Environments

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"`
}

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_json while you use func logrus_mate.LoadLogrusMateConfig, please forward to the project of env_json to known more details.

Hooks
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
Mail app_name host port from to username password
Logstash app_name protocol address always_sent_fields prefix
Stack-Hook caller-level stack-level from 0 to 5 (panic, fatal, error, warning, info, debug); warning: put in first
file filename maxdays rotate daily maxsize maxlines level from 0 to 5 (panic, fatal, error, warning, info, debug)
file use formatter filename maxdays rotate daily maxsize maxlines level from 0 to 5 (panic, fatal, error, warning, info, debug)

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

import _ "github.com/gogap/logrus_mate/hooks/syslog"
import _ "github.com/gogap/logrus_mate/hooks/mail"

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

package myhook

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

type MyHookConfig struct {
    Address  string `json:"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
}
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"}

3rd formatters:

Formatter Output Example
logstash [Removed]

When we need use 3rd formatter, we need import these package as follow:

import _ "github.com/gogap/logrus_mate/formatters/xxx"

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

package myformatter

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

type MyFormatterConfig struct {
    Address  string `json:"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
}
Writers

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/gogap/logrus_mate/writers/redisio"

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

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(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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Formatters

func Formatters() []string

func Hooks

func Hooks() []string

func Logger

func Logger(loggerName ...string) (logger *logrus.Logger)

func NewFormatter

func NewFormatter(name string, options Options) (formatter logrus.Formatter, err error)

func NewHook

func NewHook(name string, options Options) (hook logrus.Hook, err error)

func NewJSONFormatter

func NewJSONFormatter(options Options) (formatter logrus.Formatter, err error)

func NewLogger

func NewLogger(name string, conf LoggerConfig) (logger *logrus.Logger, err error)

func NewNullFormatter

func NewNullFormatter(options Options) (formatter logrus.Formatter, err error)

func NewNullWriter

func NewNullWriter(options Options) (writer io.Writer, err error)

func NewStderrWriter

func NewStderrWriter(options Options) (writer io.Writer, err error)

func NewStdoutWriter

func NewStdoutWriter(options Options) (writer io.Writer, err error)

func NewTextFormatter

func NewTextFormatter(options Options) (formatter logrus.Formatter, err error)

func NewWriter

func NewWriter(name string, options Options) (writer io.Writer, err error)

func RegisterFormatter

func RegisterFormatter(name string, newFormatterFunc NewFormatterFunc)

func RegisterHook

func RegisterHook(name string, newHookFunc NewHookFunc)

func RegisterWriter

func RegisterWriter(name string, newWriterFunc NewWriterFunc)

func Writers

func Writers() []string

Types

type Environments

type Environments struct {
	RunEnv string `json:"run_env"`
}

type FormatterConfig

type FormatterConfig struct {
	Name    string  `json:"name"`
	Options Options `json:"options"`
}

type HookConfig

type HookConfig struct {
	Name    string  `json:"name"`
	Options Options `json:"options"`
}

type JSONFormatterConfig

type JSONFormatterConfig struct {
	TimestampFormat string `json:"timestamp_format"`
}

type LoggerConfig

type LoggerConfig struct {
	Out       WriterConfig    `json:"out"`
	Level     string          `json:"level"`
	Hooks     []HookConfig    `json:"hooks"`
	Formatter FormatterConfig `json:"formatter"`
}

func (*LoggerConfig) Validate

func (conf *LoggerConfig) Validate(env ...string) (err error)

type LoggerItem

type LoggerItem struct {
	Name   string                  `json:"name"`
	Config map[string]LoggerConfig `json:"config"`
}

type LogrusMate

type LogrusMate struct {
	// contains filtered or unexported fields
}

func NewLogrusMate

func NewLogrusMate(mateConf LogrusMateConfig) (logrusMate *LogrusMate, err error)

func (*LogrusMate) Logger

func (p *LogrusMate) Logger(loggerName ...string) (logger *logrus.Logger)

func (LogrusMate) NewLogger

func (p LogrusMate) NewLogger(name string, conf LoggerConfig) (logger *logrus.Logger, err error)

type LogrusMateConfig

type LogrusMateConfig struct {
	EnvironmentKeys Environments `json:"env_keys"`
	Loggers         []LoggerItem `json:"loggers"`
}

func LoadLogrusMateConfig

func LoadLogrusMateConfig(filename string) (conf LogrusMateConfig, err error)

func (*LogrusMateConfig) RunEnv

func (p *LogrusMateConfig) RunEnv() string

func (*LogrusMateConfig) Serialize

func (p *LogrusMateConfig) Serialize() (data []byte, err error)

func (*LogrusMateConfig) Validate

func (p *LogrusMateConfig) Validate() (err error)

type NewFormatterFunc

type NewFormatterFunc func(Options) (formatter logrus.Formatter, err error)

type NewHookFunc

type NewHookFunc func(Options) (hook logrus.Hook, err error)

type NewWriterFunc

type NewWriterFunc func(Options) (writer io.Writer, err error)

type NullFormatter

type NullFormatter struct {
}

func (NullFormatter) Format

func (NullFormatter) Format(e *logrus.Entry) ([]byte, error)

type NullWriter

type NullWriter struct {
}

func (*NullWriter) Write

func (w *NullWriter) Write(p []byte) (n int, err error)

type Options

type Options map[string]interface{}

func (Options) Float64

func (p Options) Float64(key string) (val float64, err error)

func (Options) Int

func (p Options) Int(key string) (val int, err error)

func (Options) Object

func (p Options) Object(key string, v interface{}) (err error)

func (Options) String

func (p Options) String(key string) (val string, err error)

func (Options) ToObject

func (p Options) ToObject(v interface{}) (err error)

type TextFormatterConfig

type TextFormatterConfig struct {
	ForceColors      bool   `json:"force_colors"`
	DisableColors    bool   `json:"disable_colors"`
	DisableTimestamp bool   `json:"disable_timestamp"`
	FullTimestamp    bool   `json:"full_timestamp"`
	TimestampFormat  string `json:"timestamp_format"`
	DisableSorting   bool   `json:"disable_sorting"`
}

type WriterConfig

type WriterConfig struct {
	Name    string  `json:"name"`
	Options Options `json:"options"`
}

Directories

Path Synopsis
hooks
writers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL