PLogger Structured Logger

Go Report Card Go CI codecov

A lightweight logger for Go with structured output and pluggable formatter/writer.

Table of Contents
  1. About The Project
  2. Features
  3. Getting Started
  4. Usage
  5. Benchmarks
  6. License
  7. Contact

Features

  • High performance.
  • Modular design: easily swap formatters & writers.
  • Supports multiple outputs (console, file, discard) for writer.
  • Benchmark against zap, zerolog, logrus, etc.

Getting Started

Note that we only supports the two most recent minor versions of Go.

go get github.com/lthphuw/plogger@latest

Usage

Quick start

1formatter, _ := plogger.NewJSONFormatter() 2writer, _ := plogger.NewConsoleWriter() 3 4// Create logger options using builder pattern 5opts := plogger.NewLoggerOptions(). 6 SetWriter(writer). 7 SetFormatter(formatter). 8 SetCaller(true) 9 10// Create logger 11logger, _ := plogger.NewLogger(opts) 12 13// Log an info message with additional fields 14logger.Info(plogger.NewEntry(). 15 SetMsg("Hi world"). 16 SetFieldMap(map[string]any{ 17 "app": "my-service", 18 "env": "prod", 19 }))

Output will look something like this:

1{ 2 "app": "my-service", 3 "env": "prod", 4 "file": ".../main/main.go", 5 "func": "main.main", 6 "level": "info", 7 "line": 22, 8 "msg": "Hi world", 9 "timestamp": "2025-05-22T15:47:36+07:00" 10}

Advanced Usage

Custom fields

1 2entry := plogger.NewEntry(). 3 SetMsg("User login"). 4 AddField("user_id", 123). 5 AddField("ip", "192.168.1.1") 6 7logger.Info(entry)

Use File Writer (Supports Rotation & Safe Concurrency)

1fileWriter, _ := plogger.NewFileWriter( 2 plogger.NewFileWriterOptions().SetFilename("/var/log/app.log"), 3) 4 5logger, _ := plogger.NewLogger( 6 plogger.NewLoggerOptions(). 7 SetWriter(fileWriter). 8 SetFormatter(formatter), 9)

Benchmarks

The following benchmarks are adapted from the original zap logging benchmarks.
We've reused the same test scenarios and environment setup to ensure a fair comparison, with additional entries for plogger.

Log a message:

PackageTime (ns/op)% to ploggerObjects Allocated
plogger (Ours)554.7+0%4 allocs/op
zap45.91-91.7%0 allocs/op
zap.Check42.50-92.3%0 allocs/op
zap.CheckSampled28.65-94.8%0 allocs/op
zap.Sugar56.20-89.9%1 allocs/op
zap.SugarFormat1533+176.3%58 allocs/op
zerolog26.43-95.2%0 allocs/op
zerolog.Check25.78-95.4%0 allocs/op
zerolog.Format1470+165%58 allocs/op
go-kit181.3-67.3%8 allocs/op
slog190.5-65.7%0 allocs/op
slog.LogAttrs189.1-65.9%0 allocs/op
apex/log822.1+48.2%4 allocs/op
log152040+267.6%17 allocs/op
logrus1361+145.3%20 allocs/op
stdlib.Println146.3-73.6%1 allocs/op
stdlib.Printf1100+98.3%57 allocs/op

Log a message and 10 fields:

PackageTime (ns/op)% to ploggerObjects Allocated
plogger (Ours)3647+0%4 allocs/op
zap614.6-83.1%5 allocs/op
zap.Check616.1-83.1%5 allocs/op
zap.CheckSampled94.63-97.4%0 allocs/op
zap.Sugar1049-71.2%10 allocs/op
zerolog260.7-92.8%1 allocs/op
zerolog.Check261.1-92.8%1 allocs/op
go-kit2214-39.3%56 allocs/op
slog2198-39.7%41 allocs/op
slog.LogAttrs2336-36.0%40 allocs/op
apex/log10595+190.5%64 allocs/op
log1511873+225.7%73 allocs/op
logrus12226+235.2%84 allocs/op

License

This project is licensed under the MIT License – see the LICENSE file for details.

Contact

Maintained by Phu.
For questions, feedback, or support, please contact: lthphuw@gmail.com

(back to top)