formatter

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	// Version is the current Gelf version
	Version string = "1.1"
)

Variables

This section is empty.

Functions

func ContextToJSON

func ContextToJSON(context *logger.Context, builder *strings.Builder)

ContextToJSON will marshall the logger context into json

func EntryToJSON

func EntryToJSON(entry logger.Entry, builder *strings.Builder)

EntryToJSON will marshall the logger Entry into json

func JSONEncoder

func JSONEncoder(entry logger.Entry) ([]byte, error)

JSONEncoder will return Entry to json string

Types

type DefaultFormatter

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

DefaultFormatter is the default Entry formatter

Example
package main

import (
	"fmt"

	"github.com/gol4ng/logger"
	"github.com/gol4ng/logger/formatter"
)

func main() {
	defaultFormatter := formatter.NewDefaultFormatter(formatter.WithContext(true))

	fmt.Println(defaultFormatter.Format(
		logger.Entry{
			Message: "My log message",
			Level:   logger.InfoLevel,
			Context: logger.NewContext().Add("my_key", "my_value"),
		},
	))

}
Output:

<info> My log message {"my_key":"my_value"}

func NewDefaultFormatter

func NewDefaultFormatter(options ...Option) *DefaultFormatter

NewDefaultFormatter will create a new DefaultFormatter

func (*DefaultFormatter) Format

func (n *DefaultFormatter) Format(entry logger.Entry) string

Format will return Entry as string

type Gelf

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

Gelf formatter will transform Entry into Gelf format

func NewGelf

func NewGelf() *Gelf

NewGelf will create a new Gelf formatter

func (*Gelf) Format

func (g *Gelf) Format(entry logger.Entry) string

Format will return Entry as gelf

Example
package main

import (
	"fmt"
	"os"
	"time"

	"bou.ke/monkey"

	"github.com/gol4ng/logger"
	"github.com/gol4ng/logger/formatter"
)

func main() {
	monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) })
	monkey.Patch(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
	defer monkey.UnpatchAll()

	gelfFormatter := formatter.NewGelf()

	fmt.Println(gelfFormatter.Format(logger.Entry{Message: "My log message", Level: logger.InfoLevel, Context: logger.NewContext().Add("my key", "my_value")}))

}
Output:

{"version":"1.1","host":"my_fake_hostname","level":6,"timestamp":513216000.000,"short_message":"My log message","full_message":"<info> My log message [ <my key:my_value> ]","_my_key":"my_value"}

type GelfTCPFormatter

type GelfTCPFormatter struct {
	*Gelf
}

GelfTCPFormatter will decorate Gelf formatter in order to add null byte between each entry http://docs.graylog.org/en/3.0/pages/gelf.html#gelf-via-tcp

func NewGelfTCP

func NewGelfTCP() *GelfTCPFormatter

NewGelfTCP will create a new GelfTCPFormatter

func (*GelfTCPFormatter) Format

func (g *GelfTCPFormatter) Format(entry logger.Entry) string

Format will return Entry as gelf TCP

type JSON

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

JSON formatter will transform a logger entry into JSON it takes an encode function that allows you to encode the data

the encode function is useful if you do not use the default provided logger implementation

func NewJSON

func NewJSON(encode func(logger.Entry) ([]byte, error)) *JSON

NewJSON will create a new JSON with given json encoder it allow you tu use your own json encoder

func NewJSONEncoder

func NewJSONEncoder() *JSON

NewJSONEncoder will create a new JSON with default json encoder function

func (*JSON) Format

func (j *JSON) Format(entry logger.Entry) string

Format will return Entry as json

type Line

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

Line formatter will transform log Entry into string

func NewLine

func NewLine(format string) *Line

NewLine will create a new Line with format (fmt)

func (*Line) Format

func (l *Line) Format(entry logger.Entry) string

Format will return Entry as string typically used for stdout output

type Option

type Option func(*DefaultFormatter)

Option is the option pattern interface for the DefaultFormatter

func WithColor

func WithColor(enable bool) Option

WithColor function will enable ANSI colored formatting

func WithConditionalColor added in v0.5.8

func WithConditionalColor(conditional func(_ logger.Entry) bool) Option

WithConditionalColor function will enable ANSI colored formatting

func WithConditionalContext added in v0.5.8

func WithConditionalContext(conditional func(_ logger.Entry) bool) Option

WithConditionalContext function will display context printing

func WithContext

func WithContext(enable bool) Option

WithContext function will display context printing

Jump to

Keyboard shortcuts

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