As I can see, each developer has its own need and approach to solve same things/needs. As an example, logging. There are many logging packages that works in different ways and each developer choose the one that best fits their needs/preferences. Thinking on that, I would like to create a package that should use the caller's logging package. Is it possible? Someone have a way to do that?
Something like this:
Main code using logrus package:
package main
import (
"os"
"github.com/Sirupsen/logrus"
"gitlab.com/tutume/_testing/globallogs/mypack"
)
var Log = logrus.New()
var myPack pack.Pack
func main() {
isDebug := os.Getenv("MYAPP_LOGLEVEL")
switch isDebug {
case "Debug":
Log.Level = logrus.DebugLevel
case "Info":
Log.Level = logrus.InfoLevel
default:
Log.Level = logrus.ErrorLevel
}
Log.Formatter = &logrus.TextFormatter{
ForceColors: true,
}
Log.Debug("Debugging...")
Log.Info("Informing...")
Log.Error("Normal...")
myPack.Logger = Log
myPack.DoSomething()
}
mypack code:
package pack
type Pack struct {
Logger interface{}
}
func (mp *Pack) DoSomething() {
// Logger.Debug("Debugging...")
// Logger.Info("Informing...")
// Logger.Error("Normal...")
}