How to implement level based logging in golang?
LoggingGoLogging Problem Overview
Is there any good wrapper available for level based logging in golang? If not, how should I go about implementing one myself?
What I want is pretty simple. I want a few functions e.g.
log.Error()
log.Info()
etc that display their outputs to stdout as well as save these in a log file (based on the level given to the program as commandline argument). How do I implement this wrapper?
Logging Solutions
Solution 1 - Logging
Some more suggestions, now that the existing answers are quite old:
- https://github.com/op/go-logging - smaller than the other here
- https://github.com/sirupsen/logrus - used in many popular projects such as Docker
- https://github.com/inconshreveable/log15
- https://github.com/golang/glog - from Google, implementation of their C++ glog library in Go
- https://github.com/go-kit/kit/tree/master/log focused on "structured logging" which is better for tools to consume
- https://github.com/uber-go/zap - "blazing fast"
Solution 2 - Logging
- Uber-go/Zap: Fast, structured, leveled logging in Go
- Logrus: Structured, pluggable logging for Go. (JSON and text formatting)
Both libraries have level hooks also, which is a very interesting feature. Hooks can be registered for particular log levels. So for example any error(logged using log.Error()
) occurs you can report to some monitoring tool etc.
Solution 3 - Logging
Take a look at http://cgl.tideland.biz and there at the package "applog". It's working that way.
PS: The whole CGL is currently reworked and will soon be released with new features, but under a different name. ;)
Solution 4 - Logging
I think seelog fits your requirements, and it seems to be pretty popular as it pops up often in log discussions. I never used it seriously, so I can't comment beyond that.
Solution 5 - Logging
https://github.com/hashicorp/logutils I found this to be very easy to use and you don't even need to change the method calls to log.Printf
of the std library.
Solution 6 - Logging
stdlog fits exactly your requirements:
log := stdlog.GetFromFlags()
log.Info("Connecting to the server...")
log.Errorf("Connection failed: %q", err)
Solution 7 - Logging
I am working with rlog at the moment and like their approach. The code looks good, simplistic and sufficiently documented.
What convinced me:
-
no external dependencies
-
i can use
rlog.Info()
anywhere without passing around references -
good usage of environment variables
-
arbitrary number of trace levels e.g.
rlog.Trace(4, "foo")
Solution 8 - Logging
I have added logging level support to the built-in Go log package. You can find my code here:
https://github.com/gologme/log
In addition to adding support for Info, Warn, and Debug, users can also define their own arbitrary logging levels. Logging levels are enabled and disabled individually. This means you can turn on Debug logs without also getting everything else.
Solution 9 - Logging
You can use the module midlog to implements any other log library, https://github.com/lingdor/midlog
Solution 10 - Logging
One of the logging module that you can consider is klog . It support 'V' logging which gives the flexibility to log at certain level
klog is a fork of glog and overcomes following drawbacks
glog presents a lot "gotchas" and introduces challenges in containerized environments, all of which aren't well documented. glog doesn't provide an easy way to test logs, which detracts from the stability of software using it glog is C++ based and klog is a pure golang implementation
Sample Implementation
package main
import (
"flag"
"k8s.io/klog"
)
type myError struct {
str string
}
func (e myError) Error() string {
return e.str
}
func main() {
klog.InitFlags(nil)
flag.Set("v", "1")
flag.Parse()
klog.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})
klog.V(3).Info("nice to meet you")
klog.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14})
klog.Error(myError{"an error occurred"}, "goodbye", "code", -1)
klog.Flush()
}