Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: Implement append encoder, allow flatter filters config #6069

Merged
merged 4 commits into from
Mar 5, 2024

Conversation

francislavoie
Copy link
Member

@francislavoie francislavoie commented Jan 28, 2024

Feature requested by @yroc92

This adds a simple encoder called append which can append one or more log fields to all log entries that the logger processes.

This is inspired (and largely copy-paste) of the filter encoder, and as such it wraps another encoder (defaulting to json or console as per #5980).

Since this module does not have access to the request context, it cannot use request placeholders, but it may use global placeholders such as the ones from https://caddyserver.com/docs/conventions#placeholders.

This is complementary to #6066 which allows appending extra log fields to access logs only, and allows appending data from the request context.

Most commonly, this will probably be used to append things like a Caddy instance ID, to identify which instance the log was produced from in some log ingress tool.

For example, in global options:

{
	log {
		format append {
			fields {
				machine {env.MACHINE}
			}
		}
	}
}

And running with MACHINE=foo caddy run, a log like this is shown:

2024/01/28 06:39:51.145	INFO	serving initial configuration	{"machine": "foo"}

The key functionality of this encoder is in the func (fe AppendEncoder) EncodeEntry function (near the end of the file). The rest of the code is pretty much just zap boilerplate because we need to implement the zapcore.Encoder interface which has a ton of functions 馃槵 we're just passing them through to the wrapped encoder.

@francislavoie
Copy link
Member Author

francislavoie commented Jan 28, 2024

I just added another commit which makes it possible to configure fields for both filter and append encoders in a flatter way, allowing to skip the fields { } wrapper. The wrapper is only needed if you ever have a field you want to call wrap or fields 馃槄

Before:

{
	log {
		format append {
			fields {
				machine {env.MACHINE}
			}
		}
	}
}

After:

{
	log {
		format append {
			machine {env.MACHINE}
		}
	}
}

@francislavoie francislavoie changed the title logging: Implement add encoder logging: Implement add encoder, allow flatter filters config Feb 14, 2024
@mholt
Copy link
Member

mholt commented Mar 5, 2024

Oh -- lol, would it be prudent to rename this to AppendEncoder or would that be terribly annoying? 馃槄

@francislavoie
Copy link
Member Author

Okay 馃憤

@francislavoie francislavoie changed the title logging: Implement add encoder, allow flatter filters config logging: Implement append encoder, allow flatter filters config Mar 5, 2024
GO_SEMVER: '~1.22.0'
GO_SEMVER: '~1.22.1'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Francis,

image

@mholt mholt merged commit 01d5568 into master Mar 5, 2024
25 checks passed
@mholt mholt deleted the add-encoder branch March 5, 2024 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 鈿欙笍 New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants