Skip to content

Conversation

flejz
Copy link

@flejz flejz commented Jun 26, 2024

One use case that me and my coworkers found is that we would often be loggin the same fields from a Struct over and over again, so we came up with the idea of logging tagged structs. That's what this PR is about.

That introduces a new concept of tagging the Struct with log followed by the log field name as the example below:

type User struct {
  Name   string `log:"name"`
  Active bool   `log:"is_active"`
}

Usage

We are introducing a new logging method Struct where you can pass the struct as an interface parameter.

user := User {
  Name:   "dude",
  Active: true,
}

log.Log().Struct(user).Msg("")

That would generate the log result as follows:

{"name":"dude","is_active":true}

PS: Ommiting the log tag will omit the struct field from the logs
PS2: Nested struct fields are also supported

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

Successfully merging this pull request may close these issues.

1 participant