Demonstrates using protoc-gen-slogschema with a Connect-RPC / gRPC service.
The plugin generates a MarshalSLog() []slog.Attr method directly on each protoc-gen-go message type. This means:
- One struct for both wire transport and logging — no separate logging struct, no field mapping
- Annotations in the
.protofile control logging behaviour at the source of truth *Userimplementsslogschema.SLogMarshalerautomatically — pass it straight toToAttrs
| File | Origin | Description |
|---|---|---|
user.proto |
Hand-written | Proto definition with slogschema annotations |
user.pb.go |
protoc-gen-go (simulated) |
Standard Go message types and getters |
user.slogschema.pb.go |
protoc-gen-slogschema (simulated) |
Generated MarshalSLog() methods |
main.go |
Hand-written | Usage example |
In a real project user.pb.go and user_slogschema.pb.go are both generated — you only maintain user.proto.
go run github.com/silvercory/slog-schema/example/protobuflevel=INFO msg="user loaded" id=sha256:... email=sha256:... name="Bob Smith" phone=sha256:... role=editor created_at=2024-03-15T09:00:00Z address.street="10 Downing St" address.city=London address.country_code=GB notify_email=alerts@example.com roles.0=read roles.1=write
level=ERROR msg="user suspended" id=sha256:... email=sha256:... name="Bob Smith" phone=sha256:... role=suspended ...
level=INFO msg="notification method changed" ... notify_sms=+447700900001 ...
level=INFO msg="user updated" ... (address absent)
Notice:
password_hashnever appears —skip: truein the proto means it is absent fromMarshalSLogentirelyid,email,phoneare hashed on every linedisplay_nameis logged asname(custom key vianame: "name"annotation)notify_email/notify_sms— only the setoneofvariant appears; it switches on the third lineaddressdisappears on the last line (omitempty+ nil pointer)
message User {
uint64 id = 1 [(slogschema.v1.field) = {hashed: true, omitempty: true}];
string email = 2 [(slogschema.v1.field) = {hashed: true}];
string display_name = 3 [(slogschema.v1.field) = {name: "name"}];
string phone = 4 [(slogschema.v1.field) = {hashed: true, omitempty: true}];
string role = 5;
string password_hash = 6 [(slogschema.v1.field) = {skip: true}];
...
}Install the plugin and run protoc alongside your existing generators:
go install github.com/silvercory/slog-schema/cmd/protoc-gen-slogschema@latest
protoc \
--go_out=. \
--go-grpc_out=. \
--slogschema_out=. \
--proto_path=. \
api/user.protoWith buf:
# buf.gen.yaml
plugins:
- plugin: go
out: gen
- plugin: connect-go
out: gen
- plugin: slogschema
out: gen