axpira / goplogadapter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go Reference Go Report Card codecov Pipeline

Contributors Forks Stargazers Issues MIT License


GOP Log Adapter

Adapter to use with Gop Log
You must just implement one method to create your own log implementation
Or choose one of implementations like logrus or json

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

This project implements gop log and expose just one function:

type FormatterFunc func(log.Level, string, error, map[string]interface{})

This function will be called every time need to send something to log, and you need just to implements and set as default Formatter

In this repository has some sub folder, for each sub folder is one of implementation you can use, like:

  • json: Just convert to json and send to output
import (
	_ "github.com/axpira/goplogadapter/json"
)
  • logrus: Use logrus to send log
import (
	_ "github.com/axpira/goplogadapter/logrus"
)

All this are sub module, so if you want to use you will need to import in your project

(back to top)

Built With

(back to top)

Installation

go get -u github.com/axpira/goplogadapter

Or choosing one implementation:

go get -u github.com/axpira/goplogadapter/logrus

Getting Started

You will need to use gop log to log anything.

And to choose this implementation just do it:

import _ "github.com/axpira/goplogadapter/logrus"

Now when you log:

import (
	"github.com/axpira/gop/log"
	_ "github.com/axpira/goplogadapter/logrus"
)

func main() {
	log.Info("Hello World")

	log.Inf(log.
		Str("str_field", "hello").
		Int("int_field", 42).
		Msg("Hello World"),
	)

The logrus will be called to send log

Usage

To create a new logger and set as default for gop:

import (
	"github.com/axpira/gop/log"
	gla "github.com/axpira/goplogadapter"
)

func init() {
	MyFormatter := func(level log.Level, msg string, err error, m map[string]interface{}) {
		fmt.Printf("[%v] [%v] %s %v", level, err, msg, m)
	}
	log.DefaultLogger = gla.New(gla.WithFormatter(MyFormatter), gpa.WithLevel(log.LevelTrace))
}

This example is creating a new formatter, setting as default for gop and change minimum level to log to Trace.

But you can just use one of implementation bellow

Logrus

To configure anything you want please check at logrus. For example:

import (
	"os"
	"github.com/axpira/gop/log"
	_ "github.com/axpira/goplogadapter/logrus"
	"github.com/sirupsen/logrus"
)

func main() {
	logrus.SetReportCaller(true)
	logrus.SetFormatter(&logrus.JSONFormatter{})
	log.SetOutput(os.Stdout)

	// Now the log format will be JSON with caller and output to os.Stdout
	log.Inf(log.
		Str("str_field", "hello").
		Int("int_field", 42).
		Msg("Hello World"),
)

Json

Using default golang json parse to send fields to output To configure anything you want please check at logrus. For example:

import (
	"os"
	"github.com/axpira/gop/log"
	_ "github.com/axpira/goplogadapter/json"
)

func main() {
	// Changing default output
	json.Output = os.Stderr

	log.Inf(log.
		Str("str_field", "hello").
		Int("int_field", 42).
		Msg("Hello World"),
)

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Thiago Ferreira - thiagogbferreira@gmail.com

Project Link: https://github.com/axpira/goplogadapter

(back to top)

About

License:MIT License


Languages

Language:Go 94.2%Language:Makefile 5.8%