How to Implement Level Based Logging in Golang?

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?

2

10 Answers

Some more suggestions, now that the existing answers are quite old:

  • - smaller than the other here
  • - used in many popular projects such as Docker
  • - from Google, implementation of their C++ glog library in Go
  • focused on "structured logging" which is better for tools to consume
  • - "blazing fast"
3
  • 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.

1

Take a look at 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. ;)

3

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.

1

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.

1

stdlog fits exactly your requirements:

log := stdlog.GetFromFlags()
log.Info("Connecting to the server...")
log.Errorf("Connection failed: %q", err)

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")

I have added logging level support to the built-in Go log package. You can find my code here:

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.

You can use the module midlog to implements any other log library,

1

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"

    ""


)

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()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest