plog

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2022 License: BSD-3-Clause Imports: 12 Imported by: 0

README

plog

Build Status Go Reference

JSON logger for Go.
Source files are distributed under the BSD-style license.

About

The software is considered to be at a alpha level of readiness, its extremely slow and allocates a lots of memory.

Usage

Set plog as global logger

package main

import (
    "os"
    "log"

    "github.com/pfmt/plog"
)

func main() {
    l := &plog.Log{
        Output:  os.Stdout,
        Keys:    [4]encoding.TextMarshaler{pfmt.String("message"), pfmt.String("excerpt")},
        Trunc:   12,
        Marks:   [3][]byte{[]byte("…")},
        Replace: [][2][]byte{[2][]byte{[]byte("\n"), []byte(" ")}},
    }
    log.SetFlags(0)
    log.SetOutput(l)

    log.Print("Hello,\nWorld!")
}

Output:

{
    "message":"Hello,\nWorld!",
    "excerpt":"Hello, World…"
}

Use as GELF formater

package main

import (
    "log"
    "os"

    "github.com/pfmt/plog"
)

func main() {
    l := plog.GELF()
    l.Output = os.Stdout
    log.SetFlags(0)
    log.SetOutput(l)
    log.Print("Hello,\nGELF!")
}

Output:

{
    "version":"1.1",
    "short_message":"Hello, GELF!",
    "full_message":"Hello,\nGELF!",
    "timestamp":1602785340
}

Caveat: numeric types appears in the message as a string

package main

import (
    "log"
    "os"

    "github.com/pfmt/plog"
)

func main() {
    l := plog.Log{
        Output: os.Stdout,
        Keys:   [4]encoding.TextMarshaler{pfmt.String("message")},
    }
    log.SetFlags(0)
    log.SetOutput(l)

    log.Print(123)
    log.Print(3.21)
}

Output 1:

{
    "message":"123"
}

Output 2:

{
    "message":"3.21"
}

Benchmark

$ go test -count=1 -race -bench ./... 
goos: linux
goarch: amd64
pkg: github.com/pfmt/plog
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkPlog/plog_test.go:90/io.Writer-8         	   40026	     27957 ns/op
BenchmarkPlog/plog_test.go:1138/fmt.Fprint_io.Writer-8         	   15207	     75703 ns/op
PASS
ok  	github.com/pfmt/plog	3.506s

Documentation

Overview

Package plog is a JSON logging.

Index

Constants

View Source
const (
	Original = iota
	Excerpt
	Trail
	File
)
View Source
const (
	Trunc = iota
	Empty
	Blank
)

Variables

This section is empty.

Functions

func StringAny

func StringAny(k string, v interface{}) kvm

func StringBool

func StringBool(k string, v bool) kvm

func StringBoolp

func StringBoolp(k string, v *bool) kvm

func StringBools

func StringBools(k string, v []bool) kvm

func StringBytes

func StringBytes(k string, v []byte) kvm

func StringBytesp

func StringBytesp(k string, v *[]byte) kvm

func StringBytess

func StringBytess(k string, v [][]byte) kvm

func StringBytessp

func StringBytessp(k string, v []*[]byte) kvm

func StringComplex128

func StringComplex128(k string, v complex128) kvm

func StringComplex128p

func StringComplex128p(k string, v *complex128) kvm

func StringComplex64

func StringComplex64(k string, v complex64) kvm

func StringComplex64p

func StringComplex64p(k string, v *complex64) kvm

func StringDuration

func StringDuration(k string, v time.Duration) kvm

func StringDurationp

func StringDurationp(k string, v *time.Duration) kvm

func StringError

func StringError(k string, v error) kvm

func StringErrors

func StringErrors(k string, v []error) kvm

func StringFloat32

func StringFloat32(k string, v float32) kvm

func StringFloat32p

func StringFloat32p(k string, v *float32) kvm

func StringFloat64

func StringFloat64(k string, v float64) kvm

func StringFloat64p

func StringFloat64p(k string, v *float64) kvm

func StringFunc

func StringFunc(k string, v func() pfmt.KV) kvm

func StringInt

func StringInt(k string, v int) kvm

func StringInt16

func StringInt16(k string, v int16) kvm

func StringInt16p

func StringInt16p(k string, v *int16) kvm

func StringInt32

func StringInt32(k string, v int32) kvm

func StringInt32p

func StringInt32p(k string, v *int32) kvm

func StringInt64

func StringInt64(k string, v int64) kvm

func StringInt64p

func StringInt64p(k string, v *int64) kvm

func StringInt8

func StringInt8(k string, v int8) kvm

func StringInt8p

func StringInt8p(k string, v *int8) kvm

func StringIntp

func StringIntp(k string, v *int) kvm

func StringLevel

func StringLevel(k string, v string) kvl

func StringRaw

func StringRaw(k string, v []byte) kvm

func StringReflect

func StringReflect(k string, v interface{}) kvm

func StringRunes

func StringRunes(k string, v []rune) kvm

func StringRunesp

func StringRunesp(k string, v *[]rune) kvm

func StringString

