Logging Facade

264 views
Skip to first unread message

Jérôme LAFORGE

unread,
Jan 31, 2015, 1:01:13 PM1/31/15
to golan...@googlegroups.com
Hello Gophers,
Do you know whether logging facade framework exists for Go (as SF4J http://www.slf4j.org/ for Java) ?
My main goal is to develop one library and let to the final user the choice of logging framework.

Thx in adv.
Jérôme

Ian Davis

unread,
Jan 31, 2015, 1:50:46 PM1/31/15
to golan...@googlegroups.com
Before you do that, have a think about whether a framework is necessary.
 
For example, the author of an application can create a Logger interface that contains the methods they need, e.g. in the simple case:
 
type Logger interface{
    Printf(format string, v ...interface{})
}
 
Then they write their types like this:
 
type Processor struct {
    Logger Logger
}
 
func (p *Processor) DoSomething() {
    p.Logger.Printf("it's happening")
}
 
 
Now automatically any logging package that provides a logger with that method can be used by the application. In this case it includes the standard library logger and a bunch of others. They have complete choice. If they want to use a logging package that doesn't support Printf then a simple adapter that delegates can be written:
 
type LogAdapter struct {
    logger OtherLogger
}
 
func (la *LogAdapter) Printf(format string, v ...interface{}) {
   la.logger.Infof(format, v...)
}
 
 
Ian

Zach Klippenstein

unread,
Jan 31, 2015, 10:26:31 PM1/31/15
to golan...@googlegroups.com
If you're looking for leveled logging, there's glog: https://github.com/golang/glog. It's not a facade though.

yun.z...@gmail.com

unread,
Sep 6, 2016, 12:46:08 PM9/6/16
to golang-nuts
I was looking around for the same thing and found this: https://github.com/ventu-io/slf

I agree with Ian, for simple logs, a interface will suffice. But for structured logs with Fields it gets a bit more complicated.
Reply all
Reply to author
Forward
0 new messages