Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement level based logging in golang?

Tags:

logging

go

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?

like image 859
pymd Avatar asked Jun 03 '13 11:06

pymd


People also ask

How do you implement logger in Golang?

Now create 2 directories inside the project directory, name one directory “src”, and name the other “log”, then create a new directory inside src directory and name it “utils”, then create a file inside utils directory and name it “helper.go” and as the last step here, create a file and name it “main.go” in the src ...

How do I log into a file in Golang?

To log to a file, you can use the OS package to create a log file if it does not exist or open and write to an existing file. Doing so will set the output of the log package to the specified output file. Keep in mind that the log package also supports other output destination that supports io.

How do you define logging level?

What is a logging level? A logging level is a way of classifying the entries in your log file in terms of urgency. Classifying helps filter your log files during search and helps control the amount of information in your logs. Sometimes, categorizing may require you to balance storage use.

What is Golang log package?

The package log in Golang implements the simple logging package. It defines a type, Logger, with methods for formatting output. Golang Log will be helpful in the critical scenarios in real-time applications.


1 Answers

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"
like image 171
Bryan Avatar answered Oct 20 '22 16:10

Bryan