logrusHelper

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2022 License: MIT Imports: 5 Imported by: 0

README

Logrus Helper

A Helper arround Logrus to wrap with spf13/Viper to load configuration with fangs!

And to simplify Logrus configuration use some behavior of Logrus_mate

Why?

Logrus is wonderful but miss some configuration helper. Logrus_mate v1.0.1 is powerful, but bring some unecessary pattern. This specific library version is a fork of Logrus_mate which whose upgrade in v2.0.0. spf13/Viper is simple, powerful and generic.

Howto use it

import(

	"github.com/45hur/logrus"
	"github.com/spf13/viper"
	"github.com/heirko/go-contrib/logrusHelper"
	_ "github.com/heralight/logrus_mate/hooks/file"
)


func initLogger() {

    // ########## Init Viper  
	var viper = viper.New()

	viper.SetConfigName("mate") // name of config file (without extension), here we use some logrus_mate sample
	viper.AddConfigPath("/etc/appname/")   // path to look for the config file in
	viper.AddConfigPath("$HOME/.appname")  // call multiple times to add many search paths
	viper.AddConfigPath(".")               // optionally look for config in the working directory
	err := viper.ReadInConfig() // Find and read the config file
	if err != nil { // Handle errors reading the config file
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}
    // ########### End Init Viper

    // Read configuration
	var c = logrusHelper.UnmarshalConfiguration(viper) // Unmarshal configuration from Viper
	logrusHelper.SetConfig(logrus.StandardLogger(), c) // for e.g. apply it to logrus default instance
	
	// ### End Read Configuration
	
	// ### Use logrus as normal
	logrus.WithFields(logrus.Fields{
		"animal": "walrus",
	}).Error("A walrus appears")
}

A sample is accessible in test directory at Heirko go-contrib.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetConfig

func SetConfig(logger *logrus.Logger, conf mate.LoggerConfig) (err error)

SetConfig take a logrus logger instance and a conf mate.LoggerConfig. then apply conf specification to logrus instance. It returns an error if failed.

func UnmarshalConfiguration

func UnmarshalConfiguration(viper *viper.Viper) (conf mate.LoggerConfig)

UnmarshalConfiguration read configuration from viper. It returns a logrus_mate logger configuration instance.

func UnmarshalConfigurationByKey

func UnmarshalConfigurationByKey(key string, viper *viper.Viper) (conf mate.LoggerConfig)

UnmarshalConfigurationByKey read configuration from viper by a category key.

e.g., if you have all your logging properties under a common key logging

It returns a logrus_mate logger configuration instance.

Types

This section is empty.

Jump to

Keyboard shortcuts

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