func StringString(k string, v string) kvm

func StringStringp

func StringStringp(k string, v *string) kvm

func StringStrings

func StringStrings(k string, v []string) kvm

func StringTime

func StringTime(k string, v time.Time) kvm

func StringTimep

func StringTimep(k string, v *time.Time) kvm

func StringUint

func StringUint(k string, v uint) kvm

func StringUint16

func StringUint16(k string, v uint16) kvm

func StringUint16p

func StringUint16p(k string, v *uint16) kvm

func StringUint32

func StringUint32(k string, v uint32) kvm

func StringUint32p

func StringUint32p(k string, v *uint32) kvm

func StringUint64

func StringUint64(k string, v uint64) kvm

func StringUint64p

func StringUint64p(k string, v *uint64) kvm

func StringUint8

func StringUint8(k string, v uint8) kvm

func StringUint8p

func StringUint8p(k string, v *uint8) kvm

func StringUintp

func StringUintp(k string, v *uint) kvm

func StringUintptr

func StringUintptr(k string, v uintptr) kvm

func StringUintptrp

func StringUintptrp(k string, v *uintptr) kvm

func TextAny

func TextAny(k encoding.TextMarshaler, v interface{}) kvm

func TextBool

func TextBool(k encoding.TextMarshaler, v bool) kvm

func TextBoolp

func TextBoolp(k encoding.TextMarshaler, v *bool) kvm

func TextBytes

func TextBytes(k encoding.TextMarshaler, v []byte) kvm

func TextBytesp

func TextBytesp(k encoding.TextMarshaler, v *[]byte) kvm

func TextComplex128

func TextComplex128(k encoding.TextMarshaler, v complex128) kvm

func TextComplex128p

func TextComplex128p(k encoding.TextMarshaler, v *complex128) kvm

func TextComplex64

func TextComplex64(k encoding.TextMarshaler, v complex64) kvm

func TextComplex64p

func TextComplex64p(k encoding.TextMarshaler, v *complex64) kvm

func TextDuration

func TextDuration(k encoding.TextMarshaler, v time.Duration) kvm

func TextDurationp

func TextDurationp(k encoding.TextMarshaler, v *time.Duration) kvm

func TextError

func TextError(k encoding.TextMarshaler, v error) kvm

func TextFloat32

func TextFloat32(k encoding.TextMarshaler, v float32) kvm

func TextFloat32p

func TextFloat32p(k encoding.TextMarshaler, v *float32) kvm

func TextFloat64

func TextFloat64(k encoding.TextMarshaler, v float64) kvm

func TextFloat64p

func TextFloat64p(k encoding.TextMarshaler, v *float64) kvm

func TextFunc

func TextFunc(k encoding.TextMarshaler, v func() json.Marshaler) kvm

func TextInt

func TextInt(k encoding.TextMarshaler, v int) kvm

func TextInt16

func TextInt16(k encoding.TextMarshaler, v int16) kvm

func TextInt16p

func TextInt16p(k encoding.TextMarshaler, v *int16) kvm

func TextInt32

func TextInt32(k encoding.TextMarshaler, v int32) kvm

func TextInt32p

func TextInt32p(k encoding.TextMarshaler, v *int32) kvm

func TextInt64

func TextInt64(k encoding.TextMarshaler, v int64) kvm

func TextInt64p

func TextInt64p(k encoding.TextMarshaler, v *int64) kvm

func TextInt8

func TextInt8(k encoding.TextMarshaler, v int8) kvm

func TextInt8p

func TextInt8p(k encoding.TextMarshaler, v *int8) kvm

func TextIntp

func TextIntp(k encoding.TextMarshaler, v *int) kvm

func TextRaw

func TextRaw(k encoding.TextMarshaler, v []byte) kvm

func TextReflect

func TextReflect(k encoding.TextMarshaler, v interface{}) kvm

func TextRunes

func TextRunes(k encoding.TextMarshaler, v []rune) kvm

func TextRunesp

func TextRunesp(k encoding.TextMarshaler, v *[]rune) kvm

func TextString

func TextString(k encoding.TextMarshaler, v string) kvm

func TextStringp

func TextStringp(k encoding.TextMarshaler, v *string) kvm

func TextText

func TextText(k, v encoding.TextMarshaler) kvm

func TextTime

func TextTime(k encoding.TextMarshaler, v time.Time) kvm

func TextTimep

func TextTimep(k encoding.TextMarshaler, v *time.Time) kvm

func TextUint

func TextUint(k encoding.TextMarshaler, v uint) kvm

func TextUint16

func TextUint16(k encoding.TextMarshaler, v uint16) kvm

func TextUint16p

func TextUint16p(k encoding.TextMarshaler, v *uint16) kvm

func TextUint32

func TextUint32(k encoding.TextMarshaler, v uint32) kvm

func TextUint32p

func TextUint32p(k encoding.TextMarshaler, v *uint32) kvm

func TextUint64

func TextUint64(k encoding.TextMarshaler, v uint64) kvm

func TextUint64p

func TextUint64p(k encoding.TextMarshaler, v *uint64) kvm

