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
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
go get -u github.com/axpira/goplogadapter
Or choosing one implementation:
go get -u github.com/axpira/goplogadapter/logrus
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
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
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"),
)
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"),
)
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Thiago Ferreira - thiagogbferreira@gmail.com
Project Link: https://github.com/axpira/goplogadapter