log30

package module
v0.104.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2021 License: MIT Imports: 15 Imported by: 0

README

log30

Build Status Go Reference

JSON logging for Go.

About

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

Install

go get github.com/danil/log30@v0.104.0

Usage

Set log30 as global logger

package main

import (
    "os"
    "log"

    "github.com/danil/log30"
)

func main() {
    l := log30.Log{
        Output: os.Stdout,
        Trunc: 12,
        Keys: [4]json.Marshaler{log30.String("message"), log30.String("excerpt")},
        Marks: [3][]byte{[]byte("…")},
        Replace: [][]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/danil/log30"
)

func main() {
    l := log30.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/danil/log30"
)

func main() {
    l := log30.Log{
        Output: os.Stdout,
        Keys: [4]json.Marshaler{log30.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 -bench=. ./...
goos: linux
goarch: amd64
pkg: github.com/danil/log30
BenchmarkLog30/io.Writer_38-8         	  298993	      3926 ns/op
BenchmarkLog30/fmt.Fprint_io.Writer_1069-8         	  117852	     10049 ns/op

License

Copyright (C) 2021 Danil Kutkevich
See the LICENSE file for license rights and limitations (MIT)

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func Any added in v0.104.0

func Any(v interface{}) anyV

func Bool added in v0.104.0

func Bool(v bool) boolV

Bool returns stringer/JSON marshaler interface implementation for the bool type.

func Boolp added in v0.104.0

func Boolp(p *bool) boolP

Bool returns stringer/JSON marshaler interface implementation for the pointer to the bool type.

func Bytes added in v0.104.0

func Bytes(v []byte) bytesV

Bytes returns stringer/JSON marshaler interface implementation for the byte slice type.

func Bytesp added in v0.104.0

func Bytesp(p *[]byte) bytesP

Bytesp returns stringer/JSON marshaler interface implementation for the pointer to the byte slice type.

func Complex128 added in v0.104.0

func Complex128(v complex128) complex128V

Complex128 returns stringer/JSON marshaler interface implementation for the complex128 type.

func Complex128p added in v0.104.0

func Complex128p(p *complex128) complex128P

Complex128p returns stringer/JSON marshaler interface implementation for the pointer to the complex128 type.

func Complex64 added in v0.104.0

func Complex64(v complex64) complex64V

Complex64 returns stringer/JSON marshaler interface implementation for the complex64 type.

func Complex64p added in v0.104.0

func Complex64p(p *complex64) complex64P

Complex64p returns stringer/JSON marshaler interface implementation for the pointer to the complex64 type.

func Duration added in v0.104.0

func Duration(v time.Duration) durationV

Duration returns stringer/JSON marshaler interface implementation for the time duration type.

func Durationp added in v0.104.0

func Durationp(p *time.Duration) durationP

Durationp returns stringer/JSON marshaler interface implementation for the pointer to the time duration type.

func Error added in v0.104.0

func Error(v error) errorV

Error returns stringer/JSON marshaler interface implementation for the error type.

func Float32 added in v0.104.0

func Float32(v float32) float32V

Float32 returns stringer/JSON marshaler interface implementation for the float32 type.

func Float32p added in v0.104.0

func Float32p(p *float32) float32P

Float32p returns stringer/JSON marshaler interface implementation for the pointer to the float32 type.

func Float64 added in v0.104.0

func Float64(v float64) float64V

Float64 returns stringer/JSON marshaler interface implementation for the float64 type.

func Float64p added in v0.104.0

func Float64p(p *float64) float64P

Float64p returns stringer/JSON marshaler interface implementation for the pointer to the float64 type.

func Func added in v0.104.0

func Func(v func() KV) funcV

Func returns stringer/JSON marshaler interface implementation for the custom func type.

func Int added in v0.104.0

func Int(v int) intV

Int returns stringer/JSON marshaler interface implementation for the int type.

func Int16 added in v0.104.0

func Int16(v int16) int16V

Int16 returns stringer/JSON marshaler interface implementation for the int16 type.

func Int16p added in v0.104.0

func Int16p(p *int16) int16P

Int16p returns stringer/JSON marshaler interface implementation for the pointer to the int16 type.

func Int32 added in v0.104.0

func Int32(v int32) int32V

Int32 returns stringer/JSON marshaler interface implementation for the int32 type.

func Int32p added in v0.104.0

func Int32p(p *int32) int32P

Int32p returns stringer/JSON marshaler interface implementation for the pointer to the int32 type.

func Int64 added in v0.104.0

func Int64(v int64) int64V

Int64 returns stringer/JSON marshaler interface implementation for the int64 type.

func Int64p added in v0.104.0

func Int64p(p *int64) int64P

Int64p returns stringer/JSON marshaler interface implementation for the pointer to the int64 type.

func Int8 added in v0.104.0

func Int8(v int8) int8V

Int8 returns stringer/JSON marshaler interface implementation for the int8 type.

func Int8p added in v0.104.0

func Int8p(p *int8) int8P

Int8p returns stringer/JSON marshaler interface implementation for the pointer to the int8 type.

func Intp added in v0.104.0

func Intp(p *int) intP

Intp returns stringer/JSON marshaler interface implementation for the pointer to the int type.

func Raw added in v0.104.0

func Raw(v []byte) rawV

Raw returns stringer/JSON marshaler interface implementation for the raw byte slice.

func Reflect added in v0.104.0

func Reflect(v interface{}) reflectV

func Runes added in v0.104.0

func Runes(v []rune) runesV

Runes returns stringer/JSON marshaler interface implementation for the rune slice type.

func Runesp added in v0.104.0

func Runesp(p *[]rune) runesP

Runesp returns stringer/JSON marshaler interface implementation for the pointer to the rune slice type.

func String

func String(v string) stringV

String returns stringer/JSON marshaler interface implementation for the string type.

func StringAny

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

func StringBool

func StringBool(k string, v bool) kvp

func StringBoolp

func StringBoolp(k string, v *bool) kvp

func StringBytes

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

func StringBytesp

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

func StringComplex128

func StringComplex128(k string, v complex128) kvp

func StringComplex128p

func StringComplex128p(k string, v *complex128) kvp

func StringComplex64

func StringComplex64(k string, v complex64) kvp

func StringComplex64p

func StringComplex64p(k string, v *complex64) kvp

func StringDuration

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

func StringDurationp

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

func StringError

func StringError(k string, v error) kvp

func StringFloat32

func StringFloat32(k string, v float32) kvp

func StringFloat32p

func StringFloat32p(k string, v *float32) kvp

func StringFloat64

func StringFloat64(k string, v float64) kvp

func StringFloat64p

func StringFloat64p(k string, v *float64) kvp

func StringFunc

func StringFunc(k string, v func() KV) kvp

func StringInt

func StringInt(k string, v int) kvp

func StringInt16

func StringInt16(k string, v int16) kvp

func StringInt16p

func StringInt16p(k string, v *int16) kvp

func StringInt32

func StringInt32(k string, v int32) kvp

func StringInt32p

func StringInt32p(k string, v *int32) kvp

func StringInt64

func StringInt64(k string, v int64) kvp

func StringInt64p

func StringInt64p(k string, v *int64) kvp

func StringInt8

func StringInt8(k string, v int8) kvp

func StringInt8p

func StringInt8p(k string, v *int8) kvp

func StringIntp

func StringIntp(k string, v *int) kvp

func StringRaw

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

func StringReflect

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

func StringRunes

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

func StringRunesp

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

func StringStringp

func StringStringp(k string, v *string) kvp

func StringTime

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

func StringTimep

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

func StringUint

func StringUint(k string, v uint) kvp

func StringUint16

func StringUint16(k string, v uint16) kvp

func StringUint16p

func StringUint16p(k string, v *uint16) kvp

func StringUint32

func StringUint32(k string, v uint32) kvp

func StringUint32p

func StringUint32p(k string, v *uint32) kvp

func StringUint64

func StringUint64(k string, v uint64) kvp

func StringUint64p

func StringUint64p(k string, v *uint64) kvp

func StringUint8

func StringUint8(k string, v uint8) kvp

func StringUint8p

func StringUint8p(k string, v *uint8) kvp

func StringUintp

func StringUintp(k string, v *uint) kvp

func StringUintptr

func StringUintptr(k string, v uintptr) kvp

func StringUintptrp

func StringUintptrp(k string, v *uintptr) kvp

func Stringp added in v0.104.0

func Stringp(p *string) stringP

Stringp returns stringer/JSON marshaler interface implementation for the pointer to the string type.

func Strings added in v0.104.0

func Strings(a ...string) kvp

func Text

func Text(v encoding.TextMarshaler) textV

Text returns stringer/JSON marshaler interface implementation for the encoding.TextMarshaler type.

func TextAny

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

func TextBool

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

func TextBoolp

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

func TextBytes

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

func TextBytesp

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

func TextComplex128

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

func TextComplex128p

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

func TextComplex64

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

func TextComplex64p

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

func TextDuration

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

func TextDurationp

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

func TextError

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

func TextFloat32

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

func TextFloat32p

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

func TextFloat64

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

func TextFloat64p

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

func TextFunc

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

func TextInt

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

func TextInt16

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

func TextInt16p

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

func TextInt32

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

func TextInt32p

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

func TextInt64

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

func TextInt64p

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

func TextInt8

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

func TextInt8p

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

func TextIntp

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

func TextRaw

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

func TextReflect

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

func TextRunes

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

func TextRunesp

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

func TextString

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

func TextStringp

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

func TextTime

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

func TextTimep

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

func TextUint

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

func TextUint16

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

func TextUint16p

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

func TextUint32

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

func TextUint32p

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

func TextUint64

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

func TextUint64p

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

func TextUint8

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

func TextUint8p

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

func TextUintp

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

func TextUintptr

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

func TextUintptrp

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

func Texts added in v0.104.0

func Texts(a ...encoding.TextMarshaler) kvp

func Time added in v0.104.0

func Time(v time.Time) timeV

Time returns stringer/JSON marshaler interface implementation for the time time type.

func Timep added in v0.104.0

func Timep(p *time.Time) timeP

Timep returns stringer/JSON marshaler interface implementation for the pointer to the time time type.

func Uint added in v0.104.0

func Uint(v uint) uintV

Uint returns stringer/JSON marshaler interface implementation for the uint type.

func Uint16 added in v0.104.0

func Uint16(v uint16) uint16V

Uint16 returns stringer/JSON marshaler interface implementation for the uint16 type.

func Uint16p added in v0.104.0

func Uint16p(p *uint16) uint16P

Uint16p returns stringer/JSON marshaler interface implementation for the pointer to the uint16 type.

func Uint32 added in v0.104.0

func Uint32(v uint32) uint32V

Uint32 returns stringer/JSON marshaler interface implementation for the uint32 type.

func Uint32p added in v0.104.0

func Uint32p(p *uint32) uint32P

Uint32p returns stringer/JSON marshaler interface implementation for the pointer to the uint32 type.

func Uint64 added in v0.104.0

func Uint64(v uint64) uint64V

Uint64 returns stringer/JSON marshaler interface implementation for the uint64 type.

func Uint64p added in v0.104.0

func Uint64p(p *uint64) uint64P

Uint64p returns stringer/JSON marshaler interface implementation for the pointer to the uint64 type.

func Uint8 added in v0.104.0

func Uint8(v uint8) uint8V

Uint8 returns stringer/JSON marshaler interface implementation for the uint8 type.

func Uint8p added in v0.104.0

func Uint8p(p *uint8) uint8P

Uint8p returns stringer/JSON marshaler interface implementation for the pointer to the uint8 type.

func Uintp added in v0.104.0

func Uintp(p *uint) uintP

Uintp returns stringer/JSON marshaler interface implementation for the pointer to the uint type.

func Uintptr added in v0.104.0

func Uintptr(v uintptr) uintptrV

Uintptr returns stringer/JSON marshaler interface implementation for the uintptr type.

func Uintptrp added in v0.104.0

func Uintptrp(p *uintptr) uintptrP

Uintptrp returns stringer/JSON marshaler interface implementation for the pointer to the uintptr type.

Types

type KV

type KV interface {
	encoding.TextMarshaler
	json.Marshaler
}

type Log

type Log struct {
	Output  io.Writer                 // Output is a destination for output.
	Flag    int                       // Flag is a log properties.
	KV      []KV                      // Key-values.
	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 1 = original message; 1 = message excerpt.
	Trunc   int                       // Maximum length of the message excerpt after which the message excerpt 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 (Log) New added in v0.95.0

func (l Log) New(kv ...KV) Logger

New returns copy of the logger with additional key-values. 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)

type Logger

type Logger interface {
	io.Writer
	// New returns copy of the logger with additional key-values.
	// Copy of the original key-values should have a lower priority
	// than the priority of the newer key-values.
	New(...KV) Logger
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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