tracing icon indicating copy to clipboard operation
tracing copied to clipboard

tracing-subscriber: add option to rename `level` in JSON log formatter

Open abonander opened this issue 2 years ago • 0 comments

Feature Request

Crates

  • tracing-subscriber

Motivation

When logging from a container in Google Kubernetes Engine, the logging agent can parse JSON log messages and make them browsable and queryable in the Log Explorer. There are a number of fields that it recognizes by default: https://cloud.google.com/logging/docs/structured-logging#special-payload-fields

The most important one is severity, which roughly maps to tracing's concept of levels, and in fact it seems to recognize most of the options for tracing::Level, except TRACE. The docs say it attempts to match one of the severity levels, but doesn't say what level is chosen if that fails; I presume it falls back to DEFAULT which is a decent analogue for TRACE as it's the only supported severity that falls below DEBUG.

You're likely not going to want to log everything in production anyway as you have to pay for the storage of logs, so in practice even if TRACE doesn't have a clean mapping to severities, it likely won't be an issue.

It should match WARN as WARNING if it's doing substring matches, but that's probably worth testing.

Proposal

Add a setter to tracing_subscriber::fmt::format::Json to override the field name used here.

I'm not sure what I would call the setter though. with_field_name_for_level? with_level_key?

Alternatives

It's always possible to write a custom implementation of FormatEvent but I'd rather not have to do that as I'd have to duplicate the functionality of this entire impl block.

abonander avatar May 18 '22 00:05 abonander