Skip to content

naucon/gologger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logger Interface for Go

Build Coverage

This package is a logger interface for go projects. The goal is to use a common logging interface without coupling the project to concrete logging implementations.

Adapter

It comes with the following adapters to common logger packages:

To implement another logger is straight forward. Write your own adapter that implements following functions:

type Logger interface {
  Error(msg string)
  ErrorWithFields(fields map[string]interface{}, msg string)
  Errorf(format string, v ...interface{})
  ErrorfWithFields(fields map[string]interface{}, format string, v ...interface{})
  ErrorErr(err error)
  Warn(msg string)
  WarnWithFields(fields map[string]interface{}, msg string)
  Warnf(format string, v ...interface{})
  WarnfWithFields(fields map[string]interface{}, format string, v ...interface{})
  WarnErr(err error)
  Info(msg string)
  InfoWithFields(fields map[string]interface{}, msg string)
  Infof(format string, v ...interface{})
  InfofWithFields(fields map[string]interface{}, format string, v ...interface{})
  Debug(msg string)
  DebugWithFields(fields map[string]interface{}, msg string)
  Debugf(format string, v ...interface{})
  DebugfWithFields(fields map[string]interface{}, format string, v ...interface{})
}

Installation

install the latest version via go get

go get -u github.com/naucon/gologger

Import package

import (
  "github.com/naucon/gologger"
)

Usage

First create an instance of your logger. In this example we're using zerolog.

z := zerolog.New(os.Stdout)

Import the adapter for your concrete logger.

import (
  adapter "github.com/naucon/gologger/adapter/zlog"
)

Next create an adapter instance with adapter.NewAdapter() and pass in the logger instance.

l := zlog.NewAdapter(&z)

Finally, you can decouple the logger implementation by using the Logger interface.

func doSomething(l logger.Logger) {
  err := errors.New("some error")
  fields := map[string]interface{}{
    "key": "value",
  }

  l.Error("a meaningful std message")
  l.ErrorWithFields(fields, "a meaningful std message")
  l.Errorf("a meaningful std message: %v", err)
  l.ErrorfWithFields(fields, "a meaningful std message: %v", err)
  l.ErrorErr(err)
  l.Warn("a meaningful std message")
  l.WarnWithFields(fields, "a meaningful std message")
  l.Warnf("a meaningful std message: %v", err)
  l.WarnfWithFields(fields, "a meaningful std message: %v", err)
  l.WarnErr(err)
  l.Info("a meaningful std message")
  l.InfoWithFields(fields, "a meaningful std message")
  l.Infof("a meaningful std message: %v", err)
  l.InfofWithFields(fields, "a meaningful std message: %v", err)
  l.Debug("a meaningful std message")
  l.DebugWithFields(fields, "a meaningful std message")
  l.Debugf("a meaningful std message: %v", err)
  l.DebugfWithFields(fields, "a meaningful std message: %v", err)
}

Logger Wrapper

The package comes with a loggerWrapper to wrap another adapter.

  z := zerolog.New(os.Stdout)
  l := zlog.NewAdapter(&z)
  w := logger.NewWrapper(l)
  w.Info("a meaningful std message")

Example

package main

import (
  "github.com/naucon/gologger"
  "github.com/naucon/gologger/adapter/zlog"
  "github.com/rs/zerolog"
  "os"
)

func main() {
  z := zerolog.New(os.Stdout)
  l := zlog.NewAdapter(&z)
  doSomething(l)
}

func doSomething(l logger.Logger) {
  l.Info("a meaningful std message")
}