I am trying to use the golang package: https://github.com/op/go-logging
I have set it up following the instructions and it works.
var log = logging.MustGetLogger("example")
var format = logging.MustStringFormatter(
"%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}",
)
func main() {
backend2 := logging.NewLogBackend(os.Stderr, "", 0)
backend2Formatter := logging.NewBackendFormatter(backend2, format)
logging.SetBackend(backend2Formatter)
log.Info("info")
log.Notice("notice")
log.Warning("warning")
log.Error("err")
log.Critical("crit")
r.POST("/v1.0/route-one/", controllers.RouteOne)
r.GET("/v1.0/route-two", controllers.RouteTwo)
r.Run(":8080")
}
However I want to create logs from any of the controllers from within my App, how would I create logs from within a controller after the initial declaration in main.go without having to re-write the format etc? Ideally id like to be able to just write log.Error("err")
anywhere to create a log.
UPDATE
Created a package:
package log
import (
"github.com/op/go-logging"
"os"
)
var Log = logging.MustGetLogger("example")
var format = logging.MustStringFormatter(
"%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}",
)
func init() {
backend2 := logging.NewLogBackend(os.Stderr, "", 0)
backend2Formatter := logging.NewBackendFormatter(backend2, format)
logging.SetBackend(backend2Formatter)
Log.Info("info")
Log.Notice("notice")
Log.Warning("warning")
Log.Error("err")
Log.Critical("crit")
}
Then tried to add it into my controller by importing and running Log.Error("test")
but I get the error of undefined: Log in Log.Error