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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I create a standard zerolog.Event I can use as a middleware? #581

Open
renatospaka opened this issue Aug 18, 2023 · 0 comments
Open

Comments

@renatospaka
Copy link

Hello folks.

I have overwritten some of the events zerolog provides to improve my design. However, my code has become heavily duplicated and I want to reduce this duplication.

func (l *Logger) Error(msg string) {
	l.logg.Error().
		Str("appName", strings.ToUpper(l.App)).
		Str("environment", l.config.Environment).
		Str("method", l.methodOrService.method).
		Str("step", l.methodOrService.step).
		Str("transactionId", l.methodOrService.transactionId).
		Str("clientId", l.methodOrService.clientId).
		Str("frontDoor", l.methodOrService.frontDoor).
		Str("transactiionStatusOutput", l.methodOrService.transactiionStatusOutput).
		Msg(msg)
}
func (l *Logger) Panic(msg string) {
	l.logg.Panic().
		Str("appName", strings.ToUpper(l.App)).
		Str("environment", l.config.Environment).
		Str("method", l.methodOrService.method).
		Str("step", l.methodOrService.step).
		Str("transactionId", l.methodOrService.transactionId).
		Str("clientId", l.methodOrService.clientId).
		Str("frontDoor", l.methodOrService.frontDoor).
		Str("transactiionStatusOutput", l.methodOrService.transactiionStatusOutput).
		Msg(msg)
}

These methods above repeat to Warn(), Info(), Error(), Debug().

The main goal is to reduce coder effort. But my code turned into spaghetti and I need directions to refactor it, creating a kind of "middleware" to encapsulate it:

Str("appName", strings.ToUpper(l.App)).
Str("environment", l.config.Environment).
Str("method", l.methodOrService.method).
Str("step", l.methodOrService.step).
Str("transactionId", l.methodOrService.transactionId).
Str("clientId", l.methodOrService.clientId).
Str("frontDoor", l.methodOrService.frontDoor).
Str("transactiionStatusOutput", l.methodOrService.transactiionStatusOutput).

So I created an zerolog.Event with all those Str but it completely failed on implementation.

Now I am open to suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant