I am using logger package github.com/jcelliott/lumber for logging in go, and I declare and use it like this:
func main() {
log := lumber.NewConsoleLogger(lumber.DEBUG)
...
log.Error("File error: %v\n", e)
}
How can I log from functions outside of main? Obviously here log is declared within main and that limits its scope, but I haven't found a way to have global variables in GO, is there a better way than re-declaring the logger in each function?
Declare your global variable like this :
var log lumber.Logger
func anyFunc() {
log.Error("File error: %v\n", e)
}
func main() {
log = lumber.NewConsoleLogger(lumber.DEBUG)
anyFunc()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With