over

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2022 License: MIT Imports: 9 Imported by: 9

README

Overlog

Golang Logging with Mapped Diagnostic Context

What is it?

This package is Zerolog wrapper with Mapped Diagnostic Context structure like Sl4j.

Installation

go get -u github.com/Trendyol/overlog

Getting Started

over.New() and over.NewDefault() initialize global log field. You can use over.Log() from anywhere after initialize.

Quick start:

package main

import (
	over "github.com/Trendyol/overlog"
)

func main() {
	over.NewDefault()
	over.MDC().Set("x-correlation-id", "1234")
	over.MDC().Set("x-agent-name", "trendyol.com")
	over.SetGlobalFields([]string{"x-correlation-id", "x-agent-name"})
    
	over.Log().Info("hello world")
}

// Output: {"level":"INFO","time":"2020-05-20 13:02:10,000","source":"overlog/overlogger.go:70","x-agent-name":"trendyol","x-correlation-id":"1234","message":"hello world"}

You can set your own zerolog settings.

package main

import (
	over "github.com/Trendyol/overlog"
	"github.com/rs/zerolog"
	"os"
)

func main() {
	zlogger := zerolog.New(os.Stderr).With().Str("foo", "bar").Logger()
	over.New(zlogger)
	over.MDC().Set("x-correlation-id", "1234")
	over.AddGlobalFields("x-correlation-id")

	over.Log().Info("hello world")
}

// Output: {"level":"info","foo":"bar","x-correlation-id":"1234","message":"hello world"}
Audition middleware example for echo
func main() {
	...
	over.NewDefault()
	over.SetGlobalFields([]string{"x-agent-name", "x-correlation-id"})
	...
}
func AuditionPreHandler(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        var agentName = c.Request().Header.Get("x-agent-name")
        if agentName == "" {
        	agentName = a.instance.Config().Host
        }
        over.MDC().Set("x-agent-name", agentName)

        var correlationId = c.Request().Header.Get("x-correlation-id")
        if correlationId == "" {
            correlationId = uuid.New().String()
        }
        over.MDC().Set("x-correlation-id", correlationId)

        return next(c)
    }
}
func (a *Application) AuditionAfterCompletion(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        c.Response().After(func() {
            over.MDC().Remove("x-agent-name")
            over.MDC().Remove("x-correlation-id")
        })
        return next(c)
    }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddGlobalFields

func AddGlobalFields(field string)

func ClearGlobalFields

func ClearGlobalFields()

func FullCallerEncoder

func FullCallerEncoder() func(pc uintptr, file string, line int) string

func GetGlobalFields

func GetGlobalFields() []string

func GetGoroutineID

func GetGoroutineID() uint64

func LowercaseLevelEncoder

func LowercaseLevelEncoder() func(l zerolog.Level) string

func ResetGlobalMdcAdapter

func ResetGlobalMdcAdapter()

func SetGlobalFields

func SetGlobalFields(fields []string)

func ShortCallerEncoder

func ShortCallerEncoder() func(pc uintptr, file string, line int) string

func TrimmedPath

func TrimmedPath(file string) string

func UppercaseLevelEncoder

func UppercaseLevelEncoder() func(l zerolog.Level) string

func Z

func Z() *zerolog.Logger

Types

type MDCHook

type MDCHook struct{}

func (MDCHook) Run

func (m MDCHook) Run(e *zerolog.Event, level zerolog.Level, message string)

type MdcAdapter

type MdcAdapter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func InitGlobalMdcAdapter

func InitGlobalMdcAdapter() *MdcAdapter

func MDC

func MDC() *MdcAdapter

func (*MdcAdapter) Count

func (m *MdcAdapter) Count() int

func (*MdcAdapter) Get

func (m *MdcAdapter) Get(key string) (interface{}, bool)

func (*MdcAdapter) GetString

func (m *MdcAdapter) GetString(key string) string

func (*MdcAdapter) Has

func (m *MdcAdapter) Has(key string) bool

func (*MdcAdapter) IsEmpty

func (m *MdcAdapter) IsEmpty() bool

func (*MdcAdapter) Keys

func (m *MdcAdapter) Keys() []string

func (*MdcAdapter) Remove

func (m *MdcAdapter) Remove(key string)

func (*MdcAdapter) Set

func (m *MdcAdapter) Set(key string, value interface{})

func (*MdcAdapter) SetIfAbsent

func (m *MdcAdapter) SetIfAbsent(key string, value interface{}) bool

func (*MdcAdapter) SetMap

func (m *MdcAdapter) SetMap(data map[string]interface{})

type Overlog

type Overlog struct {
	// contains filtered or unexported fields
}

func Log

func Log() *Overlog

func New

func New(logger zerolog.Logger) *Overlog

func NewDefault

func NewDefault() *Overlog

func (*Overlog) Debug

func (o *Overlog) Debug(args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Debug("hello world")
Output:

{"level":"debug","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Debugf

func (o *Overlog) Debugf(format string, args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Debugf("hello %s", "world")
Output:

{"level":"debug","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Error

func (o *Overlog) Error(args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Error(errors.New("hello world"))
Output:

{"level":"error","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Errorf

func (o *Overlog) Errorf(format string, args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Errorf("hello %s", errors.New("world"))
Output:

{"level":"error","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Fatal

func (o *Overlog) Fatal(args ...interface{})

func (*Overlog) Fatalf

func (o *Overlog) Fatalf(format string, args ...interface{})

func (*Overlog) Info

func (o *Overlog) Info(args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Info("hello world")
Output:

{"level":"info","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Infof

func (o *Overlog) Infof(format string, args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Infof("hello %s", "world")
Output:

{"level":"info","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Log

func (o *Overlog) Log(args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Log("hello world")
Output:

{"x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Logf

func (o *Overlog) Logf(format string, args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Logf("hello %s", "world")
Output:

{"x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Panic

func (o *Overlog) Panic(args ...interface{})

func (*Overlog) Panicf

func (o *Overlog) Panicf(format string, args ...interface{})

func (*Overlog) Print

func (o *Overlog) Print(args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Print("hello world")
Output:

{"level":"debug","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Printf

func (o *Overlog) Printf(format string, args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Printf("hello %s", "world")
Output:

{"level":"debug","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Trace

func (o *Overlog) Trace(args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Trace("hello world")
Output:

{"level":"trace","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Tracef

func (o *Overlog) Tracef(format string, args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Tracef("hello %s", "world")
Output:

{"level":"trace","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Warn

func (o *Overlog) Warn(args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Warn("hello world")
Output:

{"level":"warn","x-correlation-id":"1234","message":"hello world"}

func (*Overlog) Warnf

func (o *Overlog) Warnf(format string, args ...interface{})
Example
New(zerolog.New(os.Stdout))
MDC().Set("x-correlation-id", "1234")
AddGlobalFields("x-correlation-id")

Log().Warnf("hello %s", "world")
Output:

{"level":"warn","x-correlation-id":"1234","message":"hello world"}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL