Logo of the project
Logo of the project

Logger library for the gophers

An opinionated logging library for golang. Gologger writes logs as json in the following format

  1. {
  2. "time":"2016-11-10T16:11:46.59Z",
  3. "process_name":"sample_logger",
  4. "host_name":"sarath.local",
  5. "process_id":29422,
  6. "level":"ERROR",
  7. "file_name":"/home/sarath/go/src/github.com/sarathsp06/gologger/sample/main.go",
  8. "line_num":13,
  9. "log_msg":"error happened"
  10. }

That much info will be how following will be written

  1. logger.Error("error happened")

Installing / Getting started

Just like any other go library

  1. go get github.com/sarathsp06/gologger

But if one needs to get a particular version then use

  1. go get gopkg.in/sarathsp06/gologger.vx #x is the version Number

Developing

Here’s a brief intro about what a developer must do in order to start developing the project further:

  1. Get it using go get or clone
  2. Make changes and make a pull request with Updated README if feature addition

Features

What’s all the bells and whistles this project can perform? * It can log - Obviously * Easy buffered logging * You can set write to logger for redirecting logs * log message will contain much deeper details like line number of error , process id,host_name etc

How to use

This is how one may use the library

import the package

  1. import logger "github.com/sarathsp06/gologger"
Initialize the package

  1. if err := logger.InitLogger("INFO", ".", "sample_logger",true); err != nil {
  2. panic(err.Error())
  3. }

Log as you wish with formated and normal message

  1. logger.Error("error happened")
  2. logger.Debug("Debug message",":Sasa","sasa")
  3. logger.Info("error happened")
  4. logger.Warning("error happened")
  5. logger.Errorf("error happened %s", "Yo")
  6. logger.Debugf("debug message : %s", "YoYo")

Explore more ….

Here is a sample code

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  1. package main
  2. import (
  3. "os"
  4. logger "github.com/sarathsp06/gologger"
  5. )
  6. func main() {
  7. if err := logger.InitLogger("INFO", ".", "sample_logger",true); err != nil {
  8. panic(err.Error())
  9. }
  10. logger.SetLogWriter(os.Stdout)
  11. logger.Error("error happened")
  12. logger.Debug("Debug message")
  13. logger.Info("error happened")
  14. logger.Warning("error happened")
  15. logger.Errorf("error happened %s", "Yo")
  16. logger.Debugf("debug message : %s", "YoYo")
  17. }

Licensing

The code in this project is licensed under MIT license.