protoc-gen-zap

command module
v0.0.0-...-14959c7 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

protoc-gen-zap

A protoc plugin that prevents PII from leaking into logs — generates uber-go/zap MarshalLogObject methods for your proto messages with compile-time field redaction.

The problem

Logging proto structs in Go typically means marshaling to JSON via reflection. This is slow and, more critically, it's easy to accidentally log PII fields like names, emails, or SSNs.

How it works

protoc-gen-zap generates type-safe MarshalLogObject methods for each proto message. Fields tagged with (zap.redact) = true are silently dropped from logs — they never appear, not even as empty strings.

No reflection. No PII leaks. No runtime overhead.

Why zap?

Zap's ObjectMarshaler interface writes fields directly into a pre-allocated buffer using typed methods (AddString, AddInt64, etc). Compared to encoding/json + reflection:

  • ~10x faster marshaling
  • Zero allocations in the hot path
  • Fields are encoded inline — no intermediate map or struct

Tag PII fields in your proto

import "zap/zap.proto";

message User {
    string id             = 1;
    string first_name     = 2 [(zap.redact) = true];  // never logged
    string last_name      = 3 [(zap.redact) = true];  // never logged
    int64  employee_number = 4 [(zap.redact) = true]; // never logged
    string physical_desk  = 6;                        // logged
}

Generated output

func (u *User) MarshalLogObject(enc zapcore.ObjectEncoder) error {
    enc.AddString("Id:", u.Id)
    enc.AddString("PhysicalDesk:", u.PhysicalDesk)
    // first_name, last_name, employee_number — omitted entirely
    return nil
}

Redacted fields are gone at compile time — there is no runtime check, no masking, no "[REDACTED]" string in your logs.

Install & run

go install github.com/ssd-tutorials/protoc-gen-zap

protoc -I ./ \
  --plugin=protoc-gen-zap=protoc-gen-zap \
  --zap_out=:./example \
  ./example/example.proto

This generates example.pb.zap.go alongside your existing example.pb.go.

Use in logs

logger.Info("user action", zap.Object("user", user))

Only non-redacted fields are written. PII fields never reach the log sink.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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