func TextUint8

func TextUint8(k encoding.TextMarshaler, v uint8) kvm

func TextUint8p

func TextUint8p(k encoding.TextMarshaler, v *uint8) kvm

func TextUintp

func TextUintp(k encoding.TextMarshaler, v *uint) kvm

func TextUintptr

func TextUintptr(k encoding.TextMarshaler, v uintptr) kvm

func TextUintptrp

func TextUintptrp(k encoding.TextMarshaler, v *uintptr) kvm

Types

type Encoder

type Encoder interface {
	Encode(...pfmt.KV) []byte
}

type KeyValuer

type KeyValuer interface {
	KeyValues() []pfmt.KV
}

KeyValuer provides key-values slice.

type Leveler

type Leveler interface {
	Level() string
}

Leveler provides severity level.

type Log

type Log struct {
	Output  io.Writer                             // Output is a destination for output.
	Flag    int                                   // Flag is a log properties.
	KV      []pfmt.KV                             // KV is a key-values.
	Level   func(level string) (output io.Writer) // Level function receives severity level and returns a output writer for a severity level.
	Keys    [4]encoding.TextMarshaler             // Keys: 0 = original message; 1 = message excerpt; 2 = message trail; 3 = file path.
	Key     uint8                                 // Key is a default/sticky message key: all except 0 = original message; 1 = message excerpt.
	Trunc   int                                   // Trunc is a maximum length of an excerpt, after which it is truncated.
	Marks   [3][]byte                             // Marks: 0 = truncate; 1 = empty; 2 = blank.
	Replace [][2][]byte                           // Replace ia a pairs of byte slices to replace in the message excerpt.
}

Log is a JSON logger/writer.

func GELF

func GELF() *Log

GELF returns a GELF formater <https://docs.graylog.org/en/latest/pages/gelf.html>.

func New

func New(opts ...Option) Log

func (*Log) Close

func (l *Log) Close() error

Close puts a log into sync pool.

func (*Log) Encode

func (l *Log) Encode(kv ...pfmt.KV) []byte

func (*Log) KeyValues

func (l *Log) KeyValues() []pfmt.KV

func (*Log) Tee

func (l *Log) Tee(kv ...pfmt.KV) Logger

Tee returns copy of the logger with additional key-values. If first key-value pair implements the Leveler interface and the Level field of the Log is not null then calls the function from Level field with the severity level as argument which obtained from Leveler interface. Then the function from Level field returns writer for output of the logger. Copy of the original key-values has the priority lower than the priority of the newer key-values.

func (Log) Truncate

func (l Log) Truncate(dst, src []byte) (int, error)

Truncate writes excerpt of the src to the dst and returns number of the written bytes and error if occurre.

func (*Log) Write

func (l *Log) Write(src []byte) (int, error)

Write implements io.Writer. Do nothing if log does not have output.

type Logger

type Logger interface {
	io.Writer
	// Tee returns copy of the logger with an additional key-values.
	// Copy of the original key-values should have a lower priority
	// than the priority of the newer key-values.
	Tee(...pfmt.KV) Logger
	// Close puts the logger into the sync pool.
	Close() error
}

type Option

type Option func(*Log)

Option changes log configuration.

func WithBlankLabel

func WithBlankLabel(label []byte) Option

WithBlankLabel sets a blank mark, for example BLANK.

func WithEmptyLabel

func WithEmptyLabel(label []byte) Option

WithEmptyLabel sets a empty mark, for example EMPTY.

func WithExceptKey

func WithExceptKey(key string) Option

WithExceptKey sets a key name of a message except.

func WithExcerptLen

func WithExcerptLen(length int) Option

WithExcerptLen sets a maximum length of an excerpt, truncate message after maximum length.

func WithExcerptMessage

func WithExcerptMessage() Option

WithExcerptMessage uses message except key by default (switches to sticky message excerpt).

func WithFilePathKey

func WithFilePathKey(key string) Option

WithFilePathKey sets a key name of a log file path.

func WithFlag

func WithFlag(f int) Option

WithFlag sets a log properties.

func WithKV

func WithKV(kv ...pfmt.KV) Option

WithKV sets a key-values.

func WithLevel

func WithLevel(level func(level string) (output io.Writer)) Option

WithLevel sets a level function receives severity level and returns a output writer for a severity level.

func WithOriginal

func WithOriginal() Option

WithOriginal uses original message key by default (switches to sticky original message).

func WithOriginalKey

func WithOriginalKey(key string) Option

WithOriginalKey sets a key name of a original message.

func WithOutput

func WithOutput(output io.Writer) Option

WithOutput sets a destination for output

func WithReplace

func WithReplace(find, replace []byte) Option

WithReplace add find and replace pair.

func WithTrailKey

func WithTrailKey(key string) Option

WithTrailKey sets a key name of a message trail.

func WithTruncateLabel

func WithTruncateLabel(label []byte) Option

WithTruncateLabel sets a truncate mark, for example ....

func WithoutReplace

func WithoutReplace() Option

WithoutReplace disables find and replace.

Jump to

Keyboard shortcuts

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