Skip to content

Conversation

@alovak
Copy link
Contributor

@alovak alovak commented May 1, 2025

Add the StructContext function that allows to log struct fields automatically by using tags.

import "github.com/moov-io/base/log"

// Define a struct with log tags
type User struct {
    ID       int    `log:"id"`
    Username string `log:"username"`
    Email    string `log:"email,omitempty"` // won't be logged if empty
    Address  Address `log:"address"` // nested struct must have log tag
    Hidden   string // no log tag, won't be logged
}

//...
// Log with struct context
logger.With(log.StructContext(user)).Info().Log("User logged in")

@alovak alovak requested review from InfernoJJ and adamdecaf as code owners May 1, 2025 12:35
@alovak alovak force-pushed the add-struct-context branch from ca61fba to cfedbb2 Compare May 1, 2025 13:06
@adamdecaf
Copy link
Member

Cool idea. It shouldn't be too slow for small/medium structs. This can go in the next minor release.

@alovak alovak merged commit 8ee8683 into master May 1, 2025
15 checks passed
@alovak
Copy link
Contributor Author

alovak commented May 1, 2025

Now we can see what goes to logs and what to traces/spans :)

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.

4 participants