Skip to content

pfmt/plog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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