https://github.com/pfmt/plog
JSON logger for Go.
https://github.com/pfmt/plog
go golang log logging
Last synced: 6 months ago
JSON representation
JSON logger for Go.
- Host: GitHub
- URL: https://github.com/pfmt/plog
- Owner: pfmt
- License: bsd-3-clause
- Created: 2020-10-15T20:21:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-07-17T12:01:49.000Z (almost 4 years ago)
- Last Synced: 2025-03-15T13:14:10.024Z (over 1 year ago)
- Topics: go, golang, log, logging
- Language: Go
- Homepage:
- Size: 221 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# plog
[](https://cloud.drone.io/pfmt/plog)
[](https://pkg.go.dev/github.com/pfmt/plog)
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
```go
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:
```json
{
"message":"Hello,\nWorld!",
"excerpt":"Hello, World…"
}
```
## Use as GELF formater
```go
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:
```json
{
"version":"1.1",
"short_message":"Hello, GELF!",
"full_message":"Hello,\nGELF!",
"timestamp":1602785340
}
```
## Caveat: numeric types appears in the message as a string
```go
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:
```json
{
"message":"123"
}
```
Output 2:
```json
{
"message":"3.21"
}
```
## Benchmark
```sh
$ 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
```