Home > Backend Development > Golang > How to pass a structure in slog logger and use its fields automatically?

How to pass a structure in slog logger and use its fields automatically?

WBOY
Release: 2024-02-06 08:10:03
forward
469 people have browsed it

如何在 slog 记录器中传递结构并自动使用其字段?

Question content

I am using the slog package. The problem I'm facing is that when I have most of the parameters in a struct, I have to pass too many parameters to it.

Is there a way to modify the handler to use this structure? Just like what you can do in python, send a dictionary or object as extra content and then extract the required parameters from it.

Now I have this:

g.l.Log(
    context.TODO(),
    slog.LevelInfo,
    "Sending request to server.",
    "Destination", m.route.destination,
    "Protocol", m.route.protocol,
    "Service Identifier", m.route.serID,
    "Session ID", m.GetIdentifier(),
    "Client Connection", client.RemoteAddr().String(),
    "Server Connection", destination.RemoteAddr().String(),
)
Copy after login

I want to do something like this:

g.l.Log(
    context.TODO(),
    slog.LevelInfo,
    "Sending request to server.",
    "message", m,
    "Client Connection", client.RemoteAddr().String(),
    "Server Connection", destination.RemoteAddr().String(),
)
Copy after login

what should I do?


Correct answer


I found the answer to this question.

I embedded the slog logger into my custom logger.

type Logger struct {
    *slog.Logger
}
Copy after login

I also wrote an export function for my struct like this:

func (m *GatewayMessage) LoggableData() *xlog.RequestData {
    return &xlog.RequestData{
        Request:             m.Request.String(),
        OriginalRequest:     m.OriginalRequest,
    }
}

func (m *GatewayMessage) PopulateGeneralLogData() []any {
    logData := m.LoggableData()
    return []any{
        "Request", logData.Request,
        "OriginalRequest", logData.OriginalRequest,
    }
}
Copy after login

I then write a helper function that takes this GatewayMessage as a parameter along with any number of parameters, such as the slog Logger's Log function. Here is an example of debugging functionality:

func LogDebug(l *xlog.Logger, ctx context.Context, msg string, m *GatewayMessage, args ...any) {
    var generalLogData []any = make([]any, 0)
    if m != nil {
        generalLogData = m.PopulateGeneralLogData()
    }
    args = append(args, generalLogData...)
    l.RuntimeDebug(
        ctx,
        msg,
        args...,
    )
}
Copy after login

I also use a receiver named RuntimeDebug to inject a parameter named Scope in all logs.

func (l *Logger) RuntimeDebug(ctx context.Context, msg string, args ...any) {
    args = append(args, "Scope", "Runtime")
    l.Logger.Log(
        ctx,
        slog.LevelDebug,
        msg,
        args...,
    )
}
Copy after login

The above is the detailed content of How to pass a structure in slog logger and use its fields automatically?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template