Level based logging in Golang

Vivek Dasgupta
1 min readJun 25, 2018

--

While developing a new piece of software in Golang, developers may often want to print debug messages. You might also want to print logs for various levels : ERROR, WARN, INFO etc. Also having a choice of loglevel at runtime is great. So how do we achieve this ?

Golang’s log package does not have levelled logs. So I wrote a go package which can be used to achieve this. This package also handles storing logs to S3 for archival purposes.

Various level available are PANIC, ERROR, WARN, INFO and DEBUG

It is possible to set the loglevel that each message belongs to and also to set the global loglevel at runtime.

Here is the log wrapper :

Source

Full source code of this package can be found at : https://github.com/vivekdasgupta/infolog.git

Usage

Here is an example code which uses this package.

Build and run this code. Since DEBUG loglevel is 4, this code will only print messages if you supply ≥4 value as a param. Try with a value <4.

Happy Golang Coding !!

--